use of com.google.devtools.j2objc.ast.TypeDeclaration in project j2objc by google.
the class OuterReferenceResolverTest method testCapturedLocalVariable.
public void testCapturedLocalVariable() {
resolveSource("Test", "class Test { void test(final int i) { Runnable r = new Runnable() { " + "public void run() { int i2 = i + 1; } }; } }");
TypeDeclaration runnableNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(1);
assertTrue(ElementUtil.isAnonymous(runnableNode.getTypeElement()));
assertFalse(captureInfo.needsOuterReference(runnableNode.getTypeElement()));
List<VariableElement> innerFields = Lists.newArrayList(captureInfo.getCaptureFields(runnableNode.getTypeElement()));
assertEquals(1, innerFields.size());
assertEquals("val$i", ElementUtil.getName(innerFields.get(0)));
ClassInstanceCreation creationNode = (ClassInstanceCreation) nodesByType.get(Kind.CLASS_INSTANCE_CREATION).get(0);
List<Expression> captureArgs = creationNode.getCaptureArgs();
assertEquals(1, captureArgs.size());
Expression captureArg = captureArgs.get(0);
assertTrue(captureArg instanceof SimpleName);
VariableElement captureVar = TreeUtil.getVariableElement(captureArg);
assertNotNull(captureVar);
assertEquals("i", ElementUtil.getName(captureVar));
InfixExpression addition = (InfixExpression) nodesByType.get(Kind.INFIX_EXPRESSION).get(0);
Expression iNode = addition.getOperands().get(0);
assertTrue(iNode instanceof SimpleName);
VariableElement iVar = TreeUtil.getVariableElement(iNode);
assertNotNull(iVar);
assertEquals("val$i", ElementUtil.getName(iVar));
}
use of com.google.devtools.j2objc.ast.TypeDeclaration in project j2objc by google.
the class OuterReferenceResolverTest method testNoOuterFieldWhenSuperConstructorIsQualified.
public void testNoOuterFieldWhenSuperConstructorIsQualified() {
resolveSource("Test", "class Test { class B { class Inner {} } class A { class Inner extends B.Inner { " + " Inner(B b) { b.super(); } } } }");
List<TreeNode> typeNodes = nodesByType.get(Kind.TYPE_DECLARATION);
assertEquals(5, typeNodes.size());
for (TreeNode typeNode : typeNodes) {
assertNull(captureInfo.getOuterField(((TypeDeclaration) typeNode).getTypeElement()));
}
}
use of com.google.devtools.j2objc.ast.TypeDeclaration in project j2objc by google.
the class OuterReferenceResolverTest method testAnonymousClassInheritsLocalClassInStaticMethod.
public void testAnonymousClassInheritsLocalClassInStaticMethod() {
resolveSource("Test", "class Test { static void test() { class LocalClass {}; new LocalClass() {}; } }");
TypeDeclaration decl = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(2);
assertTrue(ElementUtil.isAnonymous(decl.getTypeElement()));
assertFalse(captureInfo.needsOuterParam(decl.getTypeElement()));
}
use of com.google.devtools.j2objc.ast.TypeDeclaration in project j2objc by google.
the class OuterReferenceResolverTest method testOuterVarAccess.
public void testOuterVarAccess() {
resolveSource("Test", "class Test { int i; class Inner { void test() { i++; } } }");
TypeDeclaration innerNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(1);
assertTrue(captureInfo.needsOuterReference(innerNode.getTypeElement()));
PostfixExpression increment = (PostfixExpression) nodesByType.get(Kind.POSTFIX_EXPRESSION).get(0);
Expression iNode = increment.getOperand();
assertTrue(iNode instanceof QualifiedName);
VariableElement outerVar = TreeUtil.getVariableElement(((QualifiedName) iNode).getQualifier());
assertNotNull(outerVar);
assertEquals("Test", outerVar.asType().toString());
}
use of com.google.devtools.j2objc.ast.TypeDeclaration in project j2objc by google.
the class SwitchRewriterTest method testVariableDeclarationsInSwitchStatement2.
public void testVariableDeclarationsInSwitchStatement2() throws IOException {
CompilationUnit unit = translateType("A", "public class A { public void doSomething(int i) { switch (i) { " + "case 1: int j = i * 2; log(j); break; " + "case 2: log(i); break; " + "case 3: log(i); int k = i, l = 42; break; }}" + "private void log(int i) {}}");
TypeDeclaration testType = (TypeDeclaration) unit.getTypes().get(0);
// First MethodDeclaration is the implicit default constructor.
MethodDeclaration method = TreeUtil.getMethodDeclarationsList(testType).get(1);
List<Statement> stmts = method.getBody().getStatements();
assertEquals(1, stmts.size());
Block block = (Block) stmts.get(0);
stmts = block.getStatements();
if (options.isJDT()) {
assertEquals(3, stmts.size());
assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(2) instanceof SwitchStatement);
} else {
assertEquals(4, stmts.size());
assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(2) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(3) instanceof SwitchStatement);
}
}
Aggregations