Search in sources :

Example 36 with CompilationUnit

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

the class SignatureGeneratorTest method testMethodSignatures.

public void testMethodSignatures() throws IOException {
    CompilationUnit unit = translateType("A", "class A<X, Y, Z extends Throwable> { " + "void a() {} " + "int b(boolean z) { return 0; } " + "void c(int i) throws IndexOutOfBoundsException {} " + "X d() { return null; } " + "void e(X x, Y y) {} " + "void f() throws Z {} " + "<T extends Throwable> void rethrow(Throwable t) {}" + "}");
    SignatureGenerator signatureGenerator = unit.getEnv().signatureGenerator();
    List<ExecutableElement> methods = ElementUtil.getExecutables(unit.getTypes().get(0).getTypeElement());
    // methods[0] is the default constructor.
    assertEquals(8, methods.size());
    // Verify a, b and c don't return a signature, since they aren't generic types.
    assertNull(signatureGenerator.createMethodTypeSignature(methods.get(1)));
    assertNull(signatureGenerator.createMethodTypeSignature(methods.get(2)));
    assertNull(signatureGenerator.createMethodTypeSignature(methods.get(3)));
    assertEquals("()TX;", signatureGenerator.createMethodTypeSignature(methods.get(4)));
    assertEquals("(TX;TY;)V", signatureGenerator.createMethodTypeSignature(methods.get(5)));
    assertEquals("()V^TZ;", signatureGenerator.createMethodTypeSignature(methods.get(6)));
    assertEquals("<T:Ljava/lang/Throwable;>(Ljava/lang/Throwable;)V", signatureGenerator.createMethodTypeSignature(methods.get(7)));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 37 with CompilationUnit

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

the class SignatureGeneratorTest method testClassSignatures.

public void testClassSignatures() throws IOException {
    CompilationUnit unit = translateType("A", "import java.lang.ref.*; import java.util.concurrent.*; " + "class A {} " + "class B<X> {} " + "class C<X,Y,Z> {} " + "abstract class D extends java.util.AbstractList<String> {} " + "class E<C extends Comparable<? super C>> {}" + "class F extends WeakReference<ForkJoinTask<?>> { " + "  F(ForkJoinTask<?> task, Throwable ex, F next) { super(task, null); }}");
    SignatureGenerator signatureGenerator = unit.getEnv().signatureGenerator();
    List<AbstractTypeDeclaration> decls = unit.getTypes();
    assertEquals(6, decls.size());
    // Verify A doesn't return a signature, since it isn't a generic type.
    assertNull(signatureGenerator.createClassSignature(decls.get(0).getTypeElement()));
    assertEquals("<X:Ljava/lang/Object;>Ljava/lang/Object;", signatureGenerator.createClassSignature(decls.get(1).getTypeElement()));
    assertEquals("<X:Ljava/lang/Object;Y:Ljava/lang/Object;Z:Ljava/lang/Object;>Ljava/lang/Object;", signatureGenerator.createClassSignature(decls.get(2).getTypeElement()));
    assertEquals("Ljava/util/AbstractList<Ljava/lang/String;>;", signatureGenerator.createClassSignature(decls.get(3).getTypeElement()));
    assertEquals("<C::Ljava/lang/Comparable<-TC;>;>Ljava/lang/Object;", signatureGenerator.createClassSignature(decls.get(4).getTypeElement()));
    assertEquals("Ljava/lang/ref/WeakReference<Ljava/util/concurrent/ForkJoinTask<*>;>;", signatureGenerator.createClassSignature(decls.get(5).getTypeElement()));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 38 with CompilationUnit

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

the class SignatureGeneratorTest method testMultipleBounds.

public void testMultipleBounds() throws IOException {
    CompilationUnit unit = translateType("A", "class A <T extends Number & java.io.Serializable >{}");
    SignatureGenerator signatureGenerator = unit.getEnv().signatureGenerator();
    List<AbstractTypeDeclaration> decls = unit.getTypes();
    assertEquals(1, decls.size());
    assertEquals("<T:Ljava/lang/Number;:Ljava/io/Serializable;>Ljava/lang/Object;", signatureGenerator.createClassSignature(decls.get(0).getTypeElement()));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 39 with CompilationUnit

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

the class SignatureGeneratorTest method testFieldSignatures.

public void testFieldSignatures() throws IOException {
    CompilationUnit unit = translateType("A", "class A<X,Y,Z> { int a; double[] b; X c; Y[] d; " + "Class<?> e; java.util.List<X> f; Comparable<? super X> g; " + "A<? extends Number, ?, String> h; }");
    SignatureGenerator signatureGenerator = unit.getEnv().signatureGenerator();
    List<VariableElement> vars = ElementUtil.getDeclaredFields(unit.getTypes().get(0).getTypeElement());
    assertEquals(8, vars.size());
    // Verify a and b don't return a signature, since they aren't generic types.
    assertNull(signatureGenerator.createFieldTypeSignature(vars.get(0)));
    assertNull(signatureGenerator.createFieldTypeSignature(vars.get(1)));
    assertEquals("TX;", signatureGenerator.createFieldTypeSignature(vars.get(2)));
    assertEquals("[TY;", signatureGenerator.createFieldTypeSignature(vars.get(3)));
    assertEquals("Ljava/lang/Class<*>;", signatureGenerator.createFieldTypeSignature(vars.get(4)));
    assertEquals("Ljava/util/List<TX;>;", signatureGenerator.createFieldTypeSignature(vars.get(5)));
    assertEquals("Ljava/lang/Comparable<-TX;>;", signatureGenerator.createFieldTypeSignature(vars.get(6)));
    assertEquals("LA<+Ljava/lang/Number;*Ljava/lang/String;>;", signatureGenerator.createFieldTypeSignature(vars.get(7)));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) VariableElement(javax.lang.model.element.VariableElement)

Example 40 with CompilationUnit

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

the class TypeDeclarationGeneratorTest method testSortMethods.

public void testSortMethods() throws IOException {
    String source = "class A {" + "void zebra() {}" + "void gnu(String s, int i, Runnable r) {}" + "A(int i) {}" + "void gnu() {}" + "void gnu(int i, Runnable r) {}" + "void yak() {}" + "A(String s) {}" + "A() {}" + "A(int i, Runnable r) {}" + "void gnu(String s, int i) {}}";
    CompilationUnit unit = translateType("A", source);
    final ArrayList<MethodDeclaration> methods = new ArrayList<>();
    unit.accept(new TreeVisitor() {

        @Override
        public void endVisit(MethodDeclaration node) {
            if (!ElementUtil.isSynthetic(node.getExecutableElement())) {
                methods.add(node);
            }
        }
    });
    Collections.sort(methods, TypeDeclarationGenerator.METHOD_DECL_ORDER);
    assertTrue(methods.get(0).toString().startsWith("<init>()"));
    assertTrue(methods.get(1).toString().startsWith("<init>(int i)"));
    assertTrue(methods.get(2).toString().startsWith("<init>(int i,java.lang.Runnable r)"));
    assertTrue(methods.get(3).toString().startsWith("<init>(java.lang.String s)"));
    assertTrue(methods.get(4).toString().startsWith("void gnu()"));
    assertTrue(methods.get(5).toString().startsWith("void gnu(int i,java.lang.Runnable r)"));
    assertTrue(methods.get(6).toString().startsWith("void gnu(java.lang.String s,int i)"));
    assertTrue(methods.get(7).toString().startsWith("void gnu(java.lang.String s,int i,java.lang.Runnable r)"));
    assertTrue(methods.get(8).toString().startsWith("void yak()"));
    assertTrue(methods.get(9).toString().startsWith("void zebra()"));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ArrayList(java.util.ArrayList)

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