Search in sources :

Example 1 with CompilationUnit

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

the class OuterReferenceResolverTest method resolveSource.

private void resolveSource(String name, String source) {
    CompilationUnit unit = compileType(name, source);
    captureInfo = unit.getEnv().captureInfo();
    new OuterReferenceResolver(unit).run();
    findTypeDeclarations(unit);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit)

Example 2 with CompilationUnit

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

the class ElementUtilTest method testIsRuntimeAnnotation.

public void testIsRuntimeAnnotation() throws IOException {
    // SuppressWarnings is a source-level annotation.
    CompilationUnit unit = translateType("Example", "@SuppressWarnings(\"test\") class Example {}");
    AbstractTypeDeclaration decl = unit.getTypes().get(0);
    Annotation annotation = decl.getAnnotations().get(0);
    assertFalse(ElementUtil.isRuntimeAnnotation(annotation.getAnnotationMirror()));
    // Deprecated is a runtime annotation..
    unit = translateType("Example", "@Deprecated class Example {}");
    decl = unit.getTypes().get(0);
    annotation = decl.getAnnotations().get(0);
    assertTrue(ElementUtil.isRuntimeAnnotation(annotation.getAnnotationMirror()));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) Annotation(com.google.devtools.j2objc.ast.Annotation) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 3 with CompilationUnit

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

the class SignatureGeneratorTest method testGenericMethodWithConcreteTypeArgument.

public void testGenericMethodWithConcreteTypeArgument() throws IOException {
    CompilationUnit unit = translateType("MyList", "abstract class MyList extends java.util.AbstractList<String> { " + "public boolean add(String s) { return true; }}");
    SignatureGenerator signatureGenerator = unit.getEnv().signatureGenerator();
    List<ExecutableElement> methods = ElementUtil.getExecutables(unit.getTypes().get(0).getTypeElement());
    // methods[0] is the default constructor.
    assertEquals(2, methods.size());
    // add(String) does not need a generic signature, even though it overrides a generic method.
    assertNull(signatureGenerator.createMethodTypeSignature(methods.get(1)));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 4 with CompilationUnit

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

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

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