Search in sources :

Example 16 with BodyDeclaration

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

the class OcniExtractor method visitType.

private void visitType(AbstractTypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    Set<String> methodsPrinted = Sets.newHashSet();
    List<BodyDeclaration> bodyDeclarations = node.getBodyDeclarations();
    int minPos = 0;
    int declIdx = 0;
    for (Comment comment : blockComments.get(node)) {
        int commentPos = comment.getStartPosition();
        while (declIdx < bodyDeclarations.size()) {
            BodyDeclaration decl = bodyDeclarations.get(declIdx);
            if (decl.getStartPosition() > commentPos) {
                break;
            }
            minPos = Math.max(minPos, decl.getStartPosition() + decl.getLength());
            declIdx++;
        }
        if (commentPos > minPos) {
            NativeDeclaration nativeDecl = extractNativeDeclaration(comment);
            if (nativeDecl != null) {
                findMethodSignatures(nativeDecl.getImplementationCode(), methodsPrinted);
                bodyDeclarations.add(declIdx++, nativeDecl);
            }
        }
    }
    // methods are always live.
    if (typeUtil.findSupertype(type.asType(), "java.lang.Iterable") != null && !methodsPrinted.contains("countByEnumeratingWithState:objects:count:") && (deadCodeMap == null || !deadCodeMap.containsClass(type, elementUtil))) {
        bodyDeclarations.add(NativeDeclaration.newInnerDeclaration(null, "- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state " + "objects:(__unsafe_unretained id *)stackbuf count:(NSUInteger)len {\n" + "  return JreDefaultFastEnumeration(self, state, stackbuf);\n}\n"));
    }
}
Also used : Comment(com.google.devtools.j2objc.ast.Comment) NativeDeclaration(com.google.devtools.j2objc.ast.NativeDeclaration) TypeElement(javax.lang.model.element.TypeElement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration)

Example 17 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 18 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)18 TypeElement (javax.lang.model.element.TypeElement)9 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)6 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)5 ExecutableElement (javax.lang.model.element.ExecutableElement)5 AnnotationTypeDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeDeclaration)4 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)4 Block (com.google.devtools.j2objc.ast.Block)4 Initializer (com.google.devtools.j2objc.ast.Initializer)4 VariableElement (javax.lang.model.element.VariableElement)4 FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)3 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)3 TypeDeclaration (com.google.devtools.j2objc.ast.TypeDeclaration)3 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)3 GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)3 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)3 JCTree (com.sun.tools.javac.tree.JCTree)3 Comment (com.google.devtools.j2objc.ast.Comment)2 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)2 EnumDeclaration (com.google.devtools.j2objc.ast.EnumDeclaration)2