Skip to content

Commit

Permalink
FindComments should also match against Javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Oct 2, 2024
1 parent 7e20574 commit 452d878
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.test.RewriteTest;

import java.util.Arrays;
import java.util.List;

import static org.openrewrite.java.Assertions.java;

Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,16 +77,31 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
.collect(Collectors.toList());

return new JavaIsoVisitor<ExecutionContext>() {

private final JavadocVisitor<ExecutionContext> javadocVisitor = new JavadocVisitor<ExecutionContext>(this) {
@Override
public Javadoc visitText(Javadoc.Text text, ExecutionContext ctx) {
return match(text, text.getText());
}
};

@Override
protected JavadocVisitor<ExecutionContext> 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;
}));
Expand All @@ -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 extends Tree> 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;
}
};
}
Expand Down

0 comments on commit 452d878

Please sign in to comment.