@Nullable static DocTreePath getDocTreePath(VisitorState state) { DocCommentTree docCommentTree = getDocCommentTree(state); if (docCommentTree == null) { return null; } return new DocTreePath(state.getPath(), docCommentTree); }
@Nullable private static <T> T findFirst(DocTreePath docTreePath, Class<T> clazz) { return new DocTreeScanner<T, Void>() { @Override public T scan(DocTree docTree, Void unused) { if (clazz.isInstance(docTree)) { return clazz.cast(docTree); } return super.scan(docTree, null); } }.scan(docTreePath.getLeaf(), null); }
/** * Gets a {@link DiagnosticPosition} for the {@link DocTree} pointed to by {@code path}, attached * to the {@link Tree} which it documents. */ static DiagnosticPosition diagnosticPosition(DocTreePath path, VisitorState state) { int startPosition = getStartPosition(path.getLeaf(), state); Tree tree = path.getTreePath().getLeaf(); return getDiagnosticPosition(startPosition, tree); }
/** * When we have an erroneous block tag inside a {@literal @}code tag, the enclosing * {@literal @}code tag will fail to parse. So, we're looking for an enclosing erroneous tag. */ private boolean parentIsErroneousCodeTag() { if (getCurrentPath().getParentPath() == null) { return false; } DocTree parentDoc = getCurrentPath().getParentPath().getLeaf(); if (!(parentDoc instanceof DCDocComment)) { return false; } DCDocComment dcDocComment = (DCDocComment) parentDoc; return dcDocComment.getFullBody().stream() .anyMatch( dc -> dc instanceof DCErroneous && ((DCErroneous) dc).body.startsWith("{@code")); }
/** Replaces the leaf doctree in the given path with {@code replacement}. */ public static void replaceDocTree( SuggestedFix.Builder fix, DocTreePath docPath, String replacement) { DocTree leaf = docPath.getLeaf(); checkArgument( leaf instanceof DCTree.DCEndPosTree, "no end position information for %s", leaf.getKind()); DCTree.DCEndPosTree<?> node = (DCTree.DCEndPosTree<?>) leaf; DCTree.DCDocComment comment = (DCTree.DCDocComment) docPath.getDocComment(); fix.replace((int) node.getSourcePosition(comment), node.getEndPos(comment), replacement); }
/** * Factory method for creating {@link DocTreePathHandle}. * * @param docTreePath for which the {@link DocTreePathHandle} should be created. * @param javac * @return a new {@link DocTreePathHandle} * @throws java.lang.IllegalArgumentException if arguments are not supported */ public static DocTreePathHandle create(final DocTreePath docTreePath, CompilationInfo javac) throws IllegalArgumentException { Parameters.notNull("docTreePath", docTreePath); Parameters.notNull("javac", javac); TreePathHandle treePathHandle = TreePathHandle.create(docTreePath.getTreePath(), javac); if(treePathHandle.getFileObject() == null) { return null; } int position = (int) ((DCTree) docTreePath.getLeaf()).getSourcePosition((DCTree.DCDocComment)docTreePath.getDocComment()); if (position == (-1)) { DocTree docTree = docTreePath.getLeaf(); if(docTree == docTreePath.getDocComment()) { return new DocTreePathHandle(new DocCommentDelegate(treePathHandle)); } int index = listChildren(docTreePath.getParentPath().getLeaf()).indexOf(docTree); assert index != (-1); return new DocTreePathHandle(new CountingDelegate(treePathHandle, index, docTreePath.getLeaf().getKind())); } Position pos = createPositionRef(treePathHandle.getFileObject(), position, Bias.Forward); return new DocTreePathHandle(new DocTreeDelegate(pos, new DocTreeDelegate.KindPath(docTreePath), treePathHandle)); }
@Override public Element getElement(DocTreePath path) { DocTree forTree = path.getLeaf(); if (forTree instanceof DCReference) return attributeDocReference(path.getTreePath(), ((DCReference) forTree)); if (forTree instanceof DCIdentifier) { if (path.getParentPath().getLeaf() instanceof DCParam) { return attributeParamIdentifier(path.getTreePath(), (DCParam) path.getParentPath().getLeaf()); } } return null; }
new PathFinder(pos, sourcePositions).scan(path, path.getTreePath()); } catch (Result result) { path = result.path; if (path.getLeaf() == path.getDocComment()) return path;
private void handleDanglingParams(TextTree node) { Matcher matcher = PARAM_MATCHER.matcher(node.getBody()); Comment comment = ((DCDocComment) getCurrentPath().getDocComment()).comment; while (matcher.find()) { int startPos = comment.getSourcePos(((DCText) node).pos + matcher.start()); int endPos = comment.getSourcePos(((DCText) node).pos + matcher.end()); String paramName = matcher.group(1); SuggestedFix fix = SuggestedFix.replace(startPos, endPos, String.format("{@code %s}", paramName)); state.reportMatch( buildDescription( getDiagnosticPosition(startPos, getCurrentPath().getTreePath().getLeaf())) .addFix(fix) .build()); } }
/** * Gets a documentation tree path for a tree node within a compilation unit. * @return null if the node is not found */ public static DocTreePath getPath(TreePath treePath, DocCommentTree doc, DocTree target) { return getPath(new DocTreePath(treePath, doc), target); }
}.visit(getCurrentPath().getTreePath().getLeaf(), null); return super.visitInheritDoc(inheritDocTree, null);
@Override public Void visitStartElement(StartElementTree startElementTree, Void aVoid) { if (startElementTree.getName().toString().equalsIgnoreCase("p")) { DCStartElement node = (DCStartElement) startElementTree; DocTreePath path = getCurrentPath(); int start = (int) node.getSourcePosition((DCDocComment) path.getDocComment()) + node.pos; int end = node.getEndPos((DCDocComment) getCurrentPath().getDocComment()); fixes.add(Optional.of(SuggestedFix.replace(start, end, ""))); // int endPos = startPos + node.getSignature().length(); // String oldName = node.getSignature(); // int idx = oldName.indexOf('#'); // if (idx != -1) { // startPos += idx + 1; // } // state.reportMatch( // describeMatch( // state.getPath().getLeaf(), SuggestedFix.replace(startPos, endPos, with))); // int start = element.pos; // fixes.add(Optional.of(SuggestedFix.replace(start, element.getEndPos()))); } return super.visitStartElement(startElementTree, aVoid); }
private DocTreePath resolvePathForPos(CompilationInfo javac, TreePath treePath, DocCommentTree doc, int pos) { DocTreePath tp = javac.getTreeUtilities().pathFor(treePath, doc, pos); while (tp != null) { KindPath kindPath1 = new KindPath(tp); kindPath.getList().remove(Tree.Kind.ERRONEOUS); if (kindPath1.equals(kindPath)) { return tp; } tp = tp.getParentPath(); } return null; }
@Override public Element getElement(DocTreePath path) { DocTree forTree = path.getLeaf(); if (forTree instanceof DCReference) return attributeDocReference(path.getTreePath(), ((DCReference) forTree)); if (forTree instanceof DCIdentifier) { if (path.getParentPath().getLeaf() instanceof DCParam) { return attributeParamIdentifier(path.getTreePath(), (DCParam) path.getParentPath().getLeaf()); } } return null; }
private void handleMalformedTags(TextTree node) { Matcher matcher = misplacedCurly.matcher(node.getBody()); Comment comment = ((DCDocComment) getCurrentPath().getDocComment()).comment; while (matcher.find()) { int beforeAt = comment.getSourcePos(((DCText) node).pos + matcher.start()); int startOfCurly = comment.getSourcePos(((DCText) node).pos + matcher.end(1)); SuggestedFix fix = SuggestedFix.builder() .replace(beforeAt, beforeAt, "{") .replace(startOfCurly, startOfCurly + 1, " ") .build(); state.reportMatch( buildDescription( getDiagnosticPosition(beforeAt, getCurrentPath().getTreePath().getLeaf())) .addFix(fix) .build()); } }
/** Replaces the leaf doctree in the given path with {@code replacement}. */ public static void replaceDocTree( SuggestedFix.Builder fix, DocTreePath docPath, String replacement) { DocTree leaf = docPath.getLeaf(); checkArgument( leaf instanceof DCTree.DCEndPosTree, "no end position information for %s", leaf.getKind()); DCTree.DCEndPosTree<?> node = (DCTree.DCEndPosTree<?>) leaf; DCTree.DCDocComment comment = (DCTree.DCDocComment) docPath.getDocComment(); fix.replace((int) node.getSourcePosition(comment), node.getEndPos(comment), replacement); }
KindPath(DocTreePath treePath) { while (treePath != null) { kindPath.add(treePath.getLeaf().getKind()); treePath = treePath.getParentPath(); } }
/** * Gets a documentation tree path for a tree node within a compilation unit. * @return null if the node is not found */ public static DocTreePath getPath(TreePath treePath, DocCommentTree doc, DocTree target) { return getPath(new DocTreePath(treePath, doc), target); }
@Override public JCTree getTree() { return (JCTree) path.getTreePath().getLeaf(); }
private Description generateSeeFix(DocTreePath docTreePath, SeeTree seeTree, VisitorState state) { int pos = ((DCDocComment) docTreePath.getDocComment()).comment.getSourcePos(0); SuggestedFix fix = SuggestedFix.builder() .merge(Utils.replace(seeTree, "", state)) .replace( pos, pos, String.format( "See {@link %s}.\n", seeTree.getReference().stream().map(Object::toString).collect(joining(" ")))) .build(); return buildDescription(diagnosticPosition(docTreePath, state)) .setMessage(String.format(CONSIDER_USING_MESSAGE, "see")) .addFix(fix) .build(); }