@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); }
/** 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); }
/** * 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")); }
/** * 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); }
/** * Fully qualifies a javadoc reference, e.g. for replacing {@code {@link List}} with {@code {@link * java.util.List}} * * @param fix the fix builder to add to * @param docPath the path to a {@link DCTree.DCReference} element */ public static void qualifyDocReference( SuggestedFix.Builder fix, DocTreePath docPath, VisitorState state) { DocTree leaf = docPath.getLeaf(); checkArgument( leaf.getKind() == DocTree.Kind.REFERENCE, "expected a path to a reference, got %s instead", leaf.getKind()); DCTree.DCReference reference = (DCTree.DCReference) leaf; Symbol sym = (Symbol) JavacTrees.instance(state.context).getElement(docPath); if (sym == null) { return; } String refString = reference.toString(); String qualifiedName; int idx = refString.indexOf('#'); if (idx >= 0) { qualifiedName = sym.owner.getQualifiedName() + refString.substring(idx, refString.length()); } else { qualifiedName = sym.getQualifiedName().toString(); } replaceDocTree(fix, docPath, qualifiedName); }
private Optional<Description> generateFix(String replacement) { int startPosition = getStartPosition(getCurrentPath().getLeaf(), state); if (emittedFixes.contains(startPosition)) { // We already emitted a fix surrounding this location. return Optional.empty(); } Range<Integer> containingPre = preTags.rangeContaining(startPosition); if (containingPre == null) { return reportAll ? Optional.of(replacementFix(replacement)) : Optional.empty(); } if (emittedFixes.intersects(containingPre)) { return Optional.empty(); } emittedFixes.add(containingPre); SuggestedFix fix = wrapInCodeTag(containingPre); return Optional.of( buildDescription(diagnosticPosition(getCurrentPath(), state)) .setMessage( "This HTML entity is invalid. Enclosing the code in this <pre>/<code> tag with" + " a {@code } block will force Javadoc to interpret HTML literally.") .addFix(fix) .build()); }
private Description replacementFix(String replacement) { return buildDescription(diagnosticPosition(getCurrentPath(), state)) .addFix(replace(getCurrentPath().getLeaf(), replacement, state)) .build(); } }
/** * Scan a tree from a position identified by a TreePath. */ public R scan(DocTreePath path, P p) { this.path = path; try { return path.getLeaf().accept(this, p); } finally { this.path = null; } }
/** * Scan a tree from a position identified by a TreePath. */ public R scan(DocTreePath path, P p) { this.path = path; try { return path.getLeaf().accept(this, p); } finally { this.path = null; } }
public static boolean isBlockTag(DocTreePath tag) { return BLOCK_TAGS.contains(tag.getLeaf().getKind()); }
/** * 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); return new DiagnosticPosition() { @Override public JCTree getTree() { return (JCTree) path.getTreePath().getLeaf(); } @Override public int getStartPosition() { return startPosition; } @Override public int getPreferredPosition() { return startPosition; } @Override public int getEndPosition(EndPosTable endPosTable) { return startPosition; } }; }
KindPath(DocTreePath treePath) { while (treePath != null) { kindPath.add(treePath.getLeaf().getKind()); treePath = treePath.getParentPath(); } }
@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; }
if (path.getLeaf() == target) { return path;
/**Find the name (the name after {@code #}) that is being referenced by the given {@link ReferenceTree}. * * @param path the leaf must be {@link ReferenceTree} * @return the referred member name, or {@code null} if none. * @since 0.124 */ public @CheckForNull Name getReferenceName(@NonNull DocTreePath path) { return ((DCReference) path.getLeaf()).memberName; }
@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; }
/** 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); }
/**Find the type (the part before {@code #}) that is being referenced by the given {@link ReferenceTree}. * * @param path the leaf must be {@link ReferenceTree} * @return the referred type, or {@code null} if none. * @since 0.124 */ public @CheckForNull ExpressionTree getReferenceClass(@NonNull DocTreePath path) { TreePath tp = path.getTreePath(); DCReference ref = (DCReference) path.getLeaf(); ((JavacTrees) this.info.getTrees()).ensureDocReferenceAttributed(tp, ref); return (ExpressionTree) ref.qualifierExpression; }
/**Find the parameters that are specified in the given {@link ReferenceTree}. * * @param path the leaf must be {@link ReferenceTree} * @return the parameters for the referred method, or {@code null} if none. * @since 0.124 */ public @CheckForNull List<? extends Tree> getReferenceParameters(@NonNull DocTreePath path) { TreePath tp = path.getTreePath(); DCReference ref = (DCReference) path.getLeaf(); ((JavacTrees) this.info.getTrees()).ensureDocReferenceAttributed(tp, ref); return ref.paramTypes; }
/** * 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)); }