Skip to content

Commit

Permalink
Recipe ReplaceLocalizedStreamMethods (#532)
Browse files Browse the repository at this point in the history
* Recipe for LocalizedStreamMethods

* Formatted the file

* Remove RemovedLogManagerPropertyChangeListenerMethods for now

* Fix compilation and other suggested fixes

* Retain original prefix

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Updated the getDisplayName and getDescription

* Modified as mentioned in Comments

* Updated the description

* Update ReplaceLocalizedStreamMethods.java

* Update ReplaceLocalizedStreamMethods.java

---------

Co-authored-by: bhavanapidapa <bhavana.pidapa1@ibm.com>
Co-authored-by: Tim te Beek <timtebeek@gmail.com>
Co-authored-by: Tim te Beek <tim@moderne.io>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
5 people committed Aug 20, 2024
1 parent 7bf7506 commit 270244c
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2024 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;

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<?, 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)) {
return mi.getArguments().get(0).withPrefix(mi.getPrefix());
}
return super.visitMethodInvocation(mi, ctx);
}
};
}
}
2 changes: 2 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,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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2024 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;

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

0 comments on commit 270244c

Please sign in to comment.