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))); }
private List<Statement> getFieldAdjustments(TypeDeclaration node) { List<Statement> adjustments = Lists.newArrayList(); for (VariableDeclarationFragment decl : TreeUtil.getAllFields(node)) { IVariableBinding var = decl.getVariableBinding(); if (BindingUtil.isStatic(var) || var.getType().isPrimitive()) { continue; } boolean isWeak = BindingUtil.isWeakReference(var); boolean isVolatile = BindingUtil.isVolatile(var); if (isWeak && !isVolatile) { adjustments.add(createReleaseStatement(var)); } else if (!isWeak && isVolatile) { adjustments.add(createVolatileRetainStatement(var)); } } return adjustments; }
// Returns whether the property is a strong reference. private boolean isStrongReferenceProperty(VariableDeclarationFragment property) { IVariableBinding varBinding = Types.getVariableBinding(property); ITypeBinding type = Types.getTypeBinding(property); return !type.isPrimitive() && !BindingUtil.isWeakReference(varBinding); }