Search in sources :

Example 6 with Comment

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

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

Aggregations

Comment (com.google.devtools.j2objc.ast.Comment)7 BlockComment (com.google.devtools.j2objc.ast.BlockComment)3 LineComment (com.google.devtools.j2objc.ast.LineComment)3 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)2 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 DocCommentTable (com.sun.tools.javac.tree.DocCommentTable)2 TypeElement (javax.lang.model.element.TypeElement)2 AnnotationTypeDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeDeclaration)1 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)1 FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)1 Javadoc (com.google.devtools.j2objc.ast.Javadoc)1 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)1 NativeDeclaration (com.google.devtools.j2objc.ast.NativeDeclaration)1 PackageDeclaration (com.google.devtools.j2objc.ast.PackageDeclaration)1 TreeNode (com.google.devtools.j2objc.ast.TreeNode)1 TypeDeclaration (com.google.devtools.j2objc.ast.TypeDeclaration)1 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)1 GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)1 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1