diff --git a/src/main/java/org/openrewrite/java/migrate/search/FindInternalJavaxApis.java b/src/main/java/org/openrewrite/java/migrate/search/FindInternalJavaxApis.java index e18f88713e..3664d465d3 100644 --- a/src/main/java/org/openrewrite/java/migrate/search/FindInternalJavaxApis.java +++ b/src/main/java/org/openrewrite/java/migrate/search/FindInternalJavaxApis.java @@ -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; @@ -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 @@ -55,25 +58,27 @@ public String getDescription() { @Override public TreeVisitor 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; + }) ); } diff --git a/src/test/java/org/openrewrite/java/migrate/search/FindInternalJavaxApisTest.java b/src/test/java/org/openrewrite/java/migrate/search/FindInternalJavaxApisTest.java index 2e26dc3d52..ddc7a3cccc 100644 --- a/src/test/java/org/openrewrite/java/migrate/search/FindInternalJavaxApisTest.java +++ b/src/test/java/org/openrewrite/java/migrate/search/FindInternalJavaxApisTest.java @@ -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 @@ -35,7 +35,7 @@ void returnsJavaxApi() { java( """ package org.openrewrite; - + interface Api { javax.xml.stream.StreamFilter test(); } @@ -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(); @@ -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(); @@ -75,7 +75,7 @@ void usesJavaxApiInParameter() { java( """ package org.openrewrite; - + interface Api { void test(javax.xml.stream.StreamFilter sf); } @@ -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); @@ -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);