Search in sources :

Example 21 with AbstractTypeDeclaration

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

the class PrivateDeclarationResolver method addPublicType.

private void addPublicType(TypeElement typeElement) {
    if (typeElement == null) {
        return;
    }
    AbstractTypeDeclaration typeNode = typeMap.get(typeElement);
    if (typeNode == null) {
        return;
    }
    if (publicTypes.add(typeElement)) {
        publicNodesToVisit.add(typeNode);
    }
    // Make sure supertypes of public types remain public, even if declared
    // private.
    addPublicType(typeElement.getSuperclass());
    for (TypeMirror interfaceType : typeElement.getInterfaces()) {
        addPublicType(interfaceType);
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 22 with AbstractTypeDeclaration

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

the class OcniExtractor method findBlockComments.

/**
   * Finds all block comments and associates them with their containing type.
   * This is trickier than you might expect because of inner types.
   */
private static ListMultimap<TreeNode, Comment> findBlockComments(CompilationUnit unit) {
    ListMultimap<TreeNode, Comment> blockComments = MultimapBuilder.hashKeys().arrayListValues().build();
    for (Comment comment : unit.getCommentList()) {
        if (!comment.isBlockComment()) {
            continue;
        }
        int commentPos = comment.getStartPosition();
        AbstractTypeDeclaration containingType = null;
        int containingTypePos = -1;
        for (AbstractTypeDeclaration type : unit.getTypes()) {
            int typePos = type.getStartPosition();
            if (typePos < 0) {
                continue;
            }
            int typeEnd = typePos + type.getLength();
            if (commentPos > typePos && commentPos < typeEnd && typePos > containingTypePos) {
                containingType = type;
                containingTypePos = typePos;
            }
        }
        blockComments.put(containingType != null ? containingType : unit, comment);
    }
    return blockComments;
}
Also used : Comment(com.google.devtools.j2objc.ast.Comment) TreeNode(com.google.devtools.j2objc.ast.TreeNode) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 23 with AbstractTypeDeclaration

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

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

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

the class ElementUtilTest method testGetAnnotation.

public void testGetAnnotation() throws IOException {
    CompilationUnit unit = translateType("Example", "@com.google.j2objc.annotations.ObjectiveCName(\"E\") class Example {}");
    AbstractTypeDeclaration decl = unit.getTypes().get(0);
    TypeElement element = decl.getTypeElement();
    AnnotationMirror annotation = ElementUtil.getAnnotation(element, ObjectiveCName.class);
    assertEquals("com.google.j2objc.annotations.ObjectiveCName", annotation.getAnnotationType().toString());
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Aggregations

AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)31 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)23 TypeElement (javax.lang.model.element.TypeElement)6 TypeMirror (javax.lang.model.type.TypeMirror)4 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)3 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)3 NameTable (com.google.devtools.j2objc.util.NameTable)3 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)2 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)2 TreeNode (com.google.devtools.j2objc.ast.TreeNode)2 TypeDeclaration (com.google.devtools.j2objc.ast.TypeDeclaration)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 Annotation (com.google.devtools.j2objc.ast.Annotation)1 AnnotationTypeDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeDeclaration)1 Block (com.google.devtools.j2objc.ast.Block)1 Comment (com.google.devtools.j2objc.ast.Comment)1 EnumDeclaration (com.google.devtools.j2objc.ast.EnumDeclaration)1 Expression (com.google.devtools.j2objc.ast.Expression)1 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)1