Skip to content
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

Recipe ReplaceLocalizedStreamMethods #532

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.openrewrite.java.migrate;

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.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 = "java.lang.Runtime getLocalizedInputStream(java.io.InputStream)";

@Option(displayName = "Method pattern to replace",
description = "The method pattern to match and replace.",
example = "java.lang.Runtime getLocalizedOutputStream(java.io.OutputStream)")
String localizedOutputStreamMethodMatcher = "java.lang.Runtime getLocalizedOutputStream(java.io.OutputStream)";

@Override
public String getDisplayName() {
return "Replace getLocalizedInputStream with direct assignment";
}

@Override
public String getDescription() {
return "Replaces `InputStream new = rt.getLocalizedInputStream(in);` with `InputStream new = in;`.";
}

public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaVisitor<ExecutionContext>() {
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)) {
Expression parameter = mi.getArguments().get(0);
parameter = parameter.withPrefix(Space.SINGLE_SPACE);
return parameter;
}
return super.visitMethodInvocation(mi, ctx);
}
};
}
}
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/rewrite/java-version-11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ recipeList:
getPeerMethodPattern: java.awt.* getPeer()
lightweightPeerFQCN: java.awt.peer.LightweightPeer
- org.openrewrite.scala.migrate.UpgradeScala_2_12
- org.openrewrite.java.migrate.RemovedLogManagerPropertyChangeListenerMethods
- org.openrewrite.java.migrate.ReplaceLocalizedStreamMethods:
localizedInputStreamMethodMatcher: java.lang.Runtime getLocalizedInputStream(java.io.InputStream)
localizedOutputStreamMethodMatcher: java.lang.Runtime getLocalizedOutputStream(java.io.OutputStream)

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.UpgradeBuildToJava11
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.openrewrite.java.migrate;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

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

public class ReplaceLocalizedStreamMethodsTest implements RewriteTest {
//language=java
String LocalizedStreamMethodsClass = """

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;
}

}
""";

@Override
public void defaults(RecipeSpec spec) {
spec.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(LocalizedStreamMethodsClass),
java(
"""
package com.test;
import java.io.InputStream;

class Test {
void exampleMethod(InputStream in) {
Runtime1 rt = null;
InputStream newStream = rt.getLocalizedInputStream1(in);
}
}
""",
"""
package com.test;
import java.io.InputStream;

class Test {
void exampleMethod(InputStream in) {
Runtime1 rt = null;
InputStream newStream = in;
}
}
"""
)
);
}

@Test
void replaceGetLocalizedOutputStream() {
rewriteRun(
//language=java
java(LocalizedStreamMethodsClass),
java(
"""
package com.test;
import java.io.OutputStream;

class Test {
void exampleMethod(OutputStream out) {
Runtime1 rt = null;
OutputStream newStream = rt.getLocalizedOutputStream1(out);
}
}
""",
"""
package com.test;
import java.io.OutputStream;

class Test {
void exampleMethod(OutputStream out) {
Runtime1 rt = null;
OutputStream newStream = out;
}
}
"""
)
);
}
}

Loading