-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recipe ReplaceLocalizedStreamMethods (#532)
* 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 <[email protected]> Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
7bf7506
commit 270244c
Showing
3 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
src/main/java/org/openrewrite/java/migrate/ReplaceLocalizedStreamMethods.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,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); | ||
} | ||
}; | ||
} | ||
} |
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
110 changes: 110 additions & 0 deletions
110
src/test/java/org/openrewrite/java/migrate/ReplaceLocalizedStreamMethodsTest.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,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; | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |