private void handleSimpleName(SimpleName node) { ASTNode firstExpression = node.getParent(); if (firstExpression instanceof FieldAccess) { while (firstExpression instanceof FieldAccess) { firstExpression = ((FieldAccess) firstExpression).getExpression(); } if (!(firstExpression instanceof SimpleName)) return; node = (SimpleName) firstExpression; } else if (firstExpression instanceof SuperFieldAccess) return; StructuralPropertyDescriptor parentDescription = node.getLocationInParent(); if (parentDescription == VariableDeclarationFragment.NAME_PROPERTY || parentDescription == SwitchCase.EXPRESSION_PROPERTY) return; IBinding binding = node.resolveBinding(); if (!(binding instanceof IVariableBinding)) return; handleVariable(node, (IVariableBinding) binding); }
/** * 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; } }
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; }
/** * 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; }
public static IBinding resolveBinding(Expression expression) { if (expression instanceof Name) return ((Name) expression).resolveBinding(); if (expression instanceof ParenthesizedExpression) return resolveBinding(((ParenthesizedExpression) expression).getExpression()); else if (expression instanceof Assignment) return resolveBinding(((Assignment) expression).getLeftHandSide()); // TODO ??? else if (expression instanceof MethodInvocation) return ((MethodInvocation) expression).resolveMethodBinding(); else if (expression instanceof SuperMethodInvocation) return ((SuperMethodInvocation) expression).resolveMethodBinding(); else if (expression instanceof FieldAccess) return ((FieldAccess) expression).resolveFieldBinding(); else if (expression instanceof SuperFieldAccess) return ((SuperFieldAccess) expression).resolveFieldBinding(); else if (expression instanceof ConditionalExpression) return resolveBinding(((ConditionalExpression) expression).getThenExpression()); 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); }
private Expression generateQualifier(String paramName, AST ast, boolean useSuper, Expression qualifier) { SimpleName paramSimpleName= ast.newSimpleName(paramName); if (useSuper) { SuperFieldAccess sf= ast.newSuperFieldAccess(); sf.setName(paramSimpleName); if (qualifier instanceof Name) { sf.setQualifier((Name) qualifier); } return sf; } if (qualifier != null) { FieldAccess parameterAccess= ast.newFieldAccess(); parameterAccess.setExpression(qualifier); parameterAccess.setName(paramSimpleName); return parameterAccess; } return paramSimpleName; }
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; }
public static IBinding resolveBinding(Expression expression){ if (expression instanceof Name) return ((Name)expression).resolveBinding(); if (expression instanceof ParenthesizedExpression) return resolveBinding(((ParenthesizedExpression)expression).getExpression()); else if (expression instanceof Assignment) return resolveBinding(((Assignment)expression).getLeftHandSide());//TODO ??? else if (expression instanceof MethodInvocation) return ((MethodInvocation)expression).resolveMethodBinding(); else if (expression instanceof SuperMethodInvocation) return ((SuperMethodInvocation)expression).resolveMethodBinding(); else if (expression instanceof FieldAccess) return ((FieldAccess)expression).resolveFieldBinding(); else if (expression instanceof SuperFieldAccess) return ((SuperFieldAccess)expression).resolveFieldBinding(); else if (expression instanceof ConditionalExpression) return resolveBinding(((ConditionalExpression)expression).getThenExpression()); return null; }
private void handleSimpleName(SimpleName node) { ASTNode firstExpression= node.getParent(); if (firstExpression instanceof FieldAccess) { while (firstExpression instanceof FieldAccess) { firstExpression= ((FieldAccess)firstExpression).getExpression(); } if (!(firstExpression instanceof SimpleName)) return; node= (SimpleName)firstExpression; } else if (firstExpression instanceof SuperFieldAccess) return; StructuralPropertyDescriptor parentDescription= node.getLocationInParent(); if (parentDescription == VariableDeclarationFragment.NAME_PROPERTY || parentDescription == SwitchCase.EXPRESSION_PROPERTY) return; IBinding binding= node.resolveBinding(); if (!(binding instanceof IVariableBinding)) return; handleVariable(node, (IVariableBinding) binding); }
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); }
/** * Always called from the statement visitor, therefore, all variables are top-level. */ public IResolvedData resolveExpression(final Expression expression, final ASTNode node, final IResolvedNode qualifierOf, final boolean isLHS, final boolean isDef) { IResolvedData rv = null; if (expression instanceof Name) { final IBinding binding = ((Name) expression).resolveBinding(); if (binding instanceof IVariableBinding) { rv = newResolvedVariable(expression, ASTTools.enclosingScope(node), qualifierOf, isLHS, false, isDef); } } else if (expression instanceof FieldAccess) { rv = newResolvedVariable(expression, ASTTools.enclosingScope(node), qualifierOf, isLHS, false, isDef); } else if (expression instanceof SuperFieldAccess) { rv = newResolvedVariable(expression, ASTTools.enclosingScope(node), qualifierOf, isLHS, false, isDef); } return rv; }
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 IResolvedNode processNode(final ASTNode node) { IResolvedData rv = null; if (node instanceof Name) { final IBinding binding = ((Name) node).resolveBinding(); if (binding instanceof IVariableBinding) { rv = factory.newResolvedVariable(node, ASTTools.enclosingScope(node), qualifierOf, isLHS, isActual, false); } } else if (node instanceof FieldAccess) { rv = factory.newResolvedVariable(node, ASTTools.enclosingScope(node), qualifierOf, isLHS, isActual, false); } else if (node instanceof SuperFieldAccess) { rv = factory.newResolvedVariable(node, ASTTools.enclosingScope(node), qualifierOf, isLHS, isActual, false); } // return the resolved node return rv; }
@Override public boolean visit(SuperFieldAccess node) { IVariableBinding fieldBinding = node.resolveFieldBinding(); if (fieldBinding != null) { handleTypeBinding(node, fieldBinding.getDeclaringClass(), false); handleFieldBinding(node, fieldBinding); } return true; }
@Override public void endVisit(SuperFieldAccess node) { if (skipNode(node)) { return; } processSequential(node, node.getQualifier(), node.getName()); }
@Override public ITypeConstraint[] create(SuperFieldAccess access) { SimpleName name = access.getName(); IBinding binding = name.resolveBinding(); if (!(binding instanceof IVariableBinding)) return new ITypeConstraint[0]; IVariableBinding vb = (IVariableBinding) binding; return createConstraintsForAccessToField(vb, null, access); }
private Expression getQualifier(ASTNode parent) { switch (parent.getNodeType()) { case ASTNode.FIELD_ACCESS: return ((FieldAccess) parent).getExpression(); case ASTNode.QUALIFIED_NAME: return ((QualifiedName)parent).getQualifier(); case ASTNode.SUPER_FIELD_ACCESS: return ((SuperFieldAccess)parent).getQualifier(); default: return null; } }
/** * Is the specified name a qualified entity, e.g. preceded by 'this', * 'super' or part of a method invocation? * * @param name * the name to check * @return <code>true</code> if this entity is qualified, * <code>false</code> otherwise */ protected static boolean isQualifiedEntity(final Name name) { Assert.isNotNull(name); final ASTNode parent= name.getParent(); if (parent instanceof QualifiedName && ((QualifiedName) parent).getName().equals(name) || parent instanceof FieldAccess && ((FieldAccess) parent).getName().equals(name) || parent instanceof SuperFieldAccess) return true; else if (parent instanceof MethodInvocation) { final MethodInvocation invocation= (MethodInvocation) parent; return invocation.getExpression() != null && invocation.getName().equals(name); } return false; }
@Override public final boolean visit(final SuperFieldAccess node) { Assert.isNotNull(node); fStatus.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.MoveInstanceMethodProcessor_uses_super, JavaStatusContext.create(fMethod.getCompilationUnit(), node))); fResult.add(node); return false; }
@Override public final void endVisit(final SuperFieldAccess node) { final Name name= node.getName(); final IBinding binding= name.resolveBinding(); if (binding instanceof IVariableBinding) endVisit((IVariableBinding) binding, null, node); }
@Override public final boolean visit(final SuperFieldAccess node) { if (!fAnonymousClassDeclaration && !fTypeDeclarationStatement) { final AST ast= node.getAST(); final FieldAccess access= ast.newFieldAccess(); access.setExpression(ast.newThisExpression()); access.setName(ast.newSimpleName(node.getName().getIdentifier())); fRewrite.replace(node, access, null); if (!fSourceRewriter.getCu().equals(fTargetRewriter.getCu())) fSourceRewriter.getImportRemover().registerRemovedNode(node); return true; } return false; }
@Override public ITypeConstraint[] create(SuperFieldAccess access){ SimpleName name= access.getName(); IBinding binding= name.resolveBinding(); if (! (binding instanceof IVariableBinding)) return new ITypeConstraint[0]; IVariableBinding vb= (IVariableBinding)binding; return createConstraintsForAccessToField(vb, null, access); }