Search in sources :

Example 36 with MethodDeclaration

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);
}
Also used : GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Block(com.google.devtools.j2objc.ast.Block)

Example 37 with MethodDeclaration

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];
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration)

Example 38 with MethodDeclaration

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

Example 39 with MethodDeclaration

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);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) Block(com.google.devtools.j2objc.ast.Block) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Example 40 with MethodDeclaration

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);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TypeMirror(javax.lang.model.type.TypeMirror) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Aggregations

MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)41 ExecutableElement (javax.lang.model.element.ExecutableElement)23 Block (com.google.devtools.j2objc.ast.Block)20 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)19 TypeElement (javax.lang.model.element.TypeElement)12 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)11 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)10 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)9 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)8 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)8 VariableElement (javax.lang.model.element.VariableElement)8 TypeMirror (javax.lang.model.type.TypeMirror)7 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)6 TreeVisitor (com.google.devtools.j2objc.ast.TreeVisitor)5 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)5 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)4 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)4 SimpleName (com.google.devtools.j2objc.ast.SimpleName)4 Statement (com.google.devtools.j2objc.ast.Statement)4 GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)4