use of com.google.devtools.j2objc.ast.AbstractTypeDeclaration in project j2objc by google.
the class TreeConverter method convertBlock.
private TreeNode convertBlock(JCTree.JCBlock node) {
Block newNode = new Block();
for (StatementTree stmt : node.getStatements()) {
TreeNode tree = convert(stmt);
if (tree instanceof AbstractTypeDeclaration) {
tree = new TypeDeclarationStatement().setDeclaration((AbstractTypeDeclaration) tree);
}
newNode.addStatement((Statement) tree);
}
return newNode;
}
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 AnonymousClassConverterTest method testMethodVarInAnonymousClass.
public void testMethodVarInAnonymousClass() throws IOException {
String source = "class Test { " + " boolean debug;" + " void foo() { " + " if (true) {" + " if (debug) {" + " 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(1);
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 PrivateDeclarationResolver method visit.
@Override
public boolean visit(CompilationUnit node) {
// Map the types by their elements.
for (AbstractTypeDeclaration typeNode : node.getTypes()) {
typeMap.put(typeNode.getTypeElement(), typeNode);
}
// Identify types that are public by their declaration.
for (AbstractTypeDeclaration typeNode : node.getTypes()) {
TypeElement typeElement = typeNode.getTypeElement();
if (!ElementUtil.isPrivateInnerType(typeElement)) {
addPublicType(typeElement);
}
}
// public because they are exposed by a field or method from another type.
while (!publicNodesToVisit.isEmpty()) {
AbstractTypeDeclaration publicNode = publicNodesToVisit.remove(publicNodesToVisit.size() - 1);
publicNode.setHasPrivateDeclaration(false);
for (BodyDeclaration decl : publicNode.getBodyDeclarations()) {
decl.accept(this);
}
}
// declarations as private.
for (AbstractTypeDeclaration typeNode : node.getTypes()) {
if (!publicTypes.contains(typeNode.getTypeElement())) {
typeNode.setHasPrivateDeclaration(true);
for (BodyDeclaration decl : typeNode.getBodyDeclarations()) {
decl.setHasPrivateDeclaration(true);
}
}
}
return false;
}
Aggregations