Skip to content

Commit dad3244

Browse files
committed
Fixes to URLConstructorToURICreate recipe
Declare as context-free and also remove the `URL` import if it isn't used anymore.
1 parent 69501b0 commit dad3244

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

src/main/java/org/openrewrite/java/migrate/net/URLConstructorToURICreate.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ 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";
41-
private static final MethodMatcher methodMatcherSingleArg = new MethodMatcher(URL_FQN + " <constructor>(java.lang.String)");
41+
private static final MethodMatcher methodMatcherSingleArg = new MethodMatcher(URL_FQN + "#<init>(java.lang.String)");
4242

4343
@Override
4444
public String getDisplayName() {
@@ -47,7 +47,7 @@ public String getDisplayName() {
4747

4848
@Override
4949
public String getDescription() {
50-
return "Converts `new URL(String)` constructor to `URI.create(String).toURL()`.";
50+
return "Converts `new URL(String)` constructor to `URI.create(String).toURL()`. The URL constructor has been deprecated due to security vulnerabilities when handling malformed URLs. Using `URI.create(String)` provides stronger validation and safer URL handling in modern Java applications.";
5151
}
5252

5353
@Override
@@ -64,10 +64,10 @@ public J visitNewClass(J.NewClass nc, ExecutionContext ctx) {
6464

6565
JavaTemplate template = JavaTemplate.builder("URI.create(#{any(String)}).toURL()")
6666
.imports(URI_FQN)
67-
.contextSensitive()
6867
.javaParser(JavaParser.fromJavaVersion())
6968
.build();
7069
maybeAddImport(URI_FQN);
70+
maybeRemoveImport(URL_FQN);
7171

7272
return template.apply(getCursor(),
7373
nc.getCoordinates().replace(),

src/test/java/org/openrewrite/java/migrate/net/URLConstructorToURICreateTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openrewrite.test.RecipeSpec;
2222
import org.openrewrite.test.RewriteTest;
2323

24+
import static org.openrewrite.gradle.Assertions.buildGradle;
2425
import static org.openrewrite.java.Assertions.java;
2526

2627
class URLConstructorToURICreateTest implements RewriteTest {
@@ -78,6 +79,58 @@ void urlConstructor() {
7879
);
7980
}
8081

82+
@Test
83+
void removeUrlImport() {
84+
rewriteRun(
85+
//language=java
86+
java(
87+
"""
88+
import java.net.URL;
89+
90+
class Test {
91+
void foo() {
92+
System.out.println(new URL("https://test.com"));
93+
}
94+
}
95+
""",
96+
"""
97+
import java.net.URI;
98+
99+
class Test {
100+
void foo() {
101+
System.out.println(URI.create("https://test.com").toURL());
102+
}
103+
}
104+
"""
105+
)
106+
);
107+
}
108+
109+
@Test
110+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/620")
111+
void gradleUrl() {
112+
rewriteRun(
113+
//language=groovy
114+
buildGradle(
115+
"""
116+
import java.net.URL
117+
import java.util.concurrent.atomic.AtomicReference
118+
119+
def url = new AtomicReference<URL>()
120+
url.set(new URL('https://www.reactive-streams.org/reactive-streams-1.0.3-javadoc/'))
121+
""",
122+
"""
123+
import java.net.URI
124+
import java.net.URL
125+
import java.util.concurrent.atomic.AtomicReference
126+
127+
def url = new AtomicReference<URL>()
128+
url.set(URI.create('https://www.reactive-streams.org/reactive-streams-1.0.3-javadoc/').toURL())
129+
"""
130+
)
131+
);
132+
}
133+
81134
@Test
82135
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/620")
83136
void urlCheckConstantAbsolutePath() {

0 commit comments

Comments
 (0)