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)));
}
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()));
}
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()));
}
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)));
}
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()"));
}
Aggregations