From 2dde25434696cc2f87e76b64bf60d8875da36a09 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 30 May 2024 08:27:37 -0700 Subject: [PATCH] Update references to javadoc APIs after the introduction of Markdown doc comments https://github.com/openjdk/jdk/commit/0a58cffe88ba823e71fcdcca64b784ed04ca5398 https://github.com/google/error-prone/issues/4415 PiperOrigin-RevId: 638661786 --- .../com/google/errorprone/util/Comments.java | 3 +- .../errorprone/util/ErrorProneComment.java | 31 +++++++++++++++++-- .../errorprone/util/ErrorProneTokens.java | 6 +++- .../bugpatterns/BooleanParameter.java | 4 +-- .../errorprone/bugpatterns/ParameterName.java | 4 +-- .../NamedParameterComment.java | 5 +-- .../bugpatterns/javadoc/NotJavadoc.java | 5 +-- .../errorprone/bugpatterns/javadoc/Utils.java | 24 ++++++++++++-- .../errorprone/fixes/SuggestedFixesTest.java | 18 +++++++++-- 9 files changed, 83 insertions(+), 17 deletions(-) diff --git a/check_api/src/main/java/com/google/errorprone/util/Comments.java b/check_api/src/main/java/com/google/errorprone/util/Comments.java index 3b7ae68fc20..49dcf230e7d 100644 --- a/check_api/src/main/java/com/google/errorprone/util/Comments.java +++ b/check_api/src/main/java/com/google/errorprone/util/Comments.java @@ -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()); } diff --git a/check_api/src/main/java/com/google/errorprone/util/ErrorProneComment.java b/check_api/src/main/java/com/google/errorprone/util/ErrorProneComment.java index ffc6b76fcf1..28de1b2ca42 100644 --- a/check_api/src/main/java/com/google/errorprone/util/ErrorProneComment.java +++ b/check_api/src/main/java/com/google/errorprone/util/ErrorProneComment.java @@ -30,9 +30,10 @@ public final class ErrorProneComment { private final int endPos; private final int offset; private final Supplier text; - private final CommentStyle style; + private final ErrorProneCommentStyle style; - ErrorProneComment(int pos, int endPos, int offset, Supplier text, CommentStyle style) { + ErrorProneComment( + int pos, int endPos, int offset, Supplier text, ErrorProneCommentStyle style) { this.pos = pos; this.endPos = endPos; this.offset = offset; @@ -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; } diff --git a/check_api/src/main/java/com/google/errorprone/util/ErrorProneTokens.java b/check_api/src/main/java/com/google/errorprone/util/ErrorProneTokens.java index b94bb10d801..a391e2659aa 100644 --- a/check_api/src/main/java/com/google/errorprone/util/ErrorProneTokens.java +++ b/check_api/src/main/java/com/google/errorprone/util/ErrorProneTokens.java @@ -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; } diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/BooleanParameter.java b/core/src/main/java/com/google/errorprone/bugpatterns/BooleanParameter.java index c06d740e663..b55eacd8b5c 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/BooleanParameter.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/BooleanParameter.java @@ -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; @@ -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; @@ -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 diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ParameterName.java b/core/src/main/java/com/google/errorprone/bugpatterns/ParameterName.java index 8cd6485ab3d..2dbd722854b 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ParameterName.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ParameterName.java @@ -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; @@ -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; @@ -199,7 +199,7 @@ private void checkArgument( VarSymbol formal, ExpressionTree actual, ErrorProneToken token, VisitorState state) { List 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; diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/argumentselectiondefects/NamedParameterComment.java b/core/src/main/java/com/google/errorprone/bugpatterns/argumentselectiondefects/NamedParameterComment.java index e14f50c6470..f60a7597b63 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/argumentselectiondefects/NamedParameterComment.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/argumentselectiondefects/NamedParameterComment.java @@ -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; @@ -107,7 +107,8 @@ private static boolean isApproximateMatchingComment(ErrorProneComment comment, S static MatchedComment match(Commented actual, String formal) { Optional 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 = diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/NotJavadoc.java b/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/NotJavadoc.java index 2e91989e091..776c7ef39c3 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/NotJavadoc.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/NotJavadoc.java @@ -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; @@ -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; @@ -62,7 +62,8 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s ImmutableRangeSet 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())) { diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/Utils.java b/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/Utils.java index f18b860d82d..d4d48255880 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/Utils.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/Utils.java @@ -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; @@ -53,8 +55,26 @@ static Optional getBestMatch(String to, int maxEditDistance, Iterable