-
Notifications
You must be signed in to change notification settings - Fork 110
Improve Handling of Absolute, Relative, and Non-Literal URLs in URL Migration #678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MBoegers
merged 11 commits into
openrewrite:main
from
karthikNousher:enhancement/uriRecipee/6616
Feb 26, 2025
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
94766fe
added new recipe to add a new convert uri method.
719e09f
Apply suggestions from code review
timtebeek 4655c48
Ran formatter
rlsanders4 088f4ae
Fix tests
rlsanders4 8611e79
Update logic
rlsanders4 0030ae2
Update string literal check
rlsanders4 4d63082
Update list
rlsanders4 1b6ca91
add test
rlsanders4 f4a58a6
Fix test
rlsanders4 42fde6d
Get constant value
rlsanders4 e050ff3
Polish pre condition handling
MBoegers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
src/main/java/org/openrewrite/java/migrate/net/URLConstructorsToURI_mock.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.