Search in sources :

Example 11 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 12 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 13 with AbstractTypeDeclaration

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

the class TranslationUtil method getInterfaceTypes.

public static List<TypeElement> getInterfaceTypes(AbstractTypeDeclaration node) {
    // Use the AST as the source of truth where possible.
    List<? extends TypeMirror> astInterfaces = null;
    if (node instanceof TypeDeclaration) {
        astInterfaces = ((TypeDeclaration) node).getSuperInterfaceTypeMirrors();
    } else if (node instanceof EnumDeclaration) {
        astInterfaces = ((EnumDeclaration) node).getSuperInterfaceTypeMirrors();
    } else {
        // AnnotationTypeDeclaration
        return ElementUtil.getInterfaces(node.getTypeElement());
    }
    List<TypeElement> result = new ArrayList<>();
    for (TypeMirror typeMirror : astInterfaces) {
        result.add(TypeUtil.asTypeElement(typeMirror));
    }
    return result;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration) EnumDeclaration(com.google.devtools.j2objc.ast.EnumDeclaration)

Example 14 with AbstractTypeDeclaration

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

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

AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)31 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)23 TypeElement (javax.lang.model.element.TypeElement)6 TypeMirror (javax.lang.model.type.TypeMirror)4 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)3 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)3 NameTable (com.google.devtools.j2objc.util.NameTable)3 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)2 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)2 TreeNode (com.google.devtools.j2objc.ast.TreeNode)2 TypeDeclaration (com.google.devtools.j2objc.ast.TypeDeclaration)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 Annotation (com.google.devtools.j2objc.ast.Annotation)1 AnnotationTypeDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeDeclaration)1 Block (com.google.devtools.j2objc.ast.Block)1 Comment (com.google.devtools.j2objc.ast.Comment)1 EnumDeclaration (com.google.devtools.j2objc.ast.EnumDeclaration)1 Expression (com.google.devtools.j2objc.ast.Expression)1 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)1