@Override public boolean visit(FieldDeclaration node) { int modifiers = node.getModifiers(); if (Modifier.isPrivate(modifiers) || Modifier.isProtected(modifiers)) { List<VariableDeclarationFragment> fragments = node.fragments(); for (VariableDeclarationFragment varDeclFrag : fragments) { IVariableBinding varBinding = varDeclFrag.resolveBinding(); String enclosingClass = varBinding.getDeclaringClass().getQualifiedName(); if(!varBinding.getType().isPrimitive() && !varBinding.getType().getQualifiedName().equals("java.lang.String")){ final TACVariable fieldVar = new TACVariable(varBinding); if(!enclosingClass.equals(Config.MAINCLASS)){ context.addEncapsulatedVariable(fieldVar); } } } } return super.visit(node); }
@Override public boolean visit(VariableDeclarationFragment node) { if (isDeclarationTarget(DeclarationType.FIELD_DECLARATION)) { IVariableBinding variableBinding = node.resolveBinding(); if (variableBinding != null) { if (variableBinding.isField()) { variableFound = variableBinding.getName(); if (matchTypeDeclaration() && TraceUtility .match(variableToFind, variableFound)) { TraceUtility.selectInEditor(node); setEnclosingDeclaration(node); } } } } return super.visit(node); }
private static void getLocalVariableLabel(IVariableBinding binding, long flags, StringBuffer buffer) { if (((flags & JavaElementLabels.F_PRE_TYPE_SIGNATURE) != 0)) { getTypeLabel(binding.getType(), (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); buffer.append(' '); } if (((flags & JavaElementLabels.F_FULLY_QUALIFIED) != 0)) { IMethodBinding declaringMethod= binding.getDeclaringMethod(); if (declaringMethod != null) { getMethodLabel(declaringMethod, flags, buffer); buffer.append('.'); } } buffer.append(binding.getName()); if (((flags & JavaElementLabels.F_APP_TYPE_SIGNATURE) != 0)) { buffer.append(JavaElementLabels.DECL_STRING); getTypeLabel(binding.getType(), (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); } }
/** * Returns the label for a Java element with the flags as defined by {@link JavaElementLabels}. * @param binding The binding to render. * @param flags The text flags as defined in {@link JavaElementLabels} * @return the label of the binding */ public static String getBindingLabel(IBinding binding, long flags) { StringBuffer buffer= new StringBuffer(60); if (binding instanceof ITypeBinding) { getTypeLabel(((ITypeBinding) binding), flags, buffer); } else if (binding instanceof IMethodBinding) { getMethodLabel(((IMethodBinding) binding), flags, buffer); } else if (binding instanceof IVariableBinding) { final IVariableBinding variable= (IVariableBinding) binding; if (variable.isField()) { getFieldLabel(variable, flags, buffer); } else { getLocalVariableLabel(variable, flags, buffer); } } return Strings.markLTR(buffer.toString()); }
public static String getImportName(IBinding binding) { ITypeBinding declaring= null; switch (binding.getKind()) { case IBinding.TYPE: return getRawQualifiedName((ITypeBinding) binding); case IBinding.PACKAGE: return binding.getName() + ".*"; //$NON-NLS-1$ case IBinding.METHOD: declaring= ((IMethodBinding) binding).getDeclaringClass(); break; case IBinding.VARIABLE: declaring= ((IVariableBinding) binding).getDeclaringClass(); if (declaring == null) { return binding.getName(); // array.length } break; default: return binding.getName(); } return JavaModelUtil.concatenateName(getRawQualifiedName(declaring), binding.getName()); }
/** * Finds the field specified by <code>fieldName</code> in * the type hierarchy denoted by the given type. Returns <code>null</code> if no such field * exists. If the field is defined in more than one super type only the first match is * returned. First the super class is examined and then the implemented interfaces. * @param type The type to search the field in * @param fieldName The name of the field to find * @return the variable binding representing the field */ public static IVariableBinding findFieldInHierarchy(ITypeBinding type, String fieldName) { IVariableBinding field= findFieldInType(type, fieldName); if (field != null) { return field; } ITypeBinding superClass= type.getSuperclass(); if (superClass != null) { field= findFieldInHierarchy(superClass, fieldName); if (field != null) { return field; } } ITypeBinding[] interfaces= type.getInterfaces(); for (int i= 0; i < interfaces.length; i++) { field= findFieldInHierarchy(interfaces[i], fieldName); if (field != null) { return field; } } return null; }
/** * Returns the binding of the variable written in an Assignment. * @param assignment The assignment * @return The binding or <code>null</code> if no bindings are available. */ public static IVariableBinding getAssignedVariable(Assignment assignment) { Expression leftHand = assignment.getLeftHandSide(); switch (leftHand.getNodeType()) { case ASTNode.SIMPLE_NAME: return (IVariableBinding) ((SimpleName) leftHand).resolveBinding(); case ASTNode.QUALIFIED_NAME: return (IVariableBinding) ((QualifiedName) leftHand).getName().resolveBinding(); case ASTNode.FIELD_ACCESS: return ((FieldAccess) leftHand).resolveFieldBinding(); case ASTNode.SUPER_FIELD_ACCESS: return ((SuperFieldAccess) leftHand).resolveFieldBinding(); default: return null; } }
/** * for an assignment x = y returns the variable binding of x * * @param varBinding * @param assignment * @return */ private static IVariableBinding getLeftHandSideVarBinding(Assignment assignment) { Expression leftHnsd = assignment.getLeftHandSide(); if (leftHnsd instanceof SimpleName) { IBinding bind = ((SimpleName) leftHnsd).resolveBinding(); if (bind instanceof IVariableBinding) { return ((IVariableBinding) bind).getVariableDeclaration(); } } if (leftHnsd instanceof FieldAccess) { FieldAccess fa = (FieldAccess) leftHnsd; return fa.resolveFieldBinding(); } // Leave it null - cannot determine actual domains for arrays // workaround for bugs related to objects created in complex expression // if (leftHnsd instanceof ArrayAccess) { // ArrayAccess aa = (ArrayAccess) leftHnsd; // return getArrayAccessVarBinding(aa); // } return null; }
public static IBinding getEnclosingDeclaration(ASTNode node) { while(node != null) { if (node instanceof AbstractTypeDeclaration) { return ((AbstractTypeDeclaration)node).resolveBinding(); } else if (node instanceof AnonymousClassDeclaration) { return ((AnonymousClassDeclaration)node).resolveBinding(); } else if (node instanceof MethodDeclaration) { return ((MethodDeclaration)node).resolveBinding(); } else if (node instanceof FieldDeclaration) { List<?> fragments= ((FieldDeclaration)node).fragments(); if (fragments.size() > 0) { return ((VariableDeclarationFragment)fragments.get(0)).resolveBinding(); } } else if (node instanceof VariableDeclarationFragment) { IVariableBinding variableBinding= ((VariableDeclarationFragment)node).resolveBinding(); if (variableBinding.getDeclaringMethod() != null || variableBinding.getDeclaringClass() != null) { return variableBinding; // workaround for incomplete wiring of DOM bindings: keep searching when variableBinding is unparented } } node= node.getParent(); } return null; }
/** * @param anInfo * @param varBinding * @return */ public static AnnotationInfo getAnnotationAliasXML(IVariableBinding varBinding) { AnnotationInfo anInfo = null; VariableAnnotationInfo methParamDecl = null; // distinguish between arguments and fields (a field has no declaring // method) if (varBinding.getDeclaringMethod() != null) methParamDecl = VariableAnnotationInfo.getBinding(varBinding.getDeclaringMethod().getKey() + varBinding.getType().getKey()); else methParamDecl = VariableAnnotationInfo.getBinding(varBinding.getType().getKey()); if (methParamDecl != null) { anInfo = methParamDecl.getAnnotationInfo(); } return anInfo; }
@Override public void endVisit(SimpleName node) { if (skipNode(node) || node.isDeclaration()) { return; } IBinding binding = node.resolveBinding(); if (binding instanceof IVariableBinding) { IVariableBinding variable = (IVariableBinding) binding; if (!variable.isField()) { setFlowInfo(node, new LocalFlowInfo(variable, FlowInfo.READ, fFlowContext)); } } else if (binding instanceof ITypeBinding) { ITypeBinding type = (ITypeBinding) binding; if (type.isTypeVariable()) { setFlowInfo(node, new TypeVariableFlowInfo(type, fFlowContext)); } } }
protected List<DomainP> getArgActualDomains(Variable arg, InvocationInstruction instr, Map<Variable, List<DomainP>> Gamma, QualifiedClassName C) { if (Gamma.containsKey(arg)) return Gamma.get(arg); if (Utils.isThis(arg)) { QualifiedClassName argType = new QualifiedClassName(arg.resolveType(), C); List<DomainP> thisFormalDomains = getDomainParamsDecls(argType); Gamma.put(arg, thisFormalDomains); return thisFormalDomains; } IVariableBinding varBinding = Utils.getArgVarBinding(arg, instr); if (varBinding != null) { return addToGamma(Gamma, varBinding, C); } return null; }
@Override public boolean visit(SimpleName node) { if (currentMethod == null) return false; IBinding binding = node.resolveBinding(); if (binding == null) return false; if (node.isDeclaration()) return true; if (node.resolveBinding() instanceof IVariableBinding) { IVariableBinding iVariableBinding = (IVariableBinding) node.resolveBinding(); if (iVariableBinding.isField()) { IVariableBinding variableDeclarationBinding = iVariableBinding.getVariableDeclaration(); if (variableDeclarationBinding.getDeclaringClass() != null) { IJavaElement accessedField = variableDeclarationBinding.getJavaElement(); if (accessedField instanceof IField) { if (!((IField) accessedField).isReadOnly()) methodDetails.addAccess((IField) accessedField); } } } } return true; }
private String finalErrorMessage(IVariableBinding variableBinding) { String variableKind = ""; String encClass = ""; if(variableBinding.isField()){ variableKind = " field "; encClass = variableBinding.getDeclaringClass().getQualifiedName(); } else if(variableBinding.isParameter()){ variableKind = " method parameter "; encClass = variableBinding.getDeclaringMethod().getDeclaringClass().getQualifiedName(); } else { variableKind = " local variable "; encClass = variableBinding.getDeclaringMethod().getDeclaringClass().getQualifiedName(); } StringBuilder errorMsg = new StringBuilder(); errorMsg.append("The "); errorMsg.append(variableKind); errorMsg.append(variableBinding.getName()); errorMsg.append(" in the class "); errorMsg.append(encClass); errorMsg.append(" is not final"); return errorMsg.toString(); }
public static FieldDeclaration createFrom(IVariableBinding fieldDecl) { FieldDeclaration retNode = null; Adapter factory = Adapter.getInstance(); AstNode astNode = factory.get(fieldDecl); if ( astNode instanceof FieldDeclaration ) { retNode = (FieldDeclaration)astNode; } else { retNode = FieldDeclaration.create(); retNode.enclosingType = TraceabilityFactory.getTypeDeclaration(fieldDecl.getDeclaringClass().getQualifiedName()); retNode.enclosingDeclaration = retNode.enclosingType; retNode.fieldName = fieldDecl.getName(); retNode.fieldType = TraceabilityFactory.getType(fieldDecl.getType()); factory.map(fieldDecl, retNode); factory.mapFieldDeclaration(retNode); } return retNode; }
private void sortTypes(ITypeBinding[] typeProposals) { ITypeBinding oldType; if (fBinding instanceof IMethodBinding) { oldType= ((IMethodBinding) fBinding).getReturnType(); } else { oldType= ((IVariableBinding) fBinding).getType(); } if (! oldType.isParameterizedType()) { return; } final ITypeBinding oldTypeDeclaration= oldType.getTypeDeclaration(); Arrays.sort(typeProposals, new Comparator<ITypeBinding>() { @Override public int compare(ITypeBinding o1, ITypeBinding o2) { return rank(o2) - rank(o1); } private int rank(ITypeBinding type) { if (type.getTypeDeclaration().equals(oldTypeDeclaration)) { return 1; } return 0; } }); }
private Expression evaluateArgumentExpressions(AST ast, ITypeBinding requiredType, String key) { CompilationUnit root= (CompilationUnit) fCallerNode.getRoot(); int offset= fCallerNode.getStartPosition(); Expression best= null; ITypeBinding bestType= null; ScopeAnalyzer analyzer= new ScopeAnalyzer(root); IBinding[] bindings= analyzer.getDeclarationsInScope(offset, ScopeAnalyzer.VARIABLES); for (int i= 0; i < bindings.length; i++) { IVariableBinding curr= (IVariableBinding) bindings[i]; ITypeBinding type= curr.getType(); if (type != null && canAssign(type, requiredType) && testModifier(curr)) { if (best == null || isMoreSpecific(bestType, type)) { best= ast.newSimpleName(curr.getName()); bestType= type; } } } Expression defaultExpression= ASTNodeFactory.newDefaultExpression(ast, requiredType); if (best == null) { best= defaultExpression; } return best; }
private static boolean addGetterSetterProposal(IInvocationContext context, ASTNode coveringNode, Collection<CUCorrectionProposal> proposals) { if (!(coveringNode instanceof SimpleName)) { return false; } SimpleName sn = (SimpleName) coveringNode; IBinding binding = sn.resolveBinding(); if (!(binding instanceof IVariableBinding)) { return false; } IVariableBinding variableBinding = (IVariableBinding) binding; if (!variableBinding.isField()) { return false; } if (proposals == null) { return true; } CUCorrectionProposal proposal = getProposal(context.getCompilationUnit(), sn, variableBinding); if (proposal != null) { proposals.add(proposal); } return true; }
private static String getExpressionBaseName(Expression expr) { IBinding argBinding= Bindings.resolveExpressionBinding(expr, true); if (argBinding instanceof IVariableBinding) { IJavaProject project= null; ASTNode root= expr.getRoot(); if (root instanceof CompilationUnit) { ITypeRoot typeRoot= ((CompilationUnit) root).getTypeRoot(); if (typeRoot != null) { project= typeRoot.getJavaProject(); } } return StubUtility.getBaseName((IVariableBinding)argBinding, project); } if (expr instanceof SimpleName) { return ((SimpleName) expr).getIdentifier(); } return null; }
@Override public boolean visit(VariableDeclarationFragment node) { IVariableBinding variableBinding = node.resolveBinding(); addNewBinding(variableBinding, node); addNewTypeBinding(variableBinding.getType()); return true; }
/** * for a store instruction x = y, returns the domain of y * * @param instr * @param value * @return */ protected List<DomainP> getSourceActualDomains(StoreInstruction instr, Map<Variable, List<DomainP>> Gamma, QualifiedClassName C) { Variable sourceOperand = instr.getSourceOperand(); if (Gamma.containsKey(sourceOperand)) return Gamma.get(sourceOperand); if (Utils.isThis(sourceOperand)) { QualifiedClassName sourceType = new QualifiedClassName(sourceOperand.resolveType(), C); List<DomainP> thisFormalDomains = getDomainParamsDecls(sourceType); Gamma.put(sourceOperand, thisFormalDomains); return thisFormalDomains; } IVariableBinding varBinding = Utils.getSourceVarBinding(instr); if (varBinding != null) { return addToGamma(Gamma, varBinding, C); } return null; }
private void extractParametersFromSource(List<Variable> parametersVarList, IMethod javaElement) { Option<MethodDeclaration> mDecl = WorkspaceUtilities.getMethodDeclFromModel(javaElement); MethodDeclaration mmDecl = mDecl.unwrap(); List<SingleVariableDeclaration> parameters = mmDecl.parameters(); for (SingleVariableDeclaration param : parameters) { IVariableBinding paramBinding = param.resolveBinding(); if(!paramBinding.getType().isPrimitive()){ Variable paramVariable = this.tm.getVariableFromBindingKey(paramBinding.getKey()); if(paramVariable!=null){ parametersVarList.add(paramVariable); } } } }
/** * Returns true iff 'methodDeclaration' represents a void static method named 'main' that takes a * single String[] parameter. */ private static boolean isMainMethod(MethodDeclaration methodDeclaration) { // Is it static? if ((methodDeclaration.getModifiers() & Modifier.STATIC) == 0) { return false; } // Does it return void? Type returnType = methodDeclaration.getReturnType2(); if (!returnType.isPrimitiveType()) { return false; } if (((PrimitiveType) returnType).getPrimitiveTypeCode() != PrimitiveType.VOID) { return false; } // Is it called 'main'? if (!"main".equals(methodDeclaration.getName().getIdentifier())) { return false; } // Does it have a single parameter? if (methodDeclaration.parameters().size() != 1) { return false; } // Is the parameter's type String[]? SingleVariableDeclaration pt = getOnlyElement((List<SingleVariableDeclaration>) methodDeclaration.parameters()); IVariableBinding vb = pt.resolveBinding(); if (vb == null) { return false; } ITypeBinding tb = vb.getType(); return tb != null && "java.lang.String[]".equals(tb.getQualifiedName()); }
/** * @param project * @param itype * @return * @throws JavaModelException */ private static IPath findPathInStaticField(IProject project, IType itype) throws JavaModelException { List<IPath> wrapper = new ArrayList<IPath>(); ICompilationUnit cu = itype.getCompilationUnit(); CompilationUnit ast = parse(cu); ast.accept(new ASTVisitor() { public boolean visit(VariableDeclarationFragment node) { SimpleName simpleName = node.getName(); IBinding bding = simpleName.resolveBinding(); if (bding instanceof IVariableBinding) { IVariableBinding binding = (IVariableBinding) bding; String type = binding.getType().getBinaryName(); // String name = simpleName.getFullyQualifiedName(); if ("MODEL_PATH".equals(name) && "java.nio.file.Path".equals(type)) { Expression expression = node.getInitializer(); if (expression instanceof MethodInvocation) { MethodInvocation mi = (MethodInvocation) expression; if ("get".equals(mi.resolveMethodBinding().getName()) && "java.nio.file.Path".equals(mi.resolveTypeBinding().getBinaryName())) { StringLiteral sl = (StringLiteral) mi.arguments().get(0); String argument = sl.getLiteralValue(); try { IPath p = ResourceManager.find(project, argument); wrapper.add(p); } catch (CoreException e) { ResourceManager.logException(e); } } } } } return true; } }); if (wrapper.size() > 0) return wrapper.get(0); return null; }
/** * Extracts the fully qualified name from a variable binding and applies * them to the name in a SourceCodeEntity. * @param binding The variable binding. * @param sce SourceCodeEntity to which to apply changes. Name must be set * to the entity's unqualified name. */ private static void extractDataFromVariableBinding( IVariableBinding binding, SourceCodeEntity sce) { if (binding != null) { //Type member variable. ITypeBinding type = binding.getDeclaringClass(); if (type != null) sce.name = type.getQualifiedName() + "." + sce.name; //Variable declared in method. else { IMethodBinding method = binding.getDeclaringMethod(); if (method != null) { type = method.getDeclaringClass(); if (type != null) { sce.name = type.getQualifiedName() + "." + method.getName() + "." + sce.name; } else sce.name = "?." + method.getName() + "." + sce.name; } else sce.name = "?." + sce.name; } } else { //If binding fails, mark the qualification as "?" to show it could //not be determined. sce.name = "?." + sce.name; } }
@Override public final boolean visit(FieldAccess node) { IVariableBinding fieldBinding = node.resolveFieldBinding(); if (fieldBinding != null) { handleTypeBinding(node, fieldBinding.getDeclaringClass(), false); handleFieldBinding(node, fieldBinding); } return true; }
@Override public boolean visit(SuperFieldAccess node) { IVariableBinding fieldBinding = node.resolveFieldBinding(); if (fieldBinding != null) { handleTypeBinding(node, fieldBinding.getDeclaringClass(), false); handleFieldBinding(node, fieldBinding); } return true; }
private boolean visitNameNode(Name node) { IBinding binding = node.resolveBinding(); if (binding instanceof IVariableBinding) { IVariableBinding variableBindig = (IVariableBinding) binding; if (variableBindig.isField()) { ITypeBinding declaringClass = variableBindig.getDeclaringClass(); handleTypeBinding(node, declaringClass, false); handleFieldBinding(node, variableBindig); } else if (!variableBindig.isEnumConstant()) { handleVariableBinding(node, variableBindig); } } return true; }
private void handleVariableBinding(ASTNode node, IVariableBinding variableBindig) { if (variableBindig == null) { StructuralPropertyDescriptor locationInParent = node.getLocationInParent(); //System.out.println(locationInParent.getId() + " has no variable binding"); } else { this.onVariableAccess(node, variableBindig); } }
private void handleFieldBinding(ASTNode node, IVariableBinding variableBindig) { if (variableBindig == null) { StructuralPropertyDescriptor locationInParent = node.getLocationInParent(); //System.out.println(locationInParent.getId() + " has no field binding"); } else { ITypeBinding declaringClass = variableBindig.getDeclaringClass(); if (declaringClass != null && !this.ignoreType(declaringClass)) { this.onFieldAccess(node, variableBindig); } } }
public static String getKeyFromFieldBinding(IVariableBinding binding) { StringBuilder sb = new StringBuilder(); String className = binding.getDeclaringClass().getErasure().getQualifiedName(); sb.append(className); sb.append('.'); sb.append(binding.getName()); return sb.toString(); }
public static String getKeyFromFieldBinding(IVariableBinding binding) { StringBuilder sb = new StringBuilder(); String className = binding.getDeclaringClass().getErasure().getQualifiedName(); sb.append(className); sb.append('#'); sb.append(binding.getName()); return sb.toString(); }
private static String getQualifiedName(IVariableBinding ivb) { try { String name = ivb.getName(); return getQualifiedName(ivb.getDeclaringClass()) + "#" + name; } catch (NullPointerException e) { return ""; } }
private String makeParameterizedName(IVariableBinding ivb) { StringBuilder sb = new StringBuilder(); sb.append(getQualifiedName(ivb.getType()) + "<"); boolean comma = false; for (ITypeBinding itb : ivb.getType().getTypeArguments()) { if (comma) sb.append(","); sb.append(getQualifiedName(itb)); comma = true; } sb.append(">"); return sb.toString(); }