com.sun.source.util.DocSourcePositions Java Examples

The following examples show how to use com.sun.source.util.DocSourcePositions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FindLocalUsagesQuery.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public DocTree visitIdentifier(com.sun.source.doctree.IdentifierTree node, Element p) {
    DocTrees trees = info.getDocTrees();
    Element el = trees.getElement(getCurrentPath());
    if (el != null && el.equals(toFind)) {
        DocSourcePositions sp = trees.getSourcePositions();
        CompilationUnitTree cut = info.getCompilationUnit();
        DocCommentTree docComment = getCurrentPath().getDocComment();
        long start = sp.getStartPosition(cut, docComment, node);
        long end = sp.getEndPosition(cut, docComment, node);
        if(start != Diagnostic.NOPOS && end != Diagnostic.NOPOS) {
            try {
                MutablePositionRegion region = createRegion(doc, (int)start, (int)end);
                usages.add(region);
            } catch (BadLocationException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }
    return super.visitIdentifier(node, p);
}
 
Example #2
Source File: Analyzer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitDocComment(DocCommentTree node, List<ErrorDescription> errors) {
    DocTreePath currentDocPath = getCurrentPath();
    Void value = super.visitDocComment(node, errors);
    DocSourcePositions sp = (DocSourcePositions) javac.getTrees().getSourcePositions();

    while (!tagStack.isEmpty()) {
        StartElementTree startTree = tagStack.pop();
        Name tagName = startTree.getName();
        HtmlTag tag = HtmlTag.get(tagName);
        if (tag.endKind == HtmlTag.EndKind.REQUIRED) {
            int s = (int) sp.getStartPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), startTree);
            int e = (int) sp.getEndPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), startTree);
            errors.add(ErrorDescriptionFactory.forSpan(ctx, s, e, TAG_START_UNMATCHED(tagName)));
        }
    }
    return value;
}
 
Example #3
Source File: FindLocalUsagesQuery.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public DocTree visitText(TextTree node, Element p) {
    if(searchComment) {
        DocTrees trees = info.getDocTrees();
        DocSourcePositions sourcePositions = trees.getSourcePositions();
        DocTreePath currentDocPath = getCurrentPath();
        if(toFind.getKind() == ElementKind.PARAMETER) {
            VariableElement var = (VariableElement) toFind;
            Element method = trees.getElement(currentDocPath);
            if(!var.getEnclosingElement().equals(method)) {
                return super.visitText(node, p);
            }
        }
        String text = node.getBody();
        String name = toFind.getSimpleName().toString();
        if(text.contains(name)) {
            int start = (int) sourcePositions.getStartPosition(info.getCompilationUnit(), currentDocPath.getDocComment(), node);
            int length = name.length();
            int offset = -1;
            do {
                offset = text.indexOf(name, ++offset);
                if(offset != -1) {
                    try {
                        MutablePositionRegion region = createRegion(doc, start + offset, start + offset + length);
                        comments.add(region);
                    } catch(BadLocationException ex) {
                        Exceptions.printStackTrace(ex);
                    }
                }
            } while (offset != -1);
        }
    }
    return super.visitText(node, p);
}
 
Example #4
Source File: JavadocCompletionQuery.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean resolveContext(CompilationInfo javac, JavadocContext jdctx) throws IOException {
    jdctx.doc = javac.getDocument();
    // find class context: class, method, ...
    DocTrees trees = javac.getDocTrees();
    TreePath javadocFor = JavadocCompletionUtils.findJavadoc(javac, this.caretOffset);
    if (javadocFor == null) {
        return false;
    }
    jdctx.javadocFor = javadocFor;
    DocCommentTree docCommentTree = trees.getDocCommentTree(javadocFor);
    if (docCommentTree == null) {
        return false;
    }
    jdctx.comment = docCommentTree;
    Element elm = trees.getElement(javadocFor);
    if (elm == null) {
        return false;
    }
    jdctx.handle = ElementHandle.create(elm);
    jdctx.commentFor = elm;
    jdctx.jdts = JavadocCompletionUtils.findJavadocTokenSequence(javac, this.caretOffset);
    if (jdctx.jdts == null) {
        return false;
    }
    jdctx.positions = (DocSourcePositions) trees.getSourcePositions();
    return jdctx.positions != null;
}
 
Example #5
Source File: Analyzer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
@NbBundle.Messages({"WRONG_RETURN_DESC=@return tag cannot be used in method with void return type.",
                    "WRONG_CONSTRUCTOR_RETURN_DESC=Illegal @return tag.",
                    "DUPLICATE_RETURN_DESC=Duplicate @return tag."})
public Void visitReturn(ReturnTree node, List<ErrorDescription> errors) {
    boolean oldInheritDoc = foundInheritDoc;
    DocTreePath currentDocPath = getCurrentPath();
    DocTreePathHandle dtph = DocTreePathHandle.create(currentDocPath, javac);
    if(dtph == null) {
        return null;
    }
    DocSourcePositions sp = (DocSourcePositions) javac.getTrees().getSourcePositions();
    int start = (int) sp.getStartPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), node);
    int end = (int) sp.getEndPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), node);
    if(returnType == null) {
        errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, WRONG_CONSTRUCTOR_RETURN_DESC(),
                new RemoveTagFix(dtph, "@return").toEditorFix()));
    } else if (returnType.getKind() == TypeKind.VOID) {
        errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, WRONG_RETURN_DESC(),
                new RemoveTagFix(dtph, "@return").toEditorFix()));
    } else if(returnTypeFound) {
        errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, DUPLICATE_RETURN_DESC(),
                new RemoveTagFix(dtph, "@return").toEditorFix()));
    } else {
        returnTypeFound = true;
    }
    super.visitReturn(node, errors);
    foundInheritDoc = oldInheritDoc;
    return null;
}
 
Example #6
Source File: Analyzer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
@Messages({"# {0} - Tag name",
           "TAG_UNKNOWN=Unknown HTML Tag: <{0}>",
           "# {0} - Tag name",
           "TAG_END_UNKNOWN=Unknown HTML End Tag: </{0}>",
           "TAG_SELF_CLOSING=Self-closing element not allowed"})
public Void visitStartElement(StartElementTree node, List<ErrorDescription> errors) {
    DocTreePath currentDocPath = getCurrentPath();
    DocTreePathHandle dtph = DocTreePathHandle.create(currentDocPath, javac);
    if(dtph == null) {
        return null;
    }
    DocSourcePositions sp = (DocSourcePositions) javac.getTrees().getSourcePositions();
    int start = (int) sp.getStartPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), node);
    int end = (int) sp.getEndPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), node);


    final Name treeName = node.getName();
    final HtmlTag t = HtmlTag.get(treeName);
    if (t == null) {
        errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, TAG_UNKNOWN(treeName)));
    } else {
        if(t.endKind != HtmlTag.EndKind.NONE) {
            tagStack.push(node);
        }
    }
    
    if (node.isSelfClosing()) {
        errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, TAG_SELF_CLOSING()));
    }
    
    return super.visitStartElement(node, errors);
}
 
Example #7
Source File: Messager.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private String getDiagSource(DocTreePath path) {
    if (path == null || path.getTreePath() == null) {
        return programName;
    }
    JavacTrees trees = JavacTrees.instance(context);
    DocSourcePositions sourcePositions = trees.getSourcePositions();
    CompilationUnitTree cu = path.getTreePath().getCompilationUnit();
    long spos = sourcePositions.getStartPosition(cu, path.getDocComment(), path.getLeaf());
    long lineNumber = cu.getLineMap().getLineNumber(spos);
    String fname = cu.getSourceFile().getName();
    String posString = fname + ":" + lineNumber;
    return posString;
}
 
Example #8
Source File: Messager.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private String getDiagSource(Element e) {
    if (e == null) {
        return programName;
    }
    JavacTrees trees = JavacTrees.instance(context);
    TreePath path = trees.getPath(e);
    DocSourcePositions sourcePositions = trees.getSourcePositions();
    JCTree tree = trees.getTree(e);
    CompilationUnitTree cu = path.getCompilationUnit();
    long spos = sourcePositions.getStartPosition(cu, tree);
    long lineNumber = cu.getLineMap().getLineNumber(spos);
    String fname = cu.getSourceFile().getName();
    String posString = fname + ":" + lineNumber;
    return posString;
}
 
Example #9
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public long getLineNumber(Element e) {
    TreePath path = getTreePath(e);
    if (path == null) { // maybe null if synthesized
        TypeElement encl = getEnclosingTypeElement(e);
        path = getTreePath(encl);
    }
    CompilationUnitTree cu = path.getCompilationUnit();
    LineMap lineMap = cu.getLineMap();
    DocSourcePositions spos = docTrees.getSourcePositions();
    long pos = spos.getStartPosition(cu, path.getLeaf());
    return lineMap.getLineNumber(pos);
}
 
Example #10
Source File: CompletionFilter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public DocSourcePositions getSourcePositions() {
    return delegate.getSourcePositions();
}
 
Example #11
Source File: Analyzer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
    @Messages({"# {0} - Tag Name", "TAG_END_NOT_PERMITTED=Invalid End Tag: </{0}>",
               "# {0} - Tag Name", "TAG_END_UNEXPECTED=Unexpected End Tag: </{0}>",
               "# {0} - Tag Name", "TAG_START_UNMATCHED=End Tag Missing: </{0}>"})
    public Void visitEndElement(EndElementTree node, List<ErrorDescription> errors) {
        DocTreePath currentDocPath = getCurrentPath();
        DocTreePathHandle dtph = DocTreePathHandle.create(currentDocPath, javac);
        if(dtph == null) {
            return null;
        }
        DocSourcePositions sp = (DocSourcePositions) javac.getTrees().getSourcePositions();
        int start = (int) sp.getStartPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), node);
        int end = (int) sp.getEndPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), node);

        final Name treeName = node.getName();
        final HtmlTag t = HtmlTag.get(treeName);
        if (t == null) {
             errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, TAG_END_UNKNOWN(treeName)));
        } else if (t.endKind == HtmlTag.EndKind.NONE) {
//            env.messages.error(HTML, node, "dc.tag.end.not.permitted", treeName);
            errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, TAG_END_NOT_PERMITTED(treeName)));
        } else {
            boolean done = false;
            while (!tagStack.isEmpty()) {
                StartElementTree startTree = tagStack.peek();
                Name tagName = startTree.getName();
                HtmlTag tag = HtmlTag.get(tagName);
                if (t == tag) {
                    tagStack.pop();
                    done = true;
                    break;
                } else if (tag.endKind != HtmlTag.EndKind.REQUIRED) {
                    tagStack.pop();
                } else {
                    boolean found = false;
                    for (StartElementTree set : tagStack) {
                        HtmlTag si = HtmlTag.get(set.getName());
                        if (si == t) {
                            found = true;
                            break;
                        }
                    }
                    if (found) {
                        int s = (int) sp.getStartPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), startTree);
                        int e = (int) sp.getEndPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), startTree);
                        errors.add(ErrorDescriptionFactory.forSpan(ctx, s, e, TAG_START_UNMATCHED(tagName)));
                        tagStack.pop();
                    } else {
                        errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, TAG_END_UNEXPECTED(treeName)));
                        done = true;
                        break;
                    }
                }
            }
            if (!done && tagStack.isEmpty()) {
                errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, TAG_END_UNEXPECTED(treeName)));
            }
        }
        return super.visitEndElement(node, errors);
    }
 
Example #12
Source File: Analyzer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
    @NbBundle.Messages({"# {0} - tag name", "# {1} - element type", "INVALID_TAG_DESC={0} tag cannot be used on {1}."})
    public Void visitParam(ParamTree tree, List<ErrorDescription> errors) {
        boolean oldInheritDoc = foundInheritDoc;
        DocTreePath currentDocPath = getCurrentPath();
        DocTreePathHandle dtph = DocTreePathHandle.create(currentDocPath, javac);
        if(dtph == null) {
            return null;
        }
        DocSourcePositions sp = (DocSourcePositions) javac.getTrees().getSourcePositions();
        int start = (int) sp.getStartPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), tree);
        int end = (int) sp.getEndPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), tree);
        if(ctx.isCanceled()) { return null; }
        boolean typaram = tree.isTypeParameter();
        switch (currentElement.getKind()) {
            case METHOD:
            case CONSTRUCTOR: {
                ExecutableElement ee = (ExecutableElement) currentElement;
                checkParamDeclared(tree, typaram ? ee.getTypeParameters() : ee.getParameters(), dtph, start, end, errors);
                break;
            }
            case CLASS:
            case INTERFACE: {
                TypeElement te = (TypeElement) currentElement;
                if (typaram) {
                    checkParamDeclared(tree, te.getTypeParameters(), dtph, start, end, errors);
                } else {
                    errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, INVALID_TAG_DESC("@param", currentElement.getKind()), new RemoveTagFix(dtph, "@param").toEditorFix())); //NOI18N
//                    env.messages.error(REFERENCE, tree, "dc.invalid.param");
                }
                break;
            }
            default:
                errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, INVALID_TAG_DESC("@param", currentElement.getKind()), new RemoveTagFix(dtph, "@param").toEditorFix())); //NOI18N
//                env.messages.error(REFERENCE, tree, "dc.invalid.param");
                break;
        }
        warnIfEmpty(tree, tree.getDescription());
        super.visitParam(tree, errors);
        foundInheritDoc = oldInheritDoc;
        return null;
    }
 
Example #13
Source File: Analyzer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
    public Void visitThrows(ThrowsTree tree, List<ErrorDescription> errors) {
        boolean oldInheritDoc = foundInheritDoc;
        ReferenceTree exName = tree.getExceptionName();
        DocTreePath refPath = new DocTreePath(getCurrentPath(), tree.getExceptionName());
        Element ex = javac.getDocTrees().getElement(refPath);
        Types types = javac.getTypes();
        Elements elements = javac.getElements();
        final TypeElement throwableEl = elements.getTypeElement("java.lang.Throwable");
        final TypeElement errorEl = elements.getTypeElement("java.lang.Error");
        final TypeElement runtimeEl = elements.getTypeElement("java.lang.RuntimeException");
        if(throwableEl == null || errorEl == null || runtimeEl == null) {
            LOG.warning("Broken java-platform, cannot resolve " + throwableEl == null? "java.lang.Throwable" : errorEl == null? "java.lang.Error" : "java.lang.RuntimeException"); //NOI18N
            return null;
        }
        TypeMirror throwable = throwableEl.asType();
        TypeMirror error = errorEl.asType();
        TypeMirror runtime = runtimeEl.asType();
        DocTreePath currentDocPath = getCurrentPath();
        DocTreePathHandle dtph = DocTreePathHandle.create(currentDocPath, javac);
        if(dtph == null) {
            return null;
        }
        DocSourcePositions sp = (DocSourcePositions) javac.getTrees().getSourcePositions();
        int start = (int) sp.getStartPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), tree);
        int end = (int) sp.getEndPosition(javac.getCompilationUnit(), currentDocPath.getDocComment(), tree);
        if (ex == null || (ex.asType().getKind() == TypeKind.DECLARED
                && types.isAssignable(ex.asType(), throwable))) {
            switch (currentElement.getKind()) {
                case CONSTRUCTOR:
                case METHOD:
                    if (ex == null || !(types.isAssignable(ex.asType(), error)
                            || types.isAssignable(ex.asType(), runtime))) {
                        ExecutableElement ee = (ExecutableElement) currentElement;
                        String fqn;
                        if (ex != null) {
                            fqn = ((TypeElement) ex).getQualifiedName().toString();
                        } else {
                            ExpressionTree referenceClass = javac.getTreeUtilities().getReferenceClass(new DocTreePath(currentDocPath, exName));
                            if(referenceClass == null) break;
                            fqn = referenceClass.toString();
                        }
                        checkThrowsDeclared(tree, ex, fqn, ee.getThrownTypes(), dtph, start, end, errors);
                    }
                    break;
                default:
//                        env.messages.error(REFERENCE, tree, "dc.invalid.throws");
            }
        } else {
//                env.messages.error(REFERENCE, tree, "dc.invalid.throws");
        }
        warnIfEmpty(tree, tree.getDescription());
        super.visitThrows(tree, errors);
        foundInheritDoc = oldInheritDoc;
        return null;
    }