Search in sources :

Example 21 with VariableDeclarationFragment

use of com.google.devtools.j2objc.ast.VariableDeclarationFragment in project j2objc by google.

the class StatementGenerator method visit.

@Override
public boolean visit(VariableDeclarationExpression node) {
    String typeString = nameTable.getObjCType(node.getTypeMirror());
    boolean needsAsterisk = typeString.endsWith("*");
    buffer.append(typeString);
    if (!needsAsterisk) {
        buffer.append(' ');
    }
    for (Iterator<VariableDeclarationFragment> it = node.getFragments().iterator(); it.hasNext(); ) {
        VariableDeclarationFragment f = it.next();
        f.accept(this);
        if (it.hasNext()) {
            buffer.append(", ");
            if (needsAsterisk) {
                buffer.append('*');
            }
        }
    }
    return false;
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment)

Example 22 with VariableDeclarationFragment

use of com.google.devtools.j2objc.ast.VariableDeclarationFragment in project j2objc by google.

the class UnsequencedExpressionRewriter method extractVariableDeclarationFragments.

private void extractVariableDeclarationFragments(List<VariableDeclarationFragment> fragments, List<Statement> stmtList) {
    for (int i = 0; i < fragments.size(); i++) {
        VariableDeclarationFragment frag = fragments.get(i);
        Expression init = frag.getInitializer();
        if (init == null) {
            continue;
        }
        newExpression(init);
        init.accept(this);
        List<VariableAccess> toExtract = getUnsequencedAccesses();
        if (!toExtract.isEmpty()) {
            if (i > 0) {
                // Extract all fragments before the current one to preserve ordering.
                VariableDeclarationStatement newDecl = new VariableDeclarationStatement(fragments.get(0).copy());
                for (int j = 1; j < i; j++) {
                    newDecl.addFragment(fragments.get(j).copy());
                }
                stmtList.add(newDecl);
                fragments.subList(0, i).clear();
            }
            extractOrderedAccesses(stmtList, currentTopNode, toExtract);
            i = 0;
        }
    }
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement)

Example 23 with VariableDeclarationFragment

use of com.google.devtools.j2objc.ast.VariableDeclarationFragment in project j2objc by google.

the class AnonymousClassConverterTest method testMethodVarInSwitch.

public void testMethodVarInSwitch() throws IOException {
    String source = "class Test { " + "  enum E { ONE, TWO };" + "  void foo(E e) { " + "    switch (e) {" + "      case ONE: {" + "        final Integer i = 1;" + "        Runnable r = new Runnable() { " + "          public void run() { int j = i + 1; } }; }}}}";
    // Verify method var in r1.run() isn't mistakenly made a field in r1.
    CompilationUnit unit = translateType("Test", source);
    NameTable nameTable = unit.getEnv().nameTable();
    List<AbstractTypeDeclaration> types = unit.getTypes();
    AbstractTypeDeclaration r1 = types.get(2);
    assertEquals("Test_1", nameTable.getFullName(r1.getTypeElement()));
    boolean found = false;
    for (VariableDeclarationFragment var : TreeUtil.getAllFields(r1)) {
        if (ElementUtil.getName(var.getVariableElement()).equals("val$i")) {
            found = true;
        }
    }
    assertTrue("required field not found", found);
    // Verify method var is passed to constructor.
    String translation = generateFromUnit(unit, "Test.m");
    assertTranslation(translation, "r = create_Test_1_initWithJavaLangInteger_(i)");
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) NameTable(com.google.devtools.j2objc.util.NameTable) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 24 with VariableDeclarationFragment

use of com.google.devtools.j2objc.ast.VariableDeclarationFragment in project j2objc by google.

the class AnonymousClassConverterTest method testMethodVarInNestedAnonymousClass.

public void testMethodVarInNestedAnonymousClass() throws IOException {
    String source = "class Test { " + "  void bar() { " + "    Runnable r1 = new Runnable() { " + "      public void run() { " + "        final Integer i = 1; " + "        Runnable r2 = new Runnable() { " + "          public void run() { int j = i + 1; } }; } }; } }";
    // Verify method var in r1.run() isn't mistakenly made a field in r1.
    CompilationUnit unit = translateType("Test", source);
    NameTable nameTable = unit.getEnv().nameTable();
    List<AbstractTypeDeclaration> types = unit.getTypes();
    AbstractTypeDeclaration r1 = types.get(1);
    assertEquals("Test_1", nameTable.getFullName(r1.getTypeElement()));
    for (VariableDeclarationFragment var : TreeUtil.getAllFields(r1)) {
        if (ElementUtil.getName(var.getVariableElement()).equals("val$i")) {
            fail("found field that shouldn't be declared");
        }
    }
    // Method var in r1.run() becomes a field in r2.
    AbstractTypeDeclaration r2 = types.get(2);
    assertEquals("Test_1_1", nameTable.getFullName(r2.getTypeElement()));
    boolean found = false;
    for (VariableDeclarationFragment var : TreeUtil.getAllFields(r2)) {
        if (ElementUtil.getName(var.getVariableElement()).equals("val$i")) {
            found = true;
        }
    }
    assertTrue("required field not found", found);
    // Verify constructor takes both outer field and var.
    String translation = generateFromUnit(unit, "Test.m");
    assertTranslation(translation, "r2 = create_Test_1_1_initWithJavaLangInteger_(i)");
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) NameTable(com.google.devtools.j2objc.util.NameTable) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Aggregations

VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)24 VariableElement (javax.lang.model.element.VariableElement)11 FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)6 Statement (com.google.devtools.j2objc.ast.Statement)5 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)5 TypeMirror (javax.lang.model.type.TypeMirror)5 Block (com.google.devtools.j2objc.ast.Block)4 Expression (com.google.devtools.j2objc.ast.Expression)4 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)4 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)3 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)3 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)3 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)3 ForStatement (com.google.devtools.j2objc.ast.ForStatement)3 NameTable (com.google.devtools.j2objc.util.NameTable)3 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 EnumDeclaration (com.google.devtools.j2objc.ast.EnumDeclaration)2 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)2 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)2