Skip to content

Commit 9450b00

Browse files
committed
FindInternalJavaxApis
1 parent f3a0409 commit 9450b00

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.search;
17+
18+
import lombok.EqualsAndHashCode;
19+
import lombok.RequiredArgsConstructor;
20+
import org.openrewrite.*;
21+
import org.openrewrite.internal.StringUtils;
22+
import org.openrewrite.java.search.UsesType;
23+
import org.openrewrite.java.table.MethodCalls;
24+
import org.openrewrite.java.trait.MethodAccess;
25+
import org.openrewrite.java.tree.JavaType;
26+
import org.openrewrite.java.tree.MethodCall;
27+
import org.openrewrite.marker.SearchResult;
28+
29+
import java.util.regex.Pattern;
30+
import java.util.stream.Collectors;
31+
32+
@RequiredArgsConstructor
33+
@EqualsAndHashCode(callSuper = false)
34+
public class FindInternalJavaxApis extends Recipe {
35+
36+
private final transient MethodCalls methodCalls = new MethodCalls(this);
37+
38+
@Option(
39+
displayName = "Method pattern",
40+
description = "A method pattern that is used to find matching method invocations.",
41+
example = "java.util.List add(..)"
42+
)
43+
private final String methodPattern;
44+
45+
@Override
46+
public String getDisplayName() {
47+
return "Find uses of internal javax APIs";
48+
}
49+
50+
@Override
51+
public String getDescription() {
52+
return "The libraries that define these APIs will have to be migrated before any of the repositories that use them.";
53+
}
54+
55+
@Override
56+
public TreeVisitor<?, ExecutionContext> getVisitor() {
57+
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+
})
77+
);
78+
}
79+
80+
private void insertRow(MethodAccess ma, ExecutionContext ctx, JavaType.Method methodType) {
81+
methodCalls.insertRow(ctx, new MethodCalls.Row(
82+
ma.getCursor().firstEnclosingOrThrow(SourceFile.class).getSourcePath().toString(),
83+
ma.getTree().printTrimmed(ma.getCursor()),
84+
methodType.getDeclaringType().toString(),
85+
methodType.getName(),
86+
methodType.getParameterTypes().stream().map(String::valueOf)
87+
.collect(Collectors.joining(", "))
88+
));
89+
}
90+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.search;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.test.RecipeSpec;
20+
import org.openrewrite.test.RewriteTest;
21+
22+
import static org.openrewrite.java.Assertions.java;
23+
24+
public class FindInternalJavaxApisTest implements RewriteTest {
25+
26+
@Override
27+
public void defaults(RecipeSpec spec) {
28+
spec.recipe(new FindInternalJavaxApis("org.openrewrite..* *(..)"));
29+
}
30+
31+
@Test
32+
void returnsJavaxApi() {
33+
//language=java
34+
rewriteRun(
35+
java(
36+
"""
37+
package org.openrewrite;
38+
39+
interface Api {
40+
javax.xml.stream.StreamFilter test();
41+
}
42+
"""
43+
),
44+
java(
45+
"""
46+
package org.openrewrite;
47+
48+
import javax.xml.stream.StreamFilter;
49+
50+
class Consumer {
51+
void test(Api api) {
52+
StreamFilter sf = api.test();
53+
}
54+
}
55+
""",
56+
"""
57+
package org.openrewrite;
58+
59+
import javax.xml.stream.StreamFilter;
60+
61+
class Consumer {
62+
void test(Api api) {
63+
StreamFilter sf = /*~~>*/api.test();
64+
}
65+
}
66+
"""
67+
)
68+
);
69+
}
70+
71+
@Test
72+
void usesJavaxApiInParameter() {
73+
//language=java
74+
rewriteRun(
75+
java(
76+
"""
77+
package org.openrewrite;
78+
79+
interface Api {
80+
void test(javax.xml.stream.StreamFilter sf);
81+
}
82+
"""
83+
),
84+
java(
85+
"""
86+
package org.openrewrite;
87+
88+
import javax.xml.stream.StreamFilter;
89+
90+
class Consumer {
91+
void test(Api api, StreamFilter sf) {
92+
api.test(sf);
93+
}
94+
}
95+
""",
96+
"""
97+
package org.openrewrite;
98+
99+
import javax.xml.stream.StreamFilter;
100+
101+
class Consumer {
102+
void test(Api api, StreamFilter sf) {
103+
/*~~>*/api.test(sf);
104+
}
105+
}
106+
"""
107+
)
108+
);
109+
}
110+
}

0 commit comments

Comments
 (0)