Java Code Examples for com.sun.source.util.DocSourcePositions#getEndPosition()

The following examples show how to use com.sun.source.util.DocSourcePositions#getEndPosition() . 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: 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 4
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 5
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 6
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 7
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;
    }