Search in sources :

Example 1 with AbstractTypeDeclaration

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

the class TreeConverter method convertBlock.

private TreeNode convertBlock(JCTree.JCBlock node) {
    Block newNode = new Block();
    for (StatementTree stmt : node.getStatements()) {
        TreeNode tree = convert(stmt);
        if (tree instanceof AbstractTypeDeclaration) {
            tree = new TypeDeclarationStatement().setDeclaration((AbstractTypeDeclaration) tree);
        }
        newNode.addStatement((Statement) tree);
    }
    return newNode;
}
Also used : StatementTree(com.sun.source.tree.StatementTree) TreeNode(com.google.devtools.j2objc.ast.TreeNode) TypeDeclarationStatement(com.google.devtools.j2objc.ast.TypeDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 2 with AbstractTypeDeclaration

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

the class SignatureGeneratorTest method testJniSignatures.

public void testJniSignatures() throws IOException {
    CompilationUnit unit = translateType("D", "package foo.bar; class D {" + "native void foo(int i, float f, String s);" + "native void a_b$c();" + "native void 你好世界();" + "native void bar();" + "native void bar(String s);" + "native void bar(boolean b, String s);" + "native void bar(String[] s); " + "static class 测试 { native void mumble(); }}");
    SignatureGenerator signatureGenerator = unit.getEnv().signatureGenerator();
    List<AbstractTypeDeclaration> decls = unit.getTypes();
    assertEquals(2, decls.size());
    List<ExecutableElement> methods = Lists.newArrayList(ElementUtil.getMethods(decls.get(0).getTypeElement()));
    assertEquals(7, methods.size());
    Set<String> signatures = new HashSet<>(methods.size());
    for (ExecutableElement method : methods) {
        signatures.add(signatureGenerator.createJniFunctionSignature(method));
    }
    // Expected JNI signatures were copied from javah output.
    // Verify no parameters, since foo isn't overloaded.
    assertTrue(signatures.contains("Java_foo_bar_D_foo"));
    // Verify underscores and dollar signs in names are mangled.
    assertTrue(signatures.contains("Java_foo_bar_D_a_1b_00024c"));
    // Verify Unicode characters are mangled.
    assertTrue(signatures.contains("Java_foo_bar_D__04f60_0597d_04e16_0754c"));
    // Verify overloaded methods have parameter suffixes.
    assertTrue(signatures.contains("Java_foo_bar_D_bar__"));
    assertTrue(signatures.contains("Java_foo_bar_D_bar__Ljava_lang_String_2"));
    assertTrue(signatures.contains("Java_foo_bar_D_bar__ZLjava_lang_String_2"));
    assertTrue(signatures.contains("Java_foo_bar_D_bar___3Ljava_lang_String_2"));
    // Check Unicode class name mangling.
    methods = Lists.newArrayList(ElementUtil.getMethods(decls.get(1).getTypeElement()));
    assertEquals(1, methods.size());
    assertEquals("Java_foo_bar_D_00024_06d4b_08bd5_mumble", signatureGenerator.createJniFunctionSignature(methods.get(0)));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) ExecutableElement(javax.lang.model.element.ExecutableElement) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration) HashSet(java.util.HashSet)

Example 3 with AbstractTypeDeclaration

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

the class SignatureGeneratorTest method testGenericInterface.

public void testGenericInterface() throws IOException {
    CompilationUnit unit = translateType("A", "interface A<E> extends java.util.Collection<E> {}");
    SignatureGenerator signatureGenerator = unit.getEnv().signatureGenerator();
    List<AbstractTypeDeclaration> decls = unit.getTypes();
    assertEquals(1, decls.size());
    assertEquals("<E:Ljava/lang/Object;>Ljava/lang/Object;Ljava/util/Collection<TE;>;", signatureGenerator.createClassSignature(decls.get(0).getTypeElement()));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 4 with AbstractTypeDeclaration

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

the class AnonymousClassConverterTest method testMethodVarInAnonymousClass.

public void testMethodVarInAnonymousClass() throws IOException {
    String source = "class Test { " + "  boolean debug;" + "  void foo() { " + "    if (true) {" + "      if (debug) {" + "        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(1);
    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 5 with AbstractTypeDeclaration

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

the class PrivateDeclarationResolver method visit.

@Override
public boolean visit(CompilationUnit node) {
    // Map the types by their elements.
    for (AbstractTypeDeclaration typeNode : node.getTypes()) {
        typeMap.put(typeNode.getTypeElement(), typeNode);
    }
    // Identify types that are public by their declaration.
    for (AbstractTypeDeclaration typeNode : node.getTypes()) {
        TypeElement typeElement = typeNode.getTypeElement();
        if (!ElementUtil.isPrivateInnerType(typeElement)) {
            addPublicType(typeElement);
        }
    }
    // public because they are exposed by a field or method from another type.
    while (!publicNodesToVisit.isEmpty()) {
        AbstractTypeDeclaration publicNode = publicNodesToVisit.remove(publicNodesToVisit.size() - 1);
        publicNode.setHasPrivateDeclaration(false);
        for (BodyDeclaration decl : publicNode.getBodyDeclarations()) {
            decl.accept(this);
        }
    }
    // declarations as private.
    for (AbstractTypeDeclaration typeNode : node.getTypes()) {
        if (!publicTypes.contains(typeNode.getTypeElement())) {
            typeNode.setHasPrivateDeclaration(true);
            for (BodyDeclaration decl : typeNode.getBodyDeclarations()) {
                decl.setHasPrivateDeclaration(true);
            }
        }
    }
    return false;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Aggregations

AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)36 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)25 TypeElement (javax.lang.model.element.TypeElement)9 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)4 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)4 TypeMirror (javax.lang.model.type.TypeMirror)4 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)3 TreeNode (com.google.devtools.j2objc.ast.TreeNode)3 TypeDeclaration (com.google.devtools.j2objc.ast.TypeDeclaration)3 NameTable (com.google.devtools.j2objc.util.NameTable)3 Annotation (com.google.devtools.j2objc.ast.Annotation)2 AnnotationTypeDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeDeclaration)2 Block (com.google.devtools.j2objc.ast.Block)2 Comment (com.google.devtools.j2objc.ast.Comment)2 FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)2 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)2 TypeDeclarationStatement (com.google.devtools.j2objc.ast.TypeDeclarationStatement)2 GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)2 StatementTree (com.sun.source.tree.StatementTree)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2