Skip to content

Commit 9c6a0ec

Browse files
committed
Split URLConstructorsToURI into two separate recipes
Fixes #467
1 parent 8c5821d commit 9c6a0ec

File tree

5 files changed

+160
-48
lines changed

5 files changed

+160
-48
lines changed

src/main/java/org/openrewrite/java/migrate/net/URLConstructorsToURI.java renamed to src/main/java/org/openrewrite/java/migrate/net/URLConstructorToURICreate.java

+2-31
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@
3434

3535
import java.net.URI;
3636

37-
public class URLConstructorsToURI extends Recipe {
37+
public class URLConstructorToURICreate extends Recipe {
3838

3939
private static final String URI_FQN = "java.net.URI";
4040
private static final String URL_FQN = "java.net.URL";
4141
private static final MethodMatcher methodMatcherSingleArg = new MethodMatcher(URL_FQN + " <constructor>(java.lang.String)");
42-
private static final MethodMatcher methodMatcherThreeArg = new MethodMatcher(URL_FQN + " <constructor>(java.lang.String, java.lang.String, java.lang.String)");
43-
private static final MethodMatcher methodMatcherFourArg = new MethodMatcher(URL_FQN + " <constructor>(java.lang.String, java.lang.String, int, java.lang.String)");
4442

4543
@Override
4644
public String getDisplayName() {
@@ -49,7 +47,7 @@ public String getDisplayName() {
4947

5048
@Override
5149
public String getDescription() {
52-
return "Converts `new URL(String)` constructors to `URI.create(String).toURL()`.";
50+
return "Converts `new URL(String)` constructor to `URI.create(String).toURL()`.";
5351
}
5452

5553
@Override
@@ -74,33 +72,6 @@ public J visitNewClass(J.NewClass nc, ExecutionContext ctx) {
7472
return template.apply(getCursor(),
7573
nc.getCoordinates().replace(),
7674
nc.getArguments().get(0));
77-
} else {
78-
if (methodMatcherThreeArg.matches(nc)) {
79-
JavaTemplate template = JavaTemplate.builder("new URI(#{any(String)}, null, #{any(String)}, -1, #{any(String)}, null, null).toURL()")
80-
.imports(URI_FQN, URL_FQN)
81-
.contextSensitive()
82-
.javaParser(JavaParser.fromJavaVersion())
83-
.build();
84-
85-
maybeAddImport(URI_FQN);
86-
return template.apply(getCursor(), nc.getCoordinates().replace(),
87-
nc.getArguments().get(0),
88-
nc.getArguments().get(1),
89-
nc.getArguments().get(2));
90-
} else if (methodMatcherFourArg.matches(nc)) {
91-
JavaTemplate template = JavaTemplate.builder("new URI(#{any(String)}, null, #{any(String)}, #{any(int)}, #{any(String)}, null, null).toURL()")
92-
.imports(URI_FQN, URL_FQN)
93-
.contextSensitive()
94-
.javaParser(JavaParser.fromJavaVersion())
95-
.build();
96-
97-
maybeAddImport(URI_FQN);
98-
return template.apply(getCursor(), nc.getCoordinates().replace(),
99-
nc.getArguments().get(0),
100-
nc.getArguments().get(1),
101-
nc.getArguments().get(2),
102-
nc.getArguments().get(3));
103-
}
10475
}
10576
return super.visitNewClass(nc, ctx);
10677
}
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 Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
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.net;
17+
18+
import org.jspecify.annotations.Nullable;
19+
import org.openrewrite.ExecutionContext;
20+
import org.openrewrite.Preconditions;
21+
import org.openrewrite.Recipe;
22+
import org.openrewrite.TreeVisitor;
23+
import org.openrewrite.analysis.constantfold.ConstantFold;
24+
import org.openrewrite.analysis.util.CursorUtil;
25+
import org.openrewrite.java.JavaParser;
26+
import org.openrewrite.java.JavaTemplate;
27+
import org.openrewrite.java.JavaVisitor;
28+
import org.openrewrite.java.MethodMatcher;
29+
import org.openrewrite.java.search.UsesType;
30+
import org.openrewrite.java.tree.Expression;
31+
import org.openrewrite.java.tree.J;
32+
import org.openrewrite.java.tree.JavaType;
33+
import org.openrewrite.java.tree.TypeUtils;
34+
35+
import java.net.URI;
36+
37+
public class URLConstructorsToNewURI extends Recipe {
38+
39+
private static final String URI_FQN = "java.net.URI";
40+
private static final String URL_FQN = "java.net.URL";
41+
private static final MethodMatcher methodMatcherThreeArg = new MethodMatcher(URL_FQN + " <constructor>(java.lang.String, java.lang.String, java.lang.String)");
42+
private static final MethodMatcher methodMatcherFourArg = new MethodMatcher(URL_FQN + " <constructor>(java.lang.String, java.lang.String, int, java.lang.String)");
43+
44+
@Override
45+
public String getDisplayName() {
46+
return "Convert `new URL(String, ..)` to `new URI(String, ..).toURL()`";
47+
}
48+
49+
@Override
50+
public String getDescription() {
51+
return "Converts `new URL(String, ..)` constructors to `new URI(String, ..).toURL()`.";
52+
}
53+
54+
@Override
55+
public TreeVisitor<?, ExecutionContext> getVisitor() {
56+
return Preconditions.check(new UsesType<>(URL_FQN, false),
57+
new JavaVisitor<ExecutionContext>() {
58+
@Override
59+
public J visitNewClass(J.NewClass nc, ExecutionContext ctx) {
60+
if (methodMatcherThreeArg.matches(nc)) {
61+
JavaTemplate template = JavaTemplate.builder("new URI(#{any(String)}, null, #{any(String)}, -1, #{any(String)}, null, null).toURL()")
62+
.imports(URI_FQN, URL_FQN)
63+
.contextSensitive()
64+
.javaParser(JavaParser.fromJavaVersion())
65+
.build();
66+
67+
maybeAddImport(URI_FQN);
68+
return template.apply(getCursor(), nc.getCoordinates().replace(),
69+
nc.getArguments().get(0),
70+
nc.getArguments().get(1),
71+
nc.getArguments().get(2));
72+
} else if (methodMatcherFourArg.matches(nc)) {
73+
JavaTemplate template = JavaTemplate.builder("new URI(#{any(String)}, null, #{any(String)}, #{any(int)}, #{any(String)}, null, null).toURL()")
74+
.imports(URI_FQN, URL_FQN)
75+
.contextSensitive()
76+
.javaParser(JavaParser.fromJavaVersion())
77+
.build();
78+
79+
maybeAddImport(URI_FQN);
80+
return template.apply(getCursor(), nc.getCoordinates().replace(),
81+
nc.getArguments().get(0),
82+
nc.getArguments().get(1),
83+
nc.getArguments().get(2),
84+
nc.getArguments().get(3));
85+
}
86+
return super.visitNewClass(nc, ctx);
87+
}
88+
});
89+
}
90+
}

src/main/resources/META-INF/rewrite/java-version-21.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ recipeList:
2929
- org.openrewrite.java.migrate.UpgradeBuildToJava21
3030
- org.openrewrite.java.migrate.RemoveIllegalSemicolons
3131
- org.openrewrite.java.migrate.lang.ThreadStopUnsupported
32-
- org.openrewrite.java.migrate.net.URLConstructorsToURI
32+
- org.openrewrite.java.migrate.net.URLConstructorToURICreate
33+
# Fails to introduce new exception handling https://github.com/openrewrite/rewrite-migrate-java/issues/467
34+
#- org.openrewrite.java.migrate.net.URLConstructorsToNewURI
3335
- org.openrewrite.java.migrate.util.SequencedCollection
3436
- org.openrewrite.java.migrate.util.UseLocaleOf
3537
- org.openrewrite.staticanalysis.ReplaceDeprecatedRuntimeExecMethods

src/test/java/org/openrewrite/java/migrate/net/URLConstructorsToURITest.java renamed to src/test/java/org/openrewrite/java/migrate/net/URLConstructorToURICreateTest.java

+2-16
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323

2424
import static org.openrewrite.java.Assertions.java;
2525

26-
class URLConstructorsToURITest implements RewriteTest {
26+
class URLConstructorToURICreateTest implements RewriteTest {
2727
@Override
2828
public void defaults(RecipeSpec spec) {
29-
spec.recipe(new URLConstructorsToURI());
29+
spec.recipe(new URLConstructorToURICreate());
3030
}
3131

3232
@Test
@@ -42,20 +42,6 @@ void urlConstructor() {
4242
class Test {
4343
void urlConstructor(String spec) throws Exception {
4444
URL url1 = new URL(spec);
45-
URL url2 = new URL(spec, "localhost", "file");
46-
URL url3 = new URL(spec, "localhost", 8080, "file");
47-
}
48-
}
49-
""",
50-
"""
51-
import java.net.URI;
52-
import java.net.URL;
53-
54-
class Test {
55-
void urlConstructor(String spec) throws Exception {
56-
URL url1 = new URL(spec);
57-
URL url2 = new URI(spec, null, "localhost", -1, "file", null, null).toURL();
58-
URL url3 = new URI(spec, null, "localhost", 8080, "file", null, null).toURL();
5945
}
6046
}
6147
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
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.net;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.openrewrite.java.Assertions.java;
24+
25+
class URLConstructorsToNewURITest implements RewriteTest {
26+
@Override
27+
public void defaults(RecipeSpec spec) {
28+
spec.recipe(new URLConstructorsToNewURI());
29+
}
30+
31+
@Test
32+
@DocumentExample
33+
void urlConstructor() {
34+
rewriteRun(
35+
//language=java
36+
java(
37+
"""
38+
import java.net.URL;
39+
40+
class Test {
41+
void urlConstructor(String spec) throws Exception {
42+
URL url1 = new URL(spec);
43+
URL url2 = new URL(spec, "localhost", "file");
44+
URL url3 = new URL(spec, "localhost", 8080, "file");
45+
}
46+
}
47+
""",
48+
"""
49+
import java.net.URI;
50+
import java.net.URL;
51+
52+
class Test {
53+
void urlConstructor(String spec) throws Exception {
54+
URL url1 = new URL(spec);
55+
URL url2 = new URI(spec, null, "localhost", -1, "file", null, null).toURL();
56+
URL url3 = new URI(spec, null, "localhost", 8080, "file", null, null).toURL();
57+
}
58+
}
59+
"""
60+
)
61+
);
62+
}
63+
}

0 commit comments

Comments
 (0)