public void testOuterVarAccess() { resolveSource("Test", "class Test { int i; class Inner { void test() { i++; } } }"); TypeDeclaration innerNode = (TypeDeclaration) nodesByType.get(ASTNode.TYPE_DECLARATION).get(1); assertTrue(OuterReferenceResolver.needsOuterReference(innerNode.resolveBinding())); PostfixExpression increment = (PostfixExpression) nodesByType.get(ASTNode.POSTFIX_EXPRESSION).get(0); List<IVariableBinding> path = OuterReferenceResolver.getPath(increment.getOperand()); assertNotNull(path); assertEquals(2, path.size()); assertEquals("Test", path.get(0).getType().getName()); }
/* * Must be one of: * <ul> * <li>[indexBinding]++</li> * <li>++[indexBinding]</li> * <li>[indexBinding]+= 1</li> * <li>[indexBinding]= [indexBinding] + 1</li> * <li>[indexBinding]= 1 + [indexBinding]</li> * <ul> */ private boolean validateUpdaters(ForStatement statement) { List<Expression> updaters = statement.updaters(); if (updaters.size() != 1) return false; Expression updater = updaters.get(0); if (updater instanceof PostfixExpression) { PostfixExpression postfix = (PostfixExpression) updater; if (!PostfixExpression.Operator.INCREMENT.equals(postfix.getOperator())) return false; IBinding binding = getBinding(postfix.getOperand()); if (!fIndexBinding.equals(binding)) return false; return true; } else if (updater instanceof PrefixExpression) { PrefixExpression prefix = (PrefixExpression) updater; if (!PrefixExpression.Operator.INCREMENT.equals(prefix.getOperator())) return false; IBinding binding = getBinding(prefix.getOperand()); if (!fIndexBinding.equals(binding)) return false; return true; } else if (updater instanceof Assignment) { Assignment assignment = (Assignment) updater; Expression left = assignment.getLeftHandSide(); IBinding binding = getBinding(left); if (!fIndexBinding.equals(binding)) return false; if (Assignment.Operator.PLUS_ASSIGN.equals(assignment.getOperator())) { return isOneLiteral(assignment.getRightHandSide()); } else if (Assignment.Operator.ASSIGN.equals(assignment.getOperator())) { Expression right = assignment.getRightHandSide(); if (!(right instanceof InfixExpression)) return false; InfixExpression infixExpression = (InfixExpression) right; Expression leftOperand = infixExpression.getLeftOperand(); IBinding leftBinding = getBinding(leftOperand); Expression rightOperand = infixExpression.getRightOperand(); IBinding rightBinding = getBinding(rightOperand); if (fIndexBinding.equals(leftBinding)) { return isOneLiteral(rightOperand); } else if (fIndexBinding.equals(rightBinding)) { return isOneLiteral(leftOperand); } } } return false; }
/* * @see ASTVisitor#visit(PostfixExpression) */ public boolean visit(PostfixExpression node) { node.getOperand().accept(this); this.fBuffer.append(node.getOperator().toString()); return false; }