private static boolean needsOuterParantheses(ASTNode nodeToCast) { ASTNode parent = nodeToCast.getParent(); if (parent instanceof MethodInvocation) { if (((MethodInvocation) parent).getExpression() == nodeToCast) { return true; } } else if (parent instanceof QualifiedName) { if (((QualifiedName) parent).getQualifier() == nodeToCast) { return true; } } else if (parent instanceof FieldAccess) { if (((FieldAccess) parent).getExpression() == nodeToCast) { return true; } } return false; }
public static SimpleName getLeftMostSimpleName(Name name) { if (name instanceof SimpleName) { return (SimpleName)name; } else { final SimpleName[] result= new SimpleName[1]; ASTVisitor visitor= new ASTVisitor() { @Override public boolean visit(QualifiedName qualifiedName) { Name left= qualifiedName.getQualifier(); if (left instanceof SimpleName) { result[0]= (SimpleName)left; } else { left.accept(this); } return false; } }; name.accept(visitor); return result[0]; } }
public boolean visit(QualifiedName node) { if (mtbStack.isEmpty() && !itbStack.isEmpty()) { // not part of a method return false; } else if (!mtbStack.isEmpty()) { if (node.getName().getIdentifier().equals("length")) { return true; } try { return visitName(node.resolveBinding(), mtbStack.peek()); } catch (Exception e) { System.err.println("Cannot resolve qualified name \"" + node.getFullyQualifiedName().toString() + "\""); return false; } } return false; }
/** * 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 {@link Name} or {@link Type} nodes, returns the topmost {@link Type} node * that shares the same type binding as the given node. * * @param node an ASTNode * @return the normalized {@link Type} node or the original node */ public static ASTNode getNormalizedNode(ASTNode node) { ASTNode current= node; // normalize name if (QualifiedName.NAME_PROPERTY.equals(current.getLocationInParent())) { current= current.getParent(); } // normalize type if (QualifiedType.NAME_PROPERTY.equals(current.getLocationInParent()) || SimpleType.NAME_PROPERTY.equals(current.getLocationInParent()) || NameQualifiedType.NAME_PROPERTY.equals(current.getLocationInParent())) { current= current.getParent(); } // normalize parameterized types if (ParameterizedType.TYPE_PROPERTY.equals(current.getLocationInParent())) { current= current.getParent(); } return current; }
static boolean isLeftHandSideOfAssignment(ASTNode node) { Assignment assignment = (Assignment) ASTNodes.getParent(node, ASTNode.ASSIGNMENT); if (assignment != null) { Expression leftHandSide = assignment.getLeftHandSide(); if (leftHandSide == node) { return true; } if (ASTNodes.isParent(node, leftHandSide)) { switch (leftHandSide.getNodeType()) { case ASTNode.SIMPLE_NAME: return true; case ASTNode.FIELD_ACCESS: return node == ((FieldAccess) leftHandSide).getName(); case ASTNode.QUALIFIED_NAME: return node == ((QualifiedName) leftHandSide).getName(); case ASTNode.SUPER_FIELD_ACCESS: return node == ((SuperFieldAccess) leftHandSide).getName(); default: return false; } } } return false; }
private static boolean needsOuterParantheses(ASTNode nodeToCast) { ASTNode parent= nodeToCast.getParent(); if (parent instanceof MethodInvocation) { if (((MethodInvocation)parent).getExpression() == nodeToCast) { return true; } } else if (parent instanceof QualifiedName) { if (((QualifiedName)parent).getQualifier() == nodeToCast) { return true; } } else if (parent instanceof FieldAccess) { if (((FieldAccess)parent).getExpression() == nodeToCast) { return true; } } return false; }
private boolean hasImportDeclaration(CompilationUnit unit) { List imports = unit.imports(); for(Iterator iter = imports.iterator(); iter.hasNext() ; ) { Object next = iter.next(); if (next instanceof ImportDeclaration ) { ImportDeclaration importDecl = (ImportDeclaration)next; Name name = importDecl.getName(); if (name instanceof QualifiedName ) { QualifiedName qName = (QualifiedName)name; String qNameString = qName.getFullyQualifiedName(); if (qNameString.startsWith("edu.cmu.cs.aliasjava.annotations") ) { return true; } } } } return false; }
public boolean visit(QualifiedName node) { if ((this.mtbStack.isEmpty()) && (!this.itbStack.isEmpty())) { return false; } if (!this.mtbStack.isEmpty()) { if (node.getName().getIdentifier().equals("length")) { return true; } try { return visitName(node.resolveBinding(), (IMethodBinding)this.mtbStack.peek()); } catch (Exception e) { System.err.println("Cannot resolve qualified name \"" + node.getFullyQualifiedName().toString() + "\""); return false; } } return false; }
public boolean visit(QualifiedName node) { if ((this.mtbStack.isEmpty()) && (!this.itbStack.isEmpty())) { return false; } if (!this.mtbStack.isEmpty()) { if (node.getName().getIdentifier().equals("length")) { return true; } try { return visitName(node.resolveBinding(), (IMethodBinding)this.mtbStack.peek()); } catch (Exception localException) { System.err.println("Cannot resolve qualified name \"" + node.getFullyQualifiedName().toString() + "\""); return false; } } return false; }
private RefactoringStatus rewriteReference(ASTRewrite astRewrite, ImportRewrite importRewrite, SimpleName name, String fullyQualifiedTypeName) { final RefactoringStatus status = new RefactoringStatus(); // get the node to replace. final Name nodeToReplace = Util.getTopmostName(name); // If its in a case statement. if (Util.isContainedInCaseLabel(name)) { if (!nodeToReplace.isSimpleName()) // Need to remove prefix. astRewrite.replace(nodeToReplace, name, null); return status; } // Make a copy of the simple name. final AST ast = name.getAST(); final SimpleName nameCopy = (SimpleName) ASTNode.copySubtree(ast, name); final String typeName = importRewrite.addImport(fullyQualifiedTypeName); final QualifiedName newNameNode = ast.newQualifiedName( ast.newName(typeName), nameCopy); astRewrite.replace(nodeToReplace, newNameNode, null); return status; }
/** * 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; } }
private static boolean isLeftHandSideOfAssignment(ASTNode node) { Assignment assignment = (Assignment) ASTNodes.getParent(node, ASTNode.ASSIGNMENT); if (assignment != null) { Expression leftHandSide = assignment.getLeftHandSide(); if (leftHandSide == node) { return true; } if (ASTNodes.isParent(node, leftHandSide)) { switch (leftHandSide.getNodeType()) { case ASTNode.SIMPLE_NAME: return true; case ASTNode.FIELD_ACCESS: return node == ((FieldAccess) leftHandSide).getName(); case ASTNode.QUALIFIED_NAME: return node == ((QualifiedName) leftHandSide).getName(); case ASTNode.SUPER_FIELD_ACCESS: return node == ((SuperFieldAccess) leftHandSide).getName(); default: return false; } } } return false; }
private static SimpleName findProblemFieldName(ASTNode selectedNode, int problemID) { // if a field access occurs in an compatibility situation (assignment/return/argument) // with expected type declared @NonNull we first need to find the SimpleName inside: if (selectedNode instanceof FieldAccess) selectedNode = ((FieldAccess) selectedNode).getName(); else if (selectedNode instanceof QualifiedName) selectedNode = ((QualifiedName) selectedNode).getName(); if (selectedNode instanceof SimpleName) { SimpleName name = (SimpleName) selectedNode; if (problemID == IProblem.NullableFieldReference) return name; // not field dereference, but compatibility issue - is value a field reference? IBinding binding = name.resolveBinding(); if ((binding instanceof IVariableBinding) && ((IVariableBinding) binding).isField()) return name; } return null; }
private static ChangeCorrectionProposal getProposal( ICompilationUnit cu, SimpleName sn, IVariableBinding variableBinding, int relevance) { Expression accessNode = sn; Expression qualifier = null; boolean useSuper = false; ASTNode parent = sn.getParent(); switch (parent.getNodeType()) { case ASTNode.QUALIFIED_NAME: accessNode = (Expression) parent; qualifier = ((QualifiedName) parent).getQualifier(); break; case ASTNode.SUPER_FIELD_ACCESS: accessNode = (Expression) parent; qualifier = ((SuperFieldAccess) parent).getQualifier(); useSuper = true; break; } ASTRewrite rewrite = ASTRewrite.create(sn.getAST()); ProposalParameter gspc = new ProposalParameter(useSuper, cu, rewrite, accessNode, qualifier, variableBinding); if (ASTResolving.isWriteAccess(sn)) return addSetterProposal(gspc, relevance); else return addGetterProposal(gspc, relevance); }
/** * For {@link Name} or {@link org.eclipse.jdt.core.dom.Type} nodes, returns the topmost {@link * org.eclipse.jdt.core.dom.Type} node that shares the same type binding as the given node. * * @param node an ASTNode * @return the normalized {@link org.eclipse.jdt.core.dom.Type} node or the original node */ public static ASTNode getNormalizedNode(ASTNode node) { ASTNode current = node; // normalize name if (QualifiedName.NAME_PROPERTY.equals(current.getLocationInParent())) { current = current.getParent(); } // normalize type if (QualifiedType.NAME_PROPERTY.equals(current.getLocationInParent()) || SimpleType.NAME_PROPERTY.equals(current.getLocationInParent()) || NameQualifiedType.NAME_PROPERTY.equals(current.getLocationInParent())) { current = current.getParent(); } // normalize parameterized types if (ParameterizedType.TYPE_PROPERTY.equals(current.getLocationInParent())) { current = current.getParent(); } return current; }
/** * Add the variable to the node aliases. */ public boolean visit(QualifiedName node){ /* All we really need from this is the variable binding. */ IBinding binding = node.resolveBinding(); if(binding == null){ if(this.aliases.contains(node.getFullyQualifiedName())){ this.result = true; return false; } } else if(binding instanceof IVariableBinding){ if(this.aliases.contains(binding.getKey())){ this.result = true; return false; } } return true; }
/** * Add the variable to the node aliases. */ public boolean visit(QualifiedName node){ /* All we really need from this is the variable binding. */ IBinding binding = node.resolveBinding(); if(binding == null){ if(!seedVariables.contains(node.getFullyQualifiedName())) seedVariables.add(node.getFullyQualifiedName()); } else if(binding instanceof IVariableBinding){ if(!seedVariables.contains(binding.getKey())) seedVariables.add(binding.getKey()); } return false; }
@Override public boolean visit(QualifiedName node) { if (isMovedMember(node.resolveBinding())) { if (node.getParent() instanceof ImportDeclaration) { ITypeBinding typeBinding= node.resolveTypeBinding(); if (typeBinding != null) fCuRewrite.getImportRewrite().removeImport(typeBinding.getQualifiedName()); String imp= fCuRewrite.getImportRewrite().addImport(fTarget.getQualifiedName() + '.' + node.getName().getIdentifier()); fCuRewrite.getImportRemover().registerAddedImport(imp); } else { rewrite(node, fTarget); } return false; } else { return super.visit(node); } }
@Override public final void endVisit(final QualifiedName node) { final ASTNode parent= node.getParent(); final Name qualifier= node.getQualifier(); IBinding binding= qualifier.resolveBinding(); if (binding instanceof ITypeBinding) { final ConstraintVariable2 variable= fModel.createTypeVariable((ITypeBinding) binding, new CompilationUnitRange(RefactoringASTParser.getCompilationUnit(node), new SourceRange(qualifier.getStartPosition(), qualifier.getLength()))); if (variable != null) qualifier.setProperty(PROPERTY_CONSTRAINT_VARIABLE, variable); } binding= node.getName().resolveBinding(); if (binding instanceof IVariableBinding && !(parent instanceof ImportDeclaration)) endVisit((IVariableBinding) binding, qualifier, node); else if (binding instanceof ITypeBinding && parent instanceof MethodDeclaration) endVisit((ITypeBinding) binding, node); }
@Override public final boolean visit(final QualifiedName node) { Assert.isNotNull(node); IBinding binding= node.resolveBinding(); if (binding instanceof ITypeBinding) { final ITypeBinding type= (ITypeBinding) binding; if (type.isClass() && type.getDeclaringClass() != null) { final Type newType= fTargetRewrite.getImportRewrite().addImport(type, node.getAST()); fRewrite.replace(node, newType, null); return false; } } binding= node.getQualifier().resolveBinding(); if (Bindings.equals(fTarget, binding)) { fRewrite.replace(node, getFieldReference(node.getName(), fRewrite), null); return false; } node.getQualifier().accept(this); return false; }
@Override public boolean visit(ClassInstanceCreation node) { // match with the constructor and the type. Type type= node.getType(); if (type instanceof ParameterizedType) { type= ((ParameterizedType) type).getType(); } if (type instanceof SimpleType) { Name name= ((SimpleType) type).getName(); if (name instanceof QualifiedName) name= ((QualifiedName)name).getName(); addUsage(name, node.resolveConstructorBinding()); } return super.visit(node); }
/** * {@inheritDoc} */ @Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException { ASTRewrite rewrite= cuRewrite.getASTRewrite(); CompilationUnit compilationUnit= cuRewrite.getRoot(); importType(fDeclaringClass, fName, cuRewrite.getImportRewrite(), compilationUnit); TextEditGroup group; if (fName.resolveBinding() instanceof IMethodBinding) { group= createTextEditGroup(FixMessages.CodeStyleFix_QualifyMethodWithDeclClass_description, cuRewrite); } else { group= createTextEditGroup(FixMessages.CodeStyleFix_QualifyFieldWithDeclClass_description, cuRewrite); } IJavaElement javaElement= fDeclaringClass.getJavaElement(); if (javaElement instanceof IType) { Name qualifierName= compilationUnit.getAST().newName(((IType)javaElement).getElementName()); SimpleName simpleName= (SimpleName)rewrite.createMoveTarget(fName); QualifiedName qualifiedName= compilationUnit.getAST().newQualifiedName(qualifierName, simpleName); rewrite.replace(fName, qualifiedName, group); } }
/** * Is the specified name a target access? * * @param name * the name to check * @return <code>true</code> if this name is a target access, * <code>false</code> otherwise */ protected boolean isTargetAccess(final Name name) { Assert.isNotNull(name); final IBinding binding= name.resolveBinding(); if (Bindings.equals(fTarget, binding)) return true; if (name.getParent() instanceof FieldAccess) { final FieldAccess access= (FieldAccess) name.getParent(); final Expression expression= access.getExpression(); if (expression instanceof Name) return isTargetAccess((Name) expression); } else if (name instanceof QualifiedName) { final QualifiedName qualified= (QualifiedName) name; if (qualified.getQualifier() != null) return isTargetAccess(qualified.getQualifier()); } return false; }
private void updateSimpleName(ASTRewrite rewriter, ParameterInfo pi, SimpleName node, List<SingleVariableDeclaration> enclosingParameters, IJavaProject project) { AST ast= rewriter.getAST(); IBinding binding= node.resolveBinding(); Expression replacementNode= fParameterObjectFactory.createFieldReadAccess(pi, getParameterName(), ast, project, false, null); if (binding instanceof IVariableBinding) { IVariableBinding variable= (IVariableBinding) binding; if (variable.isParameter() && variable.getName().equals(getNameInScope(pi, enclosingParameters))) { rewriter.replace(node, replacementNode, null); } } else { ASTNode parent= node.getParent(); if (!(parent instanceof QualifiedName || parent instanceof FieldAccess || parent instanceof SuperFieldAccess)) { if (node.getIdentifier().equals(getNameInScope(pi, enclosingParameters))) { rewriter.replace(node, replacementNode, null); } } } }
private static boolean isLeftHandSideOfAssignment(ASTNode node) { Assignment assignment= (Assignment)ASTNodes.getParent(node, ASTNode.ASSIGNMENT); if (assignment != null) { Expression leftHandSide= assignment.getLeftHandSide(); if (leftHandSide == node) { return true; } if (ASTNodes.isParent(node, leftHandSide)) { switch (leftHandSide.getNodeType()) { case ASTNode.SIMPLE_NAME: return true; case ASTNode.FIELD_ACCESS: return node == ((FieldAccess)leftHandSide).getName(); case ASTNode.QUALIFIED_NAME: return node == ((QualifiedName)leftHandSide).getName(); case ASTNode.SUPER_FIELD_ACCESS: return node == ((SuperFieldAccess)leftHandSide).getName(); default: return false; } } } return false; }
private RefactoringStatus checkExpression() throws JavaModelException { Expression selectedExpression= getSelectedExpression().getAssociatedExpression(); if (selectedExpression != null) { final ASTNode parent= selectedExpression.getParent(); if (selectedExpression instanceof NullLiteral) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_null_literals); } else if (selectedExpression instanceof ArrayInitializer) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_array_initializer); } else if (selectedExpression instanceof Assignment) { if (parent instanceof Expression && !(parent instanceof ParenthesizedExpression)) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_assignment); else return null; } else if (selectedExpression instanceof SimpleName) { if ((((SimpleName) selectedExpression)).isDeclaration()) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_names_in_declarations); if (parent instanceof QualifiedName && selectedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || parent instanceof FieldAccess && selectedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_select_expression); } else if (selectedExpression instanceof VariableDeclarationExpression && parent instanceof TryStatement) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_resource_in_try_with_resources); } } return null; }
private RefactoringStatus checkExpression() throws JavaModelException { RefactoringStatus result= new RefactoringStatus(); result.merge(checkExpressionBinding()); if(result.hasFatalError()) return result; checkAllStaticFinal(); IExpressionFragment selectedExpression= getSelectedExpression(); Expression associatedExpression= selectedExpression.getAssociatedExpression(); if (associatedExpression instanceof NullLiteral) result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_null_literals)); else if (!ConstantChecks.isLoadTimeConstant(selectedExpression)) result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_not_load_time_constant)); else if (associatedExpression instanceof SimpleName) { if (associatedExpression.getParent() instanceof QualifiedName && associatedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || associatedExpression.getParent() instanceof FieldAccess && associatedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_select_expression); } return result; }
private Expression getQualifier(final Expression exp) { if (exp instanceof FieldAccess) { return ((FieldAccess) exp).getExpression(); } if (exp instanceof SuperFieldAccess) { return ((SuperFieldAccess) exp).getQualifier(); } if (exp instanceof MethodInvocation) { return ((MethodInvocation) exp).getExpression(); } if (exp instanceof QualifiedName) { return ((QualifiedName) exp).getQualifier(); } return null; }
private static ITypeBinding extractExpressionType(SimpleType variableIdentifier) { ScopeAnalyzer analyzer= new ScopeAnalyzer((CompilationUnit) variableIdentifier.getRoot()); // get the name of the variable to search the type for Name name= null; if (variableIdentifier.getName() instanceof SimpleName) { name= variableIdentifier.getName(); } else if (variableIdentifier.getName() instanceof QualifiedName) { name= ((QualifiedName) variableIdentifier.getName()).getName(); } // analyze variables which are available in the scope at the position of the quick assist invokation IBinding[] declarationsInScope= analyzer.getDeclarationsInScope(name.getStartPosition(), ScopeAnalyzer.VARIABLES); for (int i= 0; i < declarationsInScope.length; i++) { IBinding currentVariable= declarationsInScope[i]; if (((IVariableBinding) currentVariable).getName().equals(name.getFullyQualifiedName())) { return ((IVariableBinding) currentVariable).getType(); } } return null; }
private int getParameterIndex(final Expression expression) { if (expression instanceof SimpleName) { final SimpleName simpleName = (SimpleName)expression; if (simpleName.resolveBinding() instanceof IVariableBinding) { final VariableInfo var = new VariableInfo((IVariableBinding)simpleName.resolveBinding()); if (MethodAnalyzer.this.parameters.contains(var)) { return MethodAnalyzer.this.parameters.indexOf(var); } } } if (!(expression instanceof QualifiedName)) { return -1; } final QualifiedName qualifiedName = (QualifiedName)expression; final int index = this.getParameterIndex((Expression)qualifiedName.getName()); if (index != -1) { return index; } return this.getParameterIndex((Expression)qualifiedName.getQualifier()); }