Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FindComments should also match against Javadoc #4542

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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