Search in sources :

Example 6 with BodyDeclaration

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

the class TreeConverter method convertAbstractTypeDeclaration.

private TreeNode convertAbstractTypeDeclaration(JCTree.JCClassDecl node, AbstractTypeDeclaration newNode) {
    convertBodyDeclaration(node, node.getModifiers(), newNode, node.sym);
    List<BodyDeclaration> bodyDeclarations = new ArrayList<>();
    for (JCTree bodyDecl : node.getMembers()) {
        Object member = convert(bodyDecl);
        if (member instanceof BodyDeclaration) {
            // Not true for enum constants.
            bodyDeclarations.add((BodyDeclaration) member);
        } else if (member instanceof Block) {
            JCTree.JCBlock javacBlock = (JCTree.JCBlock) bodyDecl;
            Block block = (Block) member;
            bodyDeclarations.add(new Initializer(block, javacBlock.isStatic()));
        }
    }
    return newNode.setName(convertSimpleName(node.sym, node.sym.asType(), getNamePosition(node))).setTypeElement(node.sym).setBodyDeclarations(bodyDeclarations);
}
Also used : Initializer(com.google.devtools.j2objc.ast.Initializer) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer) ArrayList(java.util.ArrayList) JCTree(com.sun.tools.javac.tree.JCTree) Block(com.google.devtools.j2objc.ast.Block) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) JavaFileObject(javax.tools.JavaFileObject)

Example 7 with BodyDeclaration

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

the class TreeConverter method convertEnum.

private TreeNode convertEnum(JCTree.JCClassDecl node) {
    if (node.sym.isAnonymous()) {
        return convertClassDeclaration(node).setPosition(getPosition(node));
    }
    EnumDeclaration newNode = (EnumDeclaration) new EnumDeclaration();
    convertBodyDeclaration(node, node.getModifiers(), newNode, node.sym);
    newNode.setName(convertSimpleName(node.sym, node.type, getNamePosition(node))).setTypeElement(node.sym);
    for (JCTree bodyDecl : node.getMembers()) {
        if (bodyDecl.getKind() == Kind.VARIABLE) {
            TreeNode var = convertVariableDeclaration((JCTree.JCVariableDecl) bodyDecl);
            if (var.getKind() == TreeNode.Kind.ENUM_CONSTANT_DECLARATION) {
                newNode.addEnumConstant((EnumConstantDeclaration) var);
            } else {
                newNode.addBodyDeclaration((BodyDeclaration) var);
            }
        } else if (bodyDecl.getKind() == Kind.BLOCK) {
            JCTree.JCBlock javacBlock = (JCTree.JCBlock) bodyDecl;
            Block block = (Block) convert(javacBlock);
            newNode.addBodyDeclaration(new Initializer(block, javacBlock.isStatic()));
        } else {
            newNode.addBodyDeclaration((BodyDeclaration) convert(bodyDecl));
        }
    }
    return newNode;
}
Also used : Initializer(com.google.devtools.j2objc.ast.Initializer) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer) TreeNode(com.google.devtools.j2objc.ast.TreeNode) JCTree(com.sun.tools.javac.tree.JCTree) Block(com.google.devtools.j2objc.ast.Block) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) EnumDeclaration(com.google.devtools.j2objc.ast.EnumDeclaration)

Example 8 with BodyDeclaration

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

the class TypeDeclarationGenerator method printInnerDeclarations.

/**
   * Print method declarations with #pragma mark lines documenting their scope.
   */
@Override
protected void printInnerDeclarations() {
    // Everything is public in interfaces.
    if (isInterfaceType() || typeNode.hasPrivateDeclaration()) {
        super.printInnerDeclarations();
        return;
    }
    ListMultimap<DeclarationCategory, BodyDeclaration> categorizedDecls = MultimapBuilder.hashKeys().arrayListValues().build();
    for (BodyDeclaration innerDecl : getInnerDeclarations()) {
        categorizedDecls.put(DeclarationCategory.categorize(innerDecl), innerDecl);
    }
    // Emit the categorized declarations using the declaration order of the category values.
    for (DeclarationCategory category : DeclarationCategory.values()) {
        List<BodyDeclaration> declarations = categorizedDecls.get(category);
        if (declarations.isEmpty()) {
            continue;
        }
        // Extract MethodDeclaration nodes so that they can be sorted.
        List<MethodDeclaration> methods = Lists.newArrayList();
        for (Iterator<BodyDeclaration> iter = declarations.iterator(); iter.hasNext(); ) {
            BodyDeclaration decl = iter.next();
            if (decl instanceof MethodDeclaration) {
                methods.add((MethodDeclaration) decl);
                iter.remove();
            }
        }
        Collections.sort(methods, METHOD_DECL_ORDER);
        newline();
        println(category.header);
        printDeclarations(methods);
        printDeclarations(declarations);
    }
}
Also used : MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration)

Example 9 with BodyDeclaration

use of com.google.devtools.j2objc.ast.BodyDeclaration 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 10 with BodyDeclaration

use of com.google.devtools.j2objc.ast.BodyDeclaration 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

BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)13 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)4 TypeElement (javax.lang.model.element.TypeElement)4 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)3 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)2 Block (com.google.devtools.j2objc.ast.Block)2 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)2 FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)2 Initializer (com.google.devtools.j2objc.ast.Initializer)2 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)2 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)2 JCTree (com.sun.tools.javac.tree.JCTree)2 VariableElement (javax.lang.model.element.VariableElement)2 TypeMirror (javax.lang.model.type.TypeMirror)2 AnnotationTypeMemberDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration)1 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)1 Comment (com.google.devtools.j2objc.ast.Comment)1 ContinueStatement (com.google.devtools.j2objc.ast.ContinueStatement)1