Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate.net;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.*;
import org.openrewrite.java.tree.*;

public class URLConstructorsToURI_mock extends Recipe {
@Override
public String getDisplayName() {
return "Custom Migration: Replace new `URL(String)` with `transformNonLiteralURIToValidURL(String)`";
}

@Override
public String getDescription() {
return "Standardizes URL creation by replacing new `URL(String)` with `transformNonLiteralURIToValidURL(String)`," +
"ensuring consistent handling of absolute and relative paths.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaVisitor<ExecutionContext>() {

private final MethodMatcher methodMatcherSingleArg = new MethodMatcher("java.net.URL <constructor>(java.lang.String)");
private final MethodMatcher methodMatcherThreeArg = new MethodMatcher("java.net.URL <constructor>(java.lang.String, java.lang.String, java.lang.String)");
private final MethodMatcher methodMatcherFourArg = new MethodMatcher("java.net.URL <constructor>(java.lang.String, java.lang.String, int, java.lang.String)");
boolean methodAdded = false;

@Override
public J visitClassDeclaration(J.ClassDeclaration cd, ExecutionContext ctx) {

cd = (J.ClassDeclaration) super.visitClassDeclaration(cd, ctx);

boolean methodExists = cd.getBody().getStatements().stream().filter(J.MethodDeclaration.class::isInstance).map(
J.MethodDeclaration.class::cast
)
.anyMatch(
md -> md.getSimpleName().equals("transformNonLiteralURIToValidURL")
);

if (!methodExists && methodAdded) {
JavaTemplate convertUriMethod = JavaTemplate.builder(
"public URL transformNonLiteralURIToValidURL(String spec) {\n" +
" if (URI.create(spec).isAbsolute()) {\n" +
" return URI.create(spec).toURL();\n" +
" } else {\n" +
" return new URL(spec);\n" +
" }\n" +
"}")
.contextSensitive()
.imports("java.net.URI", "java.net.URL")
.javaParser(JavaParser.fromJavaVersion())
.build();
maybeAddImport("java.net.URI");
getCursor().putMessage("alreadyTransformed", true); // Mark as transformed

cd = convertUriMethod.apply(updateCursor(cd), cd.getBody().getCoordinates().lastStatement());
}
return cd;
}

@Override
public J visitNewClass(J.NewClass elem, ExecutionContext ctx) {
J.MethodDeclaration enclosingMethod = getCursor().firstEnclosing(J.MethodDeclaration.class);
if (enclosingMethod != null && "transformNonLiteralURIToValidURL".equals(enclosingMethod.getSimpleName())) {
return super.visitNewClass(elem, ctx);
}
if (methodMatcherSingleArg.matches(elem)) {

if (elem.getArguments().get(0) instanceof J.Literal) {

JavaTemplate template = JavaTemplate.builder("URI.create(#{any(String)}).toURL()")
.imports("java.net.URI")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion())
.build();

J result = template.apply(getCursor(),
elem.getCoordinates().replace(),
elem.getArguments().get(0));
maybeAddImport("java.net.URI");
return result;

} else {
methodAdded = true;

JavaTemplate template = JavaTemplate.builder("transformNonLiteralURIToValidURL(#{any(String)})")
.imports("java.net.URI")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion())
.build();
J result = template.apply(getCursor(), elem.getCoordinates().replace(), elem.getArguments().get(0));
maybeAddImport("java.net.URI");
return result;
}
} else if (methodMatcherThreeArg.matches(elem)) {

JavaTemplate template = JavaTemplate.builder("new URI(#{any(String)}, null, #{any(String)}, -1, #{any(String)}, null, null).toURL()")
.imports("java.net.URI", "java.net.URL")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion())
.build();

J result = template.apply(getCursor(), elem.getCoordinates().replace(),
elem.getArguments().get(0),
elem.getArguments().get(1),
elem.getArguments().get(2));
maybeAddImport("java.net.URI");
return result;

} else if (methodMatcherFourArg.matches(elem)) {

JavaTemplate template = JavaTemplate.builder("new URI(#{any(String)}, null, #{any(String)}, #{any(int)}, #{any(String)}, null, null).toURL()")
.imports("java.net.URI", "java.net.URL")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion())
.build();

J result = template.apply(getCursor(), elem.getCoordinates().replace(),
elem.getArguments().get(0),
elem.getArguments().get(1),
elem.getArguments().get(2),
elem.getArguments().get(3));
maybeAddImport("java.net.URI");
return result;
}
return super.visitNewClass(elem, ctx);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

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

class URLConstructorsToURITest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new URLConstructorsToURIRecipes());
spec.recipe(new URLConstructorsToURI_mock())
.typeValidationOptions(TypeValidation.none());
}

@Test
Expand All @@ -53,10 +55,77 @@ void urlConstructor(String spec) throws Exception {

class Test {
void urlConstructor(String spec) throws Exception {
URL url1 = URI.create(spec).toURL();
URL url1 = transformNonLiteralURIToValidURL(spec);
URL url2 = new URI(spec, null, "localhost", -1, "file", null, null).toURL();
URL url3 = new URI(spec, null, "localhost", 8080, "file", null, null).toURL();
}

public URL transformNonLiteralURIToValidURL(String spec) {
if (URI.create(spec).isAbsolute()) {
return URI.create(spec).toURL();
} else {
return new URL(spec);
}
}
}
"""
)
);
}
@Test
@DocumentExample
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/191")
void urlCheckAbsolutePath() {
rewriteRun(
//language=java
java(
"""
import java.net.URL;

class Test {
void urlConstructor(String spec) throws Exception {
URL url1 = new URL("https://test.com/openrewrite/testCase/testCase/191");
}
}
""",
"""
import java.net.URI;
import java.net.URL;

class Test {
void urlConstructor(String spec) throws Exception {
URL url1 = URI.create("https://test.com/openrewrite/testCase/testCase/191").toURL();
}
}
"""
)
);
}

@Test
@DocumentExample
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/191")
void urlCheckRelativePath() {
rewriteRun(
//language=java
java(
"""
import java.net.URL;

class Test {
void urlConstructor(String spec) throws Exception {
URL url1 = new URL("TEST-INF/test/testCase.wsdl");
}
}
""",
"""
import java.net.URI;
import java.net.URL;

class Test {
void urlConstructor(String spec) throws Exception {
URL url1 = URI.create("TEST-INF/test/testCase.wsdl").toURL();
}
}
"""
)
Expand Down