1717
1818import lombok .EqualsAndHashCode ;
1919import lombok .RequiredArgsConstructor ;
20+ import org .jspecify .annotations .Nullable ;
2021import org .openrewrite .*;
2122import org .openrewrite .internal .StringUtils ;
2223import 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
0 commit comments