Search in sources :

Example 41 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)

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