Search in sources :

Example 26 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();
    if (options.isJDT()) {
        assertEquals(3, stmts.size());
        assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
        assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
        assertTrue(stmts.get(2) instanceof SwitchStatement);
    } else {
        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 27 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)

Example 28 with MethodDeclaration

use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.

the class CompoundTypeTest method testCompoundTypeFullName.

// Test NameTable.getObjCType(TypeMirror).
public void testCompoundTypeFullName() throws IOException {
    String source = "package foo.bar; 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);
    for (BodyDeclaration body : decl.getBodyDeclarations()) {
        if (body instanceof MethodDeclaration) {
            MethodDeclaration method = (MethodDeclaration) body;
            if (ElementUtil.getName(method.getExecutableElement()).equals("thenTesting")) {
                // 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);
                TypeMirror mirror = stmt.getExpression().getTypeMirror();
                String typeName = unit.getEnv().nameTable().getObjCType(mirror);
                assertEquals("id<FooBarTest, JavaIoSerializable>", typeName);
                return;
            }
        }
    }
    fail("thenTesting method not found");
}
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)

Example 29 with MethodDeclaration

use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.

the class ElementReferenceMapper method endVisit.

/**
   * When a constructor in invoked (including a default constructor), adds the constructor and
   * invoking method to elementReferenceMap. The class will eventually be marked as used.
   * Counts as both the declaration (in class) and invocation (new _()) of the constructor.
   */
@Override
public void endVisit(ClassInstanceCreation instance) {
    ExecutableElement childMethodElement = instance.getExecutableElement();
    handleChildMethod(childMethodElement);
    MethodDeclaration parentMethodDeclaration = TreeUtil.getEnclosingMethod(instance);
    if (parentMethodDeclaration == null) {
        staticSet.add(stitchMethodIdentifier(childMethodElement));
        return;
    }
    ExecutableElement parentMethodElement = parentMethodDeclaration.getExecutableElement();
    handleParentMethod(parentMethodElement, childMethodElement);
}
Also used : MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 30 with MethodDeclaration

use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.

the class ElementReferenceMapper method endVisit.

@Override
public void endVisit(SuperConstructorInvocation invocation) {
    ExecutableElement childMethodElement = invocation.getExecutableElement();
    handleChildMethod(childMethodElement);
    MethodDeclaration parentMethodDeclaration = TreeUtil.getEnclosingMethod(invocation);
    if (parentMethodDeclaration == null) {
        staticSet.add(stitchMethodIdentifier(childMethodElement));
        return;
    }
    ExecutableElement parentMethodElement = parentMethodDeclaration.getExecutableElement();
    handleParentMethod(parentMethodElement, childMethodElement);
}
Also used : MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement)

Aggregations

MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)30 ExecutableElement (javax.lang.model.element.ExecutableElement)18 Block (com.google.devtools.j2objc.ast.Block)13 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)11 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)7 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)7 TypeElement (javax.lang.model.element.TypeElement)7 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)6 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)6 VariableElement (javax.lang.model.element.VariableElement)6 TypeMirror (javax.lang.model.type.TypeMirror)6 TreeVisitor (com.google.devtools.j2objc.ast.TreeVisitor)5 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)4 Statement (com.google.devtools.j2objc.ast.Statement)4 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)4 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)3 SimpleName (com.google.devtools.j2objc.ast.SimpleName)3 SuperMethodInvocation (com.google.devtools.j2objc.ast.SuperMethodInvocation)3 GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)3 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)2