Search in sources :

Example 1 with Comment

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

the class TreeConverter method convertCompilationUnit.

public static CompilationUnit convertCompilationUnit(TranslationEnvironment env, org.eclipse.jdt.core.dom.CompilationUnit jdtUnit, String sourceFilePath, String mainTypeName, String source) {
    CompilationUnit unit = new CompilationUnit(env, sourceFilePath, mainTypeName, source);
    if (jdtUnit.getPackage() == null) {
        unit.setPackage(new PackageDeclaration());
    } else {
        unit.setPackage((PackageDeclaration) TreeConverter.convert(jdtUnit.getPackage()));
    }
    for (Object comment : jdtUnit.getCommentList()) {
        // Comments are not normally parented in the JDT AST. Javadoc nodes are
        // normally parented by the BodyDeclaration they apply to, so here we only
        // keep the unparented comments to avoid duplicate comment nodes.
        ASTNode commentParent = ((ASTNode) comment).getParent();
        if (commentParent == null || commentParent.equals(jdtUnit)) {
            Comment newComment = (Comment) TreeConverter.convert(comment);
            // Since the comment is unparented, it's constructor is unable to get
            // the root CompilationUnit to determine the line number.
            newComment.setLineNumber(jdtUnit.getLineNumber(newComment.getStartPosition()));
            unit.addComment(newComment);
        }
    }
    for (Object type : jdtUnit.types()) {
        unit.addType((AbstractTypeDeclaration) TreeConverter.convert(type));
    }
    return unit;
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) LineComment(com.google.devtools.j2objc.ast.LineComment) BlockComment(com.google.devtools.j2objc.ast.BlockComment) Comment(com.google.devtools.j2objc.ast.Comment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) PackageDeclaration(com.google.devtools.j2objc.ast.PackageDeclaration)

Example 2 with Comment

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

the class DeadCodeEliminator method stripClass.

private void stripClass(AbstractTypeDeclaration node) {
    boolean removeClass = true;
    for (Iterator<BodyDeclaration> iter = node.getBodyDeclarations().iterator(); iter.hasNext(); ) {
        BodyDeclaration decl = iter.next();
        // and even if they are dead, they may still be referenced by other classes.
        if (decl instanceof TypeDeclaration) {
            TypeElement type = ((TypeDeclaration) decl).getTypeElement();
            if (type.getKind().isInterface() || ElementUtil.isStatic(type)) {
                endVisit((TypeDeclaration) decl);
                continue;
            }
        }
        if (decl.getKind() == Kind.FIELD_DECLARATION) {
            FieldDeclaration field = (FieldDeclaration) decl;
            VariableDeclarationFragment fragment = field.getFragment();
            // Don't delete any constants because we can't detect their use.
            if (fragment.getVariableElement().getConstantValue() == null) {
                fragment.remove();
                iter.remove();
            } else {
                removeClass = false;
            }
        } else {
            if (decl instanceof MethodDeclaration) {
                unit.setHasIncompleteProtocol();
            }
            iter.remove();
        }
    }
    if (removeClass) {
        node.setDeadClass(true);
        // Remove any class-level OCNI comment blocks.
        int srcStart = node.getStartPosition();
        String src = unit.getSource().substring(srcStart, srcStart + node.getLength());
        if (src.contains("/*-[")) {
            int ocniStart = srcStart + src.indexOf("/*-[");
            int ocniEnd = ocniStart + node.getLength();
            Iterator<Comment> commentsIter = unit.getCommentList().iterator();
            while (commentsIter.hasNext()) {
                Comment comment = commentsIter.next();
                if (comment.isBlockComment() && comment.getStartPosition() >= ocniStart && (comment.getStartPosition() + comment.getLength()) <= ocniEnd) {
                    commentsIter.remove();
                }
            }
        }
    }
}
Also used : Comment(com.google.devtools.j2objc.ast.Comment) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration) AnnotationTypeDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 3 with Comment

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

the class TreeConverter method convertAssociatedComment.

private Comment convertAssociatedComment(Tree node, TreePath path) {
    boolean docCommentsEnabled = newUnit.getEnv().options().docCommentsEnabled();
    DocCommentTable docComments = ((JCCompilationUnit) unit).docComments;
    if (!docCommentsEnabled || docComments == null || !docComments.hasComment((JCTree) node)) {
        return null;
    }
    com.sun.tools.javac.parser.Tokens.Comment javacComment = docComments.getComment((JCTree) node);
    Comment comment;
    switch(javacComment.getStyle()) {
        case BLOCK:
            comment = new BlockComment();
            break;
        case JAVADOC:
            comment = convertJavadocComment(path);
            break;
        case LINE:
            comment = new LineComment();
            break;
        default:
            throw new AssertionError("unknown comment type");
    }
    int startPos = javacComment.getSourcePos(0);
    int endPos = startPos + javacComment.getText().length();
    comment.setSourceRange(startPos, endPos);
    comment.setLineNumber((int) unit.getLineMap().getLineNumber(startPos));
    return comment;
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) LineComment(com.google.devtools.j2objc.ast.LineComment) BlockComment(com.google.devtools.j2objc.ast.BlockComment) Comment(com.google.devtools.j2objc.ast.Comment) BlockComment(com.google.devtools.j2objc.ast.BlockComment) DocCommentTable(com.sun.tools.javac.tree.DocCommentTable) LineComment(com.google.devtools.j2objc.ast.LineComment)

Example 4 with Comment

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

the class TreeConverter method convertAssociatedComment.

private Comment convertAssociatedComment(JCTree node, Element element) {
    boolean docCommentsEnabled = newUnit.getEnv().options().docCommentsEnabled();
    DocCommentTable docComments = unit.docComments;
    if (!docCommentsEnabled || docComments == null || !docComments.hasComment(node)) {
        return null;
    }
    com.sun.tools.javac.parser.Tokens.Comment javacComment = docComments.getComment(node);
    Comment comment;
    switch(javacComment.getStyle()) {
        case BLOCK:
            comment = new BlockComment();
            break;
        case JAVADOC:
            comment = docCommentsEnabled ? convertJavadocComment(element) : new Javadoc();
            break;
        case LINE:
            comment = new LineComment();
            break;
        default:
            throw new AssertionError("unknown comment type");
    }
    int startPos = javacComment.getSourcePos(0);
    int endPos = startPos + javacComment.getText().length();
    comment.setSourceRange(startPos, endPos);
    comment.setLineNumber(unit.getLineMap().getLineNumber(startPos));
    return comment;
}
Also used : LineComment(com.google.devtools.j2objc.ast.LineComment) BlockComment(com.google.devtools.j2objc.ast.BlockComment) Comment(com.google.devtools.j2objc.ast.Comment) BlockComment(com.google.devtools.j2objc.ast.BlockComment) DocCommentTable(com.sun.tools.javac.tree.DocCommentTable) Javadoc(com.google.devtools.j2objc.ast.Javadoc) LineComment(com.google.devtools.j2objc.ast.LineComment)

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

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