diff --git a/src/main/java/org/openrewrite/java/migrate/ReplaceLocalizedStreamMethods.java b/src/main/java/org/openrewrite/java/migrate/ReplaceLocalizedStreamMethods.java new file mode 100644 index 0000000000..46ae4869e0 --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/ReplaceLocalizedStreamMethods.java @@ -0,0 +1,82 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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; + +import com.fasterxml.jackson.annotation.JsonCreator; +import lombok.EqualsAndHashCode; +import lombok.Value; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Option; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.java.JavaVisitor; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.Space; + +@Value +@EqualsAndHashCode(callSuper = false) +public class ReplaceLocalizedStreamMethods extends Recipe { + + @Option(displayName = "Method pattern to replace", + description = "The method pattern to match and replace.", + example = "java.lang.Runtime getLocalizedInputStream(java.io.InputStream)") + String localizedInputStreamMethodMatcher; + + @Option(displayName = "Method pattern to replace", + description = "The method pattern to match and replace.", + example = "java.lang.Runtime getLocalizedOutputStream(java.io.OutputStream)") + String localizedOutputStreamMethodMatcher; + + @JsonCreator + public ReplaceLocalizedStreamMethods( + @Nullable String localizedInputStreamMethodMatcher, + @Nullable String localizedOutputStreamMethodMatcher) { + this.localizedInputStreamMethodMatcher = localizedInputStreamMethodMatcher == null ? + "java.lang.Runtime getLocalizedInputStream(java.io.InputStream)" : localizedInputStreamMethodMatcher; + this.localizedOutputStreamMethodMatcher = localizedOutputStreamMethodMatcher == null ? + "java.lang.Runtime getLocalizedOutputStream(java.io.OutputStream)" : localizedOutputStreamMethodMatcher; + } + + @Override + public String getDisplayName() { + return "Replace `getLocalizedInputStream` and `getLocalizedOutputStream` with direct assignment"; + } + + @Override + public String getDescription() { + return "Replaces `Runtime.getLocalizedInputStream(InputStream)` and `Runtime.getLocalizedOutputStream(OutputStream)` with their direct arguments. " + + "This modification is made because the previous implementation of `getLocalizedInputStream` and `getLocalizedOutputStream` merely returned the arguments provided."; + } + + @Override + public TreeVisitor getVisitor() { + return new JavaVisitor() { + private final MethodMatcher LocalizedInputStreamMethod = new MethodMatcher(localizedInputStreamMethodMatcher, false); + private final MethodMatcher localizedOutputStreamMethod = new MethodMatcher(localizedOutputStreamMethodMatcher, false); + + @Override + public J visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) { + if (LocalizedInputStreamMethod.matches(mi) || localizedOutputStreamMethod.matches(mi)) { + return mi.getArguments().get(0).withPrefix(mi.getPrefix()); + } + return super.visitMethodInvocation(mi, ctx); + } + }; + } +} diff --git a/src/main/resources/META-INF/rewrite/java-version-11.yml b/src/main/resources/META-INF/rewrite/java-version-11.yml index f54c3ee6e5..eb3e7cf90f 100644 --- a/src/main/resources/META-INF/rewrite/java-version-11.yml +++ b/src/main/resources/META-INF/rewrite/java-version-11.yml @@ -73,6 +73,8 @@ recipeList: getPeerMethodPattern: java.awt.* getPeer() lightweightPeerFQCN: java.awt.peer.LightweightPeer - org.openrewrite.scala.migrate.UpgradeScala_2_12 + - org.openrewrite.java.migrate.ReplaceLocalizedStreamMethods + --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.UpgradeBuildToJava11 diff --git a/src/test/java/org/openrewrite/java/migrate/ReplaceLocalizedStreamMethodsTest.java b/src/test/java/org/openrewrite/java/migrate/ReplaceLocalizedStreamMethodsTest.java new file mode 100644 index 0000000000..f27f0a408c --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/ReplaceLocalizedStreamMethodsTest.java @@ -0,0 +1,110 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class ReplaceLocalizedStreamMethodsTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec + .parser(JavaParser.fromJavaVersion().dependsOn( + //language=java + """ + package com.test; + import java.io.InputStream; + import java.io.OutputStream; + public class Runtime1 { + public InputStream getLocalizedInputStream1(InputStream in) { + return in; + } + public OutputStream getLocalizedOutputStream1(OutputStream out) { + return out; + } + } + """ + )) + .recipe(new ReplaceLocalizedStreamMethods( + "com.test.Runtime1 getLocalizedInputStream1(java.io.InputStream)", + "com.test.Runtime1 getLocalizedOutputStream1(java.io.OutputStream)")); + } + + @Test + @DocumentExample + void replaceGetLocalizedInputStream() { + rewriteRun( + //language=java + java( + """ + package com.test; + import java.io.InputStream; + + class Test { + void exampleMethod(Runtime1 rt, InputStream in) { + InputStream newStream = rt.getLocalizedInputStream1(in); + } + } + """, + """ + package com.test; + import java.io.InputStream; + + class Test { + void exampleMethod(Runtime1 rt, InputStream in) { + InputStream newStream = in; + } + } + """ + ) + ); + } + + @Test + void replaceGetLocalizedOutputStream() { + rewriteRun( + //language=java + java( + """ + package com.test; + import java.io.OutputStream; + + class Test { + void exampleMethod(Runtime1 rt, OutputStream out) { + OutputStream newStream = rt.getLocalizedOutputStream1(out); + } + } + """, + """ + package com.test; + import java.io.OutputStream; + + class Test { + void exampleMethod(Runtime1 rt, OutputStream out) { + OutputStream newStream = out; + } + } + """ + ) + ); + } +}