private List<IVariableBinding> getCapturedVariables(ClassInstanceCreation node) { ITypeBinding newType = node.getTypeBinding().getTypeDeclaration(); ITypeBinding owningType = TreeUtil.getOwningType(node).getTypeBinding().getTypeDeclaration(); // Test for the recursive construction of a local class. if (owningType.isEqualTo(newType)) { return OuterReferenceResolver.getInnerFields(newType); } return OuterReferenceResolver.getCapturedVars(newType); }
public void testCapturedWeakLocalVariable() { resolveSource( "Test", "import com.google.j2objc.annotations.Weak;" + "class Test { void test(@Weak final int i) { Runnable r = new Runnable() { " + "public void run() { int i2 = i + 1; } }; } }"); AnonymousClassDeclaration runnableNode = (AnonymousClassDeclaration) nodesByType.get(ASTNode.ANONYMOUS_CLASS_DECLARATION).get(0); ITypeBinding runnableBinding = runnableNode.resolveBinding(); List<IVariableBinding> innerFields = OuterReferenceResolver.getInnerFields(runnableBinding); assertEquals(1, innerFields.size()); assertTrue(BindingUtil.isWeakReference(innerFields.get(0))); }
public void testCapturedLocalVariable() { resolveSource( "Test", "class Test { void test(final int i) { Runnable r = new Runnable() { " + "public void run() { int i2 = i + 1; } }; } }"); AnonymousClassDeclaration runnableNode = (AnonymousClassDeclaration) nodesByType.get(ASTNode.ANONYMOUS_CLASS_DECLARATION).get(0); ITypeBinding runnableBinding = runnableNode.resolveBinding(); assertFalse(OuterReferenceResolver.needsOuterReference(runnableBinding)); List<IVariableBinding> capturedVars = OuterReferenceResolver.getCapturedVars(runnableBinding); List<IVariableBinding> innerFields = OuterReferenceResolver.getInnerFields(runnableBinding); assertEquals(1, capturedVars.size()); assertEquals(1, innerFields.size()); assertEquals("i", capturedVars.get(0).getName()); assertEquals("val$i", innerFields.get(0).getName()); InfixExpression addition = (InfixExpression) nodesByType.get(ASTNode.INFIX_EXPRESSION).get(0); List<IVariableBinding> iPath = OuterReferenceResolver.getPath(addition.getLeftOperand()); assertNotNull(iPath); assertEquals(1, iPath.size()); assertEquals("val$i", iPath.get(0).getName()); }