Skip to content

Commit

Permalink
Update references to javadoc APIs after the introduction of Markdown …
Browse files Browse the repository at this point in the history
…doc comments

openjdk/jdk@0a58cff

#4415

PiperOrigin-RevId: 638661786
  • Loading branch information
cushon authored and Error Prone Team committed May 30, 2024
1 parent 5fef6e0 commit 2dde254
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ public static String getTextFromComment(ErrorProneComment comment) {
return comment.getText().replaceAll("^\\s*/\\*\\s*(.*?)\\s*\\*/\\s*", "$1");
case LINE:
return comment.getText().replaceAll("^\\s*//\\s*", "");
case JAVADOC:
case JAVADOC_BLOCK:
return comment.getText().replaceAll("^\\s*/\\*\\*\\s*(.*?)\\s*\\*/\\s*", "$1");
default:
}
throw new AssertionError(comment.getStyle());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public final class ErrorProneComment {
private final int endPos;
private final int offset;
private final Supplier<String> text;
private final CommentStyle style;
private final ErrorProneCommentStyle style;

ErrorProneComment(int pos, int endPos, int offset, Supplier<String> text, CommentStyle style) {
ErrorProneComment(
int pos, int endPos, int offset, Supplier<String> text, ErrorProneCommentStyle style) {
this.pos = pos;
this.endPos = endPos;
this.offset = offset;
Expand Down Expand Up @@ -67,7 +68,31 @@ public int getSourcePos(int index) {
return pos + index + offset;
}

public CommentStyle getStyle() {
/** A compatibility wrapper for {@link CommentStyle}. */
public enum ErrorProneCommentStyle {
LINE,
BLOCK,
JAVADOC_LINE,
JAVADOC_BLOCK;

static ErrorProneCommentStyle from(CommentStyle style) {
switch (style.name()) {
case "LINE":
return ErrorProneCommentStyle.LINE;
case "BLOCK":
return ErrorProneCommentStyle.BLOCK;
case "JAVADOC_LINE":
return ErrorProneCommentStyle.JAVADOC_LINE;
case "JAVADOC":
case "JAVADOC_BLOCK":
return ErrorProneCommentStyle.JAVADOC_BLOCK;
default:
throw new AssertionError(style);
}
}
}

public ErrorProneCommentStyle getStyle() {
return style;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ protected Comment processComment(int pos, int endPos, CommentStyle style) {
AccessibleReader reader = new AccessibleReader(fac, buf, buf.length);
ErrorProneComment errorProneComment =
new ErrorProneComment(
pos, endPos, /* offset= */ 0, () -> new String(reader.getRawCharacters()), style);
pos,
endPos,
/* offset= */ 0,
() -> new String(reader.getRawCharacters()),
ErrorProneComment.ErrorProneCommentStyle.from(style));
comments.put(comment, errorProneComment);
return comment;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.util.ASTHelpers.getStartPosition;
import static com.sun.tools.javac.parser.Tokens.Comment.CommentStyle.BLOCK;

import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableSet;
Expand All @@ -35,6 +34,7 @@
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.google.errorprone.util.Comments;
import com.google.errorprone.util.ErrorProneComment.ErrorProneCommentStyle;
import com.google.errorprone.util.ErrorProneToken;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
Expand Down Expand Up @@ -130,7 +130,7 @@ private void checkParameter(

private static boolean hasParameterComment(ErrorProneToken token) {
return token.comments().stream()
.filter(c -> c.getStyle() == BLOCK)
.filter(c -> c.getStyle() == ErrorProneCommentStyle.BLOCK)
.anyMatch(
c ->
NamedParameterComment.PARAMETER_COMMENT_PATTERN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.google.errorprone.util.ASTHelpers;
import com.google.errorprone.util.Comments;
import com.google.errorprone.util.ErrorProneComment;
import com.google.errorprone.util.ErrorProneComment.ErrorProneCommentStyle;
import com.google.errorprone.util.ErrorProneToken;
import com.google.errorprone.util.ErrorProneTokens;
import com.sun.source.tree.ExpressionTree;
Expand All @@ -46,7 +47,6 @@
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
import com.sun.tools.javac.util.Position;
import java.util.ArrayDeque;
import java.util.ArrayList;
Expand Down Expand Up @@ -199,7 +199,7 @@ private void checkArgument(
VarSymbol formal, ExpressionTree actual, ErrorProneToken token, VisitorState state) {
List<FixInfo> matches = new ArrayList<>();
for (ErrorProneComment comment : token.comments()) {
if (comment.getStyle().equals(CommentStyle.LINE)) {
if (comment.getStyle().equals(ErrorProneCommentStyle.LINE)) {
// These are usually not intended as a parameter comment, and we don't want to flag if they
// happen to match the parameter comment format.
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import com.google.errorprone.util.Commented;
import com.google.errorprone.util.Comments;
import com.google.errorprone.util.ErrorProneComment;
import com.google.errorprone.util.ErrorProneComment.ErrorProneCommentStyle;
import com.sun.source.tree.ExpressionTree;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
import java.util.Arrays;
import java.util.Optional;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -107,7 +107,8 @@ private static boolean isApproximateMatchingComment(ErrorProneComment comment, S
static MatchedComment match(Commented<ExpressionTree> actual, String formal) {
Optional<ErrorProneComment> lastBlockComment =
Streams.findLast(
actual.beforeComments().stream().filter(c -> c.getStyle() == CommentStyle.BLOCK));
actual.beforeComments().stream()
.filter(c -> c.getStyle() == ErrorProneCommentStyle.BLOCK));

if (lastBlockComment.isPresent()) {
Matcher m =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static com.google.errorprone.util.ASTHelpers.getStartPosition;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ErrorProneTokens.getTokens;
import static com.sun.tools.javac.parser.Tokens.Comment.CommentStyle.JAVADOC;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableRangeSet;
Expand All @@ -35,6 +34,7 @@
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.google.errorprone.util.ErrorProneComment;
import com.google.errorprone.util.ErrorProneComment.ErrorProneCommentStyle;
import com.google.errorprone.util.ErrorProneToken;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
Expand Down Expand Up @@ -62,7 +62,8 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s
ImmutableRangeSet<Integer> suppressedRegions = suppressedRegions(state);
for (ErrorProneToken token : getTokens(state.getSourceCode().toString(), state.context)) {
for (ErrorProneComment comment : token.comments()) {
if (!comment.getStyle().equals(JAVADOC) || comment.getText().equals("/**/")) {
if (!comment.getStyle().equals(ErrorProneCommentStyle.JAVADOC_BLOCK)
|| comment.getText().equals("/**/")) {
continue;
}
if (javadocableTrees.containsKey(token.pos())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
import com.sun.source.util.DocTreePath;
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.tree.DCTree.DCDocComment;
import com.sun.tools.javac.tree.DocCommentTable;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.Position;
import java.lang.reflect.Method;
import java.util.Optional;
import javax.annotation.Nullable;

Expand All @@ -53,8 +55,26 @@ static Optional<String> getBestMatch(String to, int maxEditDistance, Iterable<St
}

static DCDocComment getDocComment(VisitorState state, Tree tree) {
return ((JCCompilationUnit) state.getPath().getCompilationUnit())
.docComments.getCommentTree((JCTree) tree);
return getCommentTree(
((JCCompilationUnit) state.getPath().getCompilationUnit()).docComments, (JCTree) tree);
}

private static final Method COMMENT_TREE_METHOD = getCommentTreeMethod();

private static Method getCommentTreeMethod() {
try {
return DocCommentTable.class.getMethod("getCommentTree", JCTree.class);
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}

private static DCDocComment getCommentTree(DocCommentTable docCommentTable, JCTree tree) {
try {
return (DCDocComment) COMMENT_TREE_METHOD.invoke(docCommentTable, tree);
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}

static SuggestedFix replace(DocTree docTree, String replacement, VisitorState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import com.sun.source.util.DocTreePathScanner;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.tree.DCTree;
import com.sun.tools.javac.tree.DCTree.DCDocComment;
import com.sun.tools.javac.tree.DocCommentTable;
import com.sun.tools.javac.tree.JCTree;
import java.io.IOException;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -906,8 +908,9 @@ public static class JavadocQualifier extends BugChecker implements BugChecker.Cl
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
DCTree.DCDocComment comment =
((JCTree.JCCompilationUnit) state.getPath().getCompilationUnit())
.docComments.getCommentTree((JCTree) tree);
getCommentTree(
((JCTree.JCCompilationUnit) state.getPath().getCompilationUnit()).docComments,
(JCTree) tree);
if (comment == null) {
return Description.NO_MATCH;
}
Expand All @@ -927,6 +930,17 @@ public Void visitLink(LinkTree node, Void unused) {
}
}

private static DCDocComment getCommentTree(DocCommentTable docCommentTable, JCTree tree) {
try {
return (DCDocComment)
DocCommentTable.class
.getMethod("getCommentTree", JCTree.class)
.invoke(docCommentTable, tree);
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}

@Test
public void qualifyJavadocTest() {
BugCheckerRefactoringTestHelper.newInstance(JavadocQualifier.class, getClass())
Expand Down

0 comments on commit 2dde254

Please sign in to comment.