Search in sources :

Example 16 with TreeNode

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

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

the class JavadocConverter method visitText.

@Override
public Void visitText(TextTree node, TagElement tag) {
    String[] lines = node.getBody().split("\n");
    int linePos = pos(node);
    for (String line : lines) {
        if (line.length() > 0) {
            linePos = source.indexOf(line, linePos);
            int endPos = linePos + line.length();
            TreeNode newNode = setPos(new TextElement().setText(line), linePos, endPos);
            tag.addFragment(newNode);
        }
    }
    return null;
}
Also used : TextElement(com.google.devtools.j2objc.ast.TextElement) TreeNode(com.google.devtools.j2objc.ast.TreeNode)

Example 18 with TreeNode

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

the class JavadocConverter method visitReference.

@Override
public Void visitReference(ReferenceTree node, TagElement tag) {
    DCTree.DCReference ref = (DCTree.DCReference) node;
    JCTree qualifier = ref.qualifierExpression;
    TreeNode newNode;
    if (qualifier != null && qualifier.getKind() == com.sun.source.tree.Tree.Kind.MEMBER_SELECT) {
        newNode = convertQualifiedName(qualifier);
    } else {
        newNode = new TextElement().setText(node.getSignature());
    }
    tag.addFragment(setPos(node, newNode));
    return null;
}
Also used : TextElement(com.google.devtools.j2objc.ast.TextElement) TreeNode(com.google.devtools.j2objc.ast.TreeNode) JCTree(com.sun.tools.javac.tree.JCTree) DCTree(com.sun.tools.javac.tree.DCTree)

Example 19 with TreeNode

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

the class JavadocConverter method convertJavadoc.

/**
   * Returns an AST node for the javadoc comment of a specified class,
   * method, or field element.
   */
static Javadoc convertJavadoc(Element element, String source, JavacEnvironment env, boolean reportWarnings) {
    DocTrees docTrees = DocTrees.instance(env.task());
    TreePath path = docTrees.getPath(element);
    if (path == null) {
        throw new AssertionError("could not find tree path for element");
    }
    DCTree.DCDocComment docComment = (DCTree.DCDocComment) docTrees.getDocCommentTree(path);
    if (docComment == null) {
        // Declaration does not have a javadoc comment.
        return null;
    }
    JavadocConverter converter = new JavadocConverter(element, docComment, source, docTrees, path.getCompilationUnit(), reportWarnings);
    Javadoc result = new Javadoc();
    // First tag has no name.
    TagElement newTag = new TagElement();
    converter.scan(docComment.getFirstSentence(), newTag);
    converter.scan(docComment.getBody(), newTag);
    if (!newTag.getFragments().isEmpty()) {
        List<TreeNode> fragments = newTag.getFragments();
        int start = fragments.get(0).getStartPosition();
        TreeNode lastFragment = fragments.get(fragments.size() - 1);
        int end = start + lastFragment.getLength();
        converter.setPos(newTag, start, end);
        result.addTag(newTag);
    }
    for (DocTree tag : docComment.getBlockTags()) {
        if (tag.getKind() != DocTree.Kind.ERRONEOUS) {
            newTag = new TagElement();
            converter.scan(tag, newTag);
            result.addTag(newTag);
        }
    }
    return result;
}
Also used : Javadoc(com.google.devtools.j2objc.ast.Javadoc) TreePath(com.sun.source.util.TreePath) DocTrees(com.sun.source.util.DocTrees) TreeNode(com.google.devtools.j2objc.ast.TreeNode) TagElement(com.google.devtools.j2objc.ast.TagElement) DocTree(com.sun.source.doctree.DocTree) DCTree(com.sun.tools.javac.tree.DCTree)

Example 20 with TreeNode

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

the class JavadocGenerator method getSourceIndent.

/**
   * Fetch the leading whitespace from the comment line. Since the JDT
   * strips leading and trailing whitespace from lines, the original
   * source is fetched and is walked backwards from the fragment's start
   * until the previous new line, then moved forward if there is a leading
   * "* ".
   */
private String getSourceIndent(TreeNode fragment) {
    int index = fragment.getStartPosition();
    if (index < 1) {
        return "";
    }
    TreeNode node = fragment.getParent();
    while (node != null && node.getKind() != TreeNode.Kind.COMPILATION_UNIT) {
        node = node.getParent();
    }
    if (node instanceof CompilationUnit) {
        String source = ((CompilationUnit) node).getSource();
        int i = index - 1;
        char c;
        while (i >= 0 && (c = source.charAt(i)) != '\n') {
            if (c != '*' && !Character.isWhitespace(c)) {
                // Pre tag embedded in other text, so no indent.
                return "";
            }
            --i;
        }
        String lineStart = source.substring(i + 1, index);
        i = lineStart.indexOf('*');
        if (i == -1) {
            return lineStart;
        }
        // Indent could end with '*' instead of "* ", if there's no text after it.
        return (++i + 1) < lineStart.length() ? lineStart.substring(i + 1) : lineStart.substring(i);
    }
    return "";
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeNode(com.google.devtools.j2objc.ast.TreeNode)

Aggregations

TreeNode (com.google.devtools.j2objc.ast.TreeNode)24 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)7 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)6 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)6 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)6 SimpleName (com.google.devtools.j2objc.ast.SimpleName)6 VariableElement (javax.lang.model.element.VariableElement)6 Expression (com.google.devtools.j2objc.ast.Expression)5 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)5 TextElement (com.google.devtools.j2objc.ast.TextElement)5 JCTree (com.sun.tools.javac.tree.JCTree)5 Block (com.google.devtools.j2objc.ast.Block)4 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)4 DCTree (com.sun.tools.javac.tree.DCTree)4 Assignment (com.google.devtools.j2objc.ast.Assignment)3 CastExpression (com.google.devtools.j2objc.ast.CastExpression)3 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)3 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)3 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)2 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)2