From ec622aaa05f351a2e2cd3f3968a93aff7575c74a Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Wed, 2 Oct 2024 10:57:08 +0200 Subject: [PATCH] `FindComments` should also match against Javadoc (#4542) --- .../openrewrite/java/FindCommentsTest.java | 24 ++++++++++ .../openrewrite/java/search/FindComments.java | 45 ++++++++++++++----- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/FindCommentsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/FindCommentsTest.java index ca5d5d3fbb4..980f7d4c09b 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/FindCommentsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/FindCommentsTest.java @@ -22,6 +22,7 @@ import org.openrewrite.test.RewriteTest; import java.util.Arrays; +import java.util.List; import static org.openrewrite.java.Assertions.java; @@ -59,6 +60,29 @@ class Test { ); } + @Test + void findInJavadoc() { + rewriteRun( + spec -> spec.recipe(new FindComments(List.of("foo"))), + java( + """ + /** Example with a {@code foo} in Javadoc. + * Here another foo. + */ + class Test { + } + """, + """ + /** Example with a {@code ~~>foo} in Javadoc. + *~~> Here another foo. + */ + class Test { + } + """ + ) + ); + } + @Test void findSecrets() { rewriteRun( diff --git a/rewrite-java/src/main/java/org/openrewrite/java/search/FindComments.java b/rewrite-java/src/main/java/org/openrewrite/java/search/FindComments.java index 7d9db9bb669..a541ed4f5f1 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/search/FindComments.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/search/FindComments.java @@ -17,13 +17,12 @@ import lombok.EqualsAndHashCode; import lombok.Value; +import org.jspecify.annotations.Nullable; import org.openrewrite.*; import org.openrewrite.internal.ListUtils; import org.openrewrite.java.JavaIsoVisitor; -import org.openrewrite.java.tree.J; -import org.openrewrite.java.tree.JavaType; -import org.openrewrite.java.tree.Space; -import org.openrewrite.java.tree.TextComment; +import org.openrewrite.java.JavadocVisitor; +import org.openrewrite.java.tree.*; import org.openrewrite.marker.SearchResult; import java.util.List; @@ -78,16 +77,31 @@ public TreeVisitor getVisitor() { .collect(Collectors.toList()); return new JavaIsoVisitor() { + + private final JavadocVisitor javadocVisitor = new JavadocVisitor(this) { + @Override + public Javadoc visitText(Javadoc.Text text, ExecutionContext ctx) { + return match(text, text.getText()); + } + }; + + @Override + protected JavadocVisitor getJavadocVisitor() { + return javadocVisitor; + } + @Override public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) { return space.withComments(ListUtils.map(space.getComments(), comment -> { - if(comment instanceof TextComment) { + if (comment instanceof TextComment) { for (Pattern p : compiledPatterns) { if (p.matcher(((TextComment) comment).getText()).find()) { return comment.withMarkers(comment.getMarkers(). computeByType(new SearchResult(randomId(), null), (s1, s2) -> s1 == null ? s2 : s1)); } } + } else if (comment instanceof Javadoc.DocComment) { + return (Comment) getJavadocVisitor().visitDocComment((Javadoc.DocComment) comment, ctx); } return comment; })); @@ -99,15 +113,26 @@ public J.Literal visitLiteral(J.Literal literal, ExecutionContext ctx) { return literal; } + J.Literal matched = literal.getValue() != null ? match(literal, literal.getValue().toString()) : literal; + if (matched != literal) { + return matched; + } + + return match(literal, literal.getValueSource()); + } + + private T match(T t, @Nullable String value) { + if (value == null) { + return t; + } + for (Pattern p : compiledPatterns) { - if (literal.getValue() != null && p.matcher(literal.getValue().toString()).find()) { - return SearchResult.found(literal); - } else if (literal.getValueSource() != null && p.matcher(literal.getValueSource()).find()) { - return SearchResult.found(literal); + if (p.matcher(value).find()) { + return SearchResult.found(t); } } - return literal; + return t; } }; }