|
| 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 | +} |
0 commit comments