Search in sources :

Example 31 with CompilationUnit

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

the class TreeConverter method convertCompilationUnit.

public static CompilationUnit convertCompilationUnit(Options options, JavacEnvironment env, JCTree.JCCompilationUnit javacUnit) {
    String sourceFilePath = getPath(javacUnit.getSourceFile());
    try {
        TreeConverter converter = new TreeConverter(javacUnit, env);
        JavaFileObject sourceFile = javacUnit.getSourceFile();
        String source = sourceFile.getCharContent(false).toString();
        String mainTypeName = FileUtil.getMainTypeName(sourceFile);
        converter.newUnit = new CompilationUnit(new TranslationEnvironment(options, env), sourceFilePath, mainTypeName, source);
        PackageElement pkg = javacUnit.packge != null ? javacUnit.packge : env.defaultPackage();
        converter.newUnit.setPackage(converter.convertPackage(pkg, Trees.instance(env.task())));
        for (JCTree type : javacUnit.getTypeDecls()) {
            TreeNode newNode = converter.convert(type);
            if (newNode.getKind() != TreeNode.Kind.EMPTY_STATEMENT) {
                converter.newUnit.addType((AbstractTypeDeclaration) newNode);
            }
        }
        addOcniComments(converter.newUnit, options.jsniWarnings());
        return converter.newUnit;
    } catch (Throwable e) {
        ErrorUtil.fatalError(e, sourceFilePath);
        return null;
    }
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) JavaFileObject(javax.tools.JavaFileObject) TranslationEnvironment(com.google.devtools.j2objc.util.TranslationEnvironment) TreeNode(com.google.devtools.j2objc.ast.TreeNode) JCTree(com.sun.tools.javac.tree.JCTree) PackageElement(javax.lang.model.element.PackageElement)

Example 32 with CompilationUnit

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

the class GenerationTest method translateMethod.

/**
   * Translate a Java method into a JDT DOM MethodDeclaration.  Although JDT
   * has support for parsing methods, it doesn't resolve them.  The statements
   * are therefore wrapped in a type declaration so they having bindings.
   */
protected MethodDeclaration translateMethod(String method) {
    // Wrap statements in test class, so type resolution works.
    String source = "public class Test { " + method + " }";
    CompilationUnit unit = translateType("Test", source);
    final MethodDeclaration[] result = new MethodDeclaration[1];
    unit.accept(new TreeVisitor() {

        @Override
        public boolean visit(MethodDeclaration node) {
            String name = ElementUtil.getName(node.getExecutableElement());
            if (name.equals(NameTable.INIT_NAME) || name.equals(NameTable.FINALIZE_METHOD) || name.equals(NameTable.DEALLOC_METHOD)) {
                return false;
            }
            assert result[0] == null;
            result[0] = node;
            return false;
        }
    });
    return result[0];
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration)

Example 33 with CompilationUnit

use of com.google.devtools.j2objc.ast.CompilationUnit 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 34 with CompilationUnit

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

the class AnonymousClassConverterTest method translateClassBody.

protected List<TypeDeclaration> translateClassBody(String testSource) {
    String source = "public class Test { " + testSource + " }";
    CompilationUnit unit = translateType("Test", source);
    return Lists.newArrayList(Iterables.filter(unit.getTypes(), TypeDeclaration.class));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 35 with CompilationUnit

use of com.google.devtools.j2objc.ast.CompilationUnit 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

CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)71 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)24 HashSet (java.util.HashSet)24 ReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode)23 HashMap (java.util.HashMap)23 Set (java.util.Set)23 CodeReferenceMap (com.google.devtools.j2objc.util.CodeReferenceMap)9 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)7 MethodReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.MethodReferenceNode)7 TypeElement (javax.lang.model.element.TypeElement)5 TreeVisitor (com.google.devtools.j2objc.ast.TreeVisitor)4 NameTable (com.google.devtools.j2objc.util.NameTable)4 TreeNode (com.google.devtools.j2objc.ast.TreeNode)3 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)3 RegularInputFile (com.google.devtools.j2objc.file.RegularInputFile)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)2