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