Search in sources :

Example 6 with TypeDeclaration

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));
}
Also used : ClassInstanceCreation(com.google.devtools.j2objc.ast.ClassInstanceCreation) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) VariableElement(javax.lang.model.element.VariableElement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Example 7 with TypeDeclaration

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()));
    }
}
Also used : TreeNode(com.google.devtools.j2objc.ast.TreeNode) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Example 8 with TypeDeclaration

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()));
}
Also used : TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Example 9 with TypeDeclaration

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());
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) VariableElement(javax.lang.model.element.VariableElement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Example 10 with TypeDeclaration

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);
    }
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) Block(com.google.devtools.j2objc.ast.Block) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Aggregations

TypeDeclaration (com.google.devtools.j2objc.ast.TypeDeclaration)15 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)6 VariableElement (javax.lang.model.element.VariableElement)6 Expression (com.google.devtools.j2objc.ast.Expression)5 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)4 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)4 SimpleName (com.google.devtools.j2objc.ast.SimpleName)4 AnnotationTypeDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeDeclaration)3 TypeElement (javax.lang.model.element.TypeElement)3 ClassInstanceCreation (com.google.devtools.j2objc.ast.ClassInstanceCreation)2 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)2 Annotation (com.google.devtools.j2objc.ast.Annotation)1 Block (com.google.devtools.j2objc.ast.Block)1 EnumDeclaration (com.google.devtools.j2objc.ast.EnumDeclaration)1 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)1 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)1 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)1 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)1 PackageDeclaration (com.google.devtools.j2objc.ast.PackageDeclaration)1 QualifiedName (com.google.devtools.j2objc.ast.QualifiedName)1