Skip to content

Commit 8006395

Browse files
committed
Allow methodPattern parameter to be optional
1 parent 37ac5f9 commit 8006395

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

src/main/java/org/openrewrite/java/migrate/search/FindInternalJavaxApis.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import lombok.EqualsAndHashCode;
1919
import lombok.RequiredArgsConstructor;
20+
import org.jspecify.annotations.Nullable;
2021
import org.openrewrite.*;
2122
import org.openrewrite.internal.StringUtils;
2223
import org.openrewrite.java.search.UsesType;
@@ -37,9 +38,11 @@ public class FindInternalJavaxApis extends Recipe {
3738

3839
@Option(
3940
displayName = "Method pattern",
40-
description = "A method pattern that is used to find matching method invocations.",
41-
example = "java.util.List add(..)"
41+
description = "Optionally limit the search to declarations that match the provided method pattern.",
42+
example = "java.util.List add(..)",
43+
required = false
4244
)
45+
@Nullable
4346
private final String methodPattern;
4447

4548
@Override
@@ -55,25 +58,27 @@ public String getDescription() {
5558
@Override
5659
public TreeVisitor<?, ExecutionContext> getVisitor() {
5760
Pattern javaxType = Pattern.compile(StringUtils.aspectjNameToPattern("javax..*"));
58-
return Preconditions.check(new UsesType<>("javax..*", null), new MethodAccess.Matcher(methodPattern)
59-
.asVisitor((ma, ctx) -> {
60-
MethodCall call = ma.getTree();
61-
JavaType.Method methodType = call.getMethodType();
62-
if (methodType == null) {
63-
return call;
64-
}
65-
if (methodType.getReturnType() != null && methodType.getReturnType().isAssignableFrom(javaxType)) {
66-
insertRow(ma, ctx, methodType);
67-
return SearchResult.found(call);
68-
}
69-
for (JavaType parameterType : methodType.getParameterTypes()) {
70-
if (parameterType.isAssignableFrom(javaxType)) {
71-
insertRow(ma, ctx, methodType);
72-
return SearchResult.found(call);
73-
}
74-
}
75-
return call;
76-
})
61+
return Preconditions.check(new UsesType<>("javax..*", null),
62+
(methodPattern == null ? new MethodAccess.Matcher() : new MethodAccess.Matcher(methodPattern))
63+
.asVisitor((ma, ctx) -> {
64+
MethodCall call = ma.getTree();
65+
JavaType.Method methodType = call.getMethodType();
66+
//noinspection ConstantValue
67+
if (methodType == null || methodType.getReturnType() == null || methodType.getReturnType() instanceof JavaType.Unknown) {
68+
return call;
69+
}
70+
if (methodType.getReturnType().isAssignableFrom(javaxType)) {
71+
insertRow(ma, ctx, methodType);
72+
return SearchResult.found(call);
73+
}
74+
for (JavaType parameterType : methodType.getParameterTypes()) {
75+
if (parameterType.isAssignableFrom(javaxType)) {
76+
insertRow(ma, ctx, methodType);
77+
return SearchResult.found(call);
78+
}
79+
}
80+
return call;
81+
})
7782
);
7883
}
7984

src/test/java/org/openrewrite/java/migrate/search/FindInternalJavaxApisTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class FindInternalJavaxApisTest implements RewriteTest {
2525

2626
@Override
2727
public void defaults(RecipeSpec spec) {
28-
spec.recipe(new FindInternalJavaxApis("org.openrewrite..* *(..)"));
28+
spec.recipe(new FindInternalJavaxApis(null));
2929
}
3030

3131
@Test
@@ -35,7 +35,7 @@ void returnsJavaxApi() {
3535
java(
3636
"""
3737
package org.openrewrite;
38-
38+
3939
interface Api {
4040
javax.xml.stream.StreamFilter test();
4141
}
@@ -44,9 +44,9 @@ interface Api {
4444
java(
4545
"""
4646
package org.openrewrite;
47-
47+
4848
import javax.xml.stream.StreamFilter;
49-
49+
5050
class Consumer {
5151
void test(Api api) {
5252
StreamFilter sf = api.test();
@@ -55,9 +55,9 @@ void test(Api api) {
5555
""",
5656
"""
5757
package org.openrewrite;
58-
58+
5959
import javax.xml.stream.StreamFilter;
60-
60+
6161
class Consumer {
6262
void test(Api api) {
6363
StreamFilter sf = /*~~>*/api.test();
@@ -75,7 +75,7 @@ void usesJavaxApiInParameter() {
7575
java(
7676
"""
7777
package org.openrewrite;
78-
78+
7979
interface Api {
8080
void test(javax.xml.stream.StreamFilter sf);
8181
}
@@ -84,9 +84,9 @@ interface Api {
8484
java(
8585
"""
8686
package org.openrewrite;
87-
87+
8888
import javax.xml.stream.StreamFilter;
89-
89+
9090
class Consumer {
9191
void test(Api api, StreamFilter sf) {
9292
api.test(sf);
@@ -95,9 +95,9 @@ void test(Api api, StreamFilter sf) {
9595
""",
9696
"""
9797
package org.openrewrite;
98-
98+
9999
import javax.xml.stream.StreamFilter;
100-
100+
101101
class Consumer {
102102
void test(Api api, StreamFilter sf) {
103103
/*~~>*/api.test(sf);

0 commit comments

Comments
 (0)