use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class MetadataWriter method visitType.
private void visitType(AbstractTypeDeclaration node) {
if (node.isDeadClass()) {
return;
}
TypeElement type = node.getTypeElement();
if (!translationUtil.needsReflection(type)) {
return;
}
ExecutableElement metadataElement = GeneratedExecutableElement.newMethodWithSelector("__metadata", CLASS_INFO_TYPE, type).addModifiers(Modifier.STATIC, Modifier.PRIVATE);
MethodDeclaration metadataDecl = new MethodDeclaration(metadataElement);
metadataDecl.setHasDeclaration(false);
Block body = new Block();
metadataDecl.setBody(body);
new MetadataGenerator(node, body.getStatements()).generateClassMetadata();
node.addBodyDeclaration(metadataDecl);
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class GenerationTest method translateMethod.
/**
* Translate a Java method into a JDT DOM MethodDeclaration. Although JDT
* has support for parsing methods, it doesn't resolve them. The statements
* are therefore wrapped in a type declaration so they having bindings.
*/
protected MethodDeclaration translateMethod(String method) {
// Wrap statements in test class, so type resolution works.
String source = "public class Test { " + method + " }";
CompilationUnit unit = translateType("Test", source);
final MethodDeclaration[] result = new MethodDeclaration[1];
unit.accept(new TreeVisitor() {
@Override
public boolean visit(MethodDeclaration node) {
String name = ElementUtil.getName(node.getExecutableElement());
if (name.equals(NameTable.INIT_NAME) || name.equals(NameTable.FINALIZE_METHOD) || name.equals(NameTable.DEALLOC_METHOD)) {
return false;
}
assert result[0] == null;
result[0] = node;
return false;
}
});
return result[0];
}
use of com.google.devtools.j2objc.ast.MethodDeclaration 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()"));
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class SwitchRewriterTest method testVariableDeclarationsInSwitchStatement2.
public void testVariableDeclarationsInSwitchStatement2() throws IOException {
CompilationUnit unit = translateType("A", "public class A { public void doSomething(int i) { switch (i) { " + "case 1: int j = i * 2; log(j); break; " + "case 2: log(i); break; " + "case 3: log(i); int k = i, l = 42; break; }}" + "private void log(int i) {}}");
TypeDeclaration testType = (TypeDeclaration) unit.getTypes().get(0);
// First MethodDeclaration is the implicit default constructor.
MethodDeclaration method = TreeUtil.getMethodDeclarationsList(testType).get(1);
List<Statement> stmts = method.getBody().getStatements();
assertEquals(1, stmts.size());
Block block = (Block) stmts.get(0);
stmts = block.getStatements();
assertEquals(4, stmts.size());
assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(2) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(3) instanceof SwitchStatement);
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class CompoundTypeTest method testIsCompound.
// Test TypeUtil.isIntersection(TypeMirror).
public void testIsCompound() throws Exception {
String source = "interface Test<T> extends java.util.Comparator<T> {" + " default Test<T> thenTesting(Test<? super T> other) { " + " return (Test<T> & java.io.Serializable) (c1, c2) -> { " + " int res = compare(c1, c2); " + " return (res != 0) ? res : other.compare(c1, c2); }; }}";
CompilationUnit unit = compileType("Test", source);
AbstractTypeDeclaration decl = unit.getTypes().get(0);
int methodsFound = 0;
for (BodyDeclaration body : decl.getBodyDeclarations()) {
if (body instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) body;
if (ElementUtil.getName(method.getExecutableElement()).equals("thenTesting")) {
// Verify a normal type isn't marked as compound.
TypeMirror returnType = method.getReturnTypeMirror();
assertFalse(TypeUtil.isIntersection(returnType));
// The method's return type isn't compound, but the cast expression in
// its return statement is.
ReturnStatement stmt = (ReturnStatement) method.getBody().getStatements().get(0);
assertTrue(TypeUtil.isIntersection(stmt.getExpression().getTypeMirror()));
methodsFound++;
}
}
}
assertEquals(1, methodsFound);
}
Aggregations