-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace URL constructors in Java 21 (#352)
* Replace URL constructors in Java 21 As per https://bugs.openjdk.org/browse/JDK-8294241 Fixes #191 * Add license headers
- Loading branch information
Showing
3 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/main/java/org/openrewrite/java/migrate/net/URLConstructorsToURI.java
This file contains 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,70 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0 | ||
* <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 com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import org.openrewrite.java.template.RecipeDescriptor; | ||
|
||
public class URLConstructorsToURI { | ||
@RecipeDescriptor( | ||
name = "Convert `new URL(String)` to `URI.create(String).toURL()`", | ||
description = "Converts `new URL(String)` constructors to `URI.create(String).toURL()`." | ||
) | ||
public static class URLSingleArgumentConstructor { | ||
@BeforeTemplate | ||
java.net.URL urlConstructor(String spec) throws Exception { | ||
return new java.net.URL(spec); | ||
} | ||
|
||
@AfterTemplate | ||
java.net.URL uriCreateToURL(String spec) throws Exception { | ||
return java.net.URI.create(spec).toURL(); | ||
} | ||
} | ||
|
||
@RecipeDescriptor( | ||
name = "Convert `new URL(String, String, String)` to `new URI(...).toURL()`", | ||
description = "Converts `new URL(String, String, String)` constructors to `new URI(...).toURL()`." | ||
) | ||
public static class URLThreeArgumentConstructor { | ||
@BeforeTemplate | ||
java.net.URL urlConstructor(String protocol, String host, String file) throws Exception { | ||
return new java.net.URL(protocol, host, file); | ||
} | ||
|
||
@AfterTemplate | ||
java.net.URL newUriToUrl(String protocol, String host, String file) throws Exception { | ||
return new java.net.URI(protocol, null, host, -1, file, null, null).toURL(); | ||
} | ||
} | ||
|
||
@RecipeDescriptor( | ||
name = "Convert `new URL(String, String, int, String)` to `new URI(...).toURL()`", | ||
description = "Converts `new URL(String, String, int, String)` constructors to `new URI(...).toURL()`." | ||
) | ||
public static class URLFourArgumentConstructor { | ||
@BeforeTemplate | ||
java.net.URL urlConstructor(String protocol, String host, int port, String file) throws Exception { | ||
return new java.net.URL(protocol, host, port, file); | ||
} | ||
|
||
@AfterTemplate | ||
java.net.URL newUriToUrl(String protocol, String host, int port, String file) throws Exception { | ||
return new java.net.URI(protocol, null, host, port, file, null, null).toURL(); | ||
} | ||
} | ||
} |
This file contains 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
65 changes: 65 additions & 0 deletions
65
src/test/java/org/openrewrite/java/migrate/net/URLConstructorsToURITest.java
This file contains 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,65 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0 | ||
* <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.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.Issue; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class URLConstructorsToURITest implements RewriteTest { | ||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new URLConstructorsToURIRecipes()); | ||
} | ||
|
||
@Test | ||
@DocumentExample | ||
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/191") | ||
void urlConstructor() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import java.net.URL; | ||
class Test { | ||
void urlConstructor(String spec) throws Exception { | ||
URL url1 = new URL(spec); | ||
URL url2 = new URL(spec, "localhost", "file"); | ||
URL url3 = new URL(spec, "localhost", 8080, "file"); | ||
} | ||
} | ||
""", | ||
""" | ||
import java.net.URI; | ||
import java.net.URL; | ||
class Test { | ||
void urlConstructor(String spec) throws Exception { | ||
URL url1 = URI.create(spec).toURL(); | ||
URL url2 = new URI(spec, null, "localhost", -1, "file", null, null).toURL(); | ||
URL url3 = new URI(spec, null, "localhost", 8080, "file", null, null).toURL(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |