Skip to content

Commit

Permalink
Allow methodPattern parameter to be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Sep 19, 2024
1 parent 37ac5f9 commit 8006395
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.java.search.UsesType;
Expand All @@ -37,9 +38,11 @@ public class FindInternalJavaxApis extends Recipe {

@Option(
displayName = "Method pattern",
description = "A method pattern that is used to find matching method invocations.",
example = "java.util.List add(..)"
description = "Optionally limit the search to declarations that match the provided method pattern.",
example = "java.util.List add(..)",
required = false
)
@Nullable
private final String methodPattern;

@Override
Expand All @@ -55,25 +58,27 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
Pattern javaxType = Pattern.compile(StringUtils.aspectjNameToPattern("javax..*"));
return Preconditions.check(new UsesType<>("javax..*", null), new MethodAccess.Matcher(methodPattern)
.asVisitor((ma, ctx) -> {
MethodCall call = ma.getTree();
JavaType.Method methodType = call.getMethodType();
if (methodType == null) {
return call;
}
if (methodType.getReturnType() != null && methodType.getReturnType().isAssignableFrom(javaxType)) {
insertRow(ma, ctx, methodType);
return SearchResult.found(call);
}
for (JavaType parameterType : methodType.getParameterTypes()) {
if (parameterType.isAssignableFrom(javaxType)) {
insertRow(ma, ctx, methodType);
return SearchResult.found(call);
}
}
return call;
})
return Preconditions.check(new UsesType<>("javax..*", null),
(methodPattern == null ? new MethodAccess.Matcher() : new MethodAccess.Matcher(methodPattern))
.asVisitor((ma, ctx) -> {
MethodCall call = ma.getTree();
JavaType.Method methodType = call.getMethodType();
//noinspection ConstantValue
if (methodType == null || methodType.getReturnType() == null || methodType.getReturnType() instanceof JavaType.Unknown) {
return call;
}
if (methodType.getReturnType().isAssignableFrom(javaxType)) {
insertRow(ma, ctx, methodType);
return SearchResult.found(call);
}
for (JavaType parameterType : methodType.getParameterTypes()) {
if (parameterType.isAssignableFrom(javaxType)) {
insertRow(ma, ctx, methodType);
return SearchResult.found(call);
}
}
return call;
})
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FindInternalJavaxApisTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new FindInternalJavaxApis("org.openrewrite..* *(..)"));
spec.recipe(new FindInternalJavaxApis(null));
}

@Test
Expand All @@ -35,7 +35,7 @@ void returnsJavaxApi() {
java(
"""
package org.openrewrite;
interface Api {
javax.xml.stream.StreamFilter test();
}
Expand All @@ -44,9 +44,9 @@ interface Api {
java(
"""
package org.openrewrite;
import javax.xml.stream.StreamFilter;
class Consumer {
void test(Api api) {
StreamFilter sf = api.test();
Expand All @@ -55,9 +55,9 @@ void test(Api api) {
""",
"""
package org.openrewrite;
import javax.xml.stream.StreamFilter;
class Consumer {
void test(Api api) {
StreamFilter sf = /*~~>*/api.test();
Expand All @@ -75,7 +75,7 @@ void usesJavaxApiInParameter() {
java(
"""
package org.openrewrite;
interface Api {
void test(javax.xml.stream.StreamFilter sf);
}
Expand All @@ -84,9 +84,9 @@ interface Api {
java(
"""
package org.openrewrite;
import javax.xml.stream.StreamFilter;
class Consumer {
void test(Api api, StreamFilter sf) {
api.test(sf);
Expand All @@ -95,9 +95,9 @@ void test(Api api, StreamFilter sf) {
""",
"""
package org.openrewrite;
import javax.xml.stream.StreamFilter;
class Consumer {
void test(Api api, StreamFilter sf) {
/*~~>*/api.test(sf);
Expand Down

0 comments on commit 8006395

Please sign in to comment.