-
Notifications
You must be signed in to change notification settings - Fork 101
Add recipe to migrate System.out.print/println
to IO.print/println
#848
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ac75430
Add ReplaceSystemOutWithIOPrint to migrate System.out.print/println t…
e5LA 739873f
refactor: reformatting
e5LA 613a4c0
Handle additional edge case with static import
timtebeek 6c085e6
Inline templates
timtebeek 2e1c69f
Use markdown
timtebeek 9f76b57
Include with Java 25 upgrade
timtebeek 79a7eb3
Do not replace `printf`
timtebeek 5ab7156
Merge branch 'main' into java25-migrate-system-out
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
src/main/java/org/openrewrite/java/migrate/io/ReplaceSystemOutWithIOPrint.java
This file contains hidden or 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,89 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license | ||
* <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.io; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.search.UsesMethod; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.TypeUtils; | ||
|
||
public class ReplaceSystemOutWithIOPrint extends Recipe { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Migrate `System.out.print` to Java 25 IO utility class"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Replace `System.out.print()`, `System.out.println()` with `IO.print()` and `IO.println()`. " + | ||
"Migrates to the new IO utility class introduced in Java 25."; | ||
} | ||
|
||
private static final MethodMatcher SYSTEM_OUT_PRINT = new MethodMatcher("java.io.PrintStream print(..)"); | ||
private static final MethodMatcher SYSTEM_OUT_PRINTLN = new MethodMatcher("java.io.PrintStream println(..)"); | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
Preconditions.or( | ||
new UsesMethod<>(SYSTEM_OUT_PRINT), | ||
new UsesMethod<>(SYSTEM_OUT_PRINTLN) | ||
), | ||
new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
J.MethodInvocation m = super.visitMethodInvocation(method, ctx); | ||
if (!isSystemOutMethod(m)) { | ||
return m; | ||
} | ||
String methodName = m.getName().getSimpleName(); | ||
return m.getArguments().isEmpty() ? | ||
JavaTemplate.builder("IO.#{}()").build() | ||
.apply(getCursor(), m.getCoordinates().replace(), methodName) : | ||
JavaTemplate.builder("IO.#{}(#{any()})").build() | ||
.apply(getCursor(), m.getCoordinates().replace(), methodName, m.getArguments().get(0)); | ||
} | ||
|
||
private boolean isSystemOutMethod(J.MethodInvocation mi) { | ||
if (SYSTEM_OUT_PRINT.matches(mi) || SYSTEM_OUT_PRINTLN.matches(mi)) { | ||
Expression expression = mi.getSelect(); | ||
if (expression instanceof J.FieldAccess) { | ||
return isSystemOut(((J.FieldAccess) expression).getName()); | ||
} | ||
if (expression instanceof J.Identifier) { | ||
maybeRemoveImport("java.lang.System.out"); | ||
return isSystemOut((J.Identifier) expression); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean isSystemOut(J.Identifier identifier) { | ||
return "out".equals(identifier.getSimpleName()) && | ||
identifier.getFieldType() != null && | ||
TypeUtils.isAssignableTo("java.lang.System", identifier.getFieldType().getOwner()); | ||
} | ||
}); | ||
} | ||
} |
This file contains hidden or 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
263 changes: 263 additions & 0 deletions
263
src/test/java/org/openrewrite/java/migrate/io/ReplaceSystemOutWithIOPrintTest.java
This file contains hidden or 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,263 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license | ||
* <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.io; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.java.search.FindMissingTypes; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
import org.openrewrite.test.TypeValidation; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
import static org.openrewrite.java.Assertions.javaVersion; | ||
|
||
class ReplaceSystemOutWithIOPrintTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new ReplaceSystemOutWithIOPrint()) | ||
.afterTypeValidationOptions(TypeValidation.all().allowMissingType(o -> { | ||
assert o instanceof FindMissingTypes.MissingTypeResult; | ||
FindMissingTypes.MissingTypeResult result = (FindMissingTypes.MissingTypeResult) o; | ||
return result.getPrintedTree().contains("IO"); | ||
})) // TODO remove once tests run on Java 25+ | ||
.parser(JavaParser.fromJavaVersion()) | ||
.allSources(s -> s.markers(javaVersion(25))); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
e5LA marked this conversation as resolved.
Show resolved
Hide resolved
|
||
void replaceSystemOutPrint() { | ||
rewriteRun( | ||
java( | ||
""" | ||
class Example { | ||
void test() { | ||
System.out.print("Hello"); | ||
} | ||
} | ||
""", | ||
""" | ||
class Example { | ||
void test() { | ||
IO.print("Hello"); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replaceSystemOutPrintln() { | ||
rewriteRun( | ||
java( | ||
""" | ||
class Example { | ||
void test() { | ||
System.out.println("Hello"); | ||
} | ||
} | ||
""", | ||
""" | ||
class Example { | ||
void test() { | ||
IO.println("Hello"); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replaceSystemOutPrintlnWithStaticImport() { | ||
rewriteRun( | ||
java( | ||
""" | ||
import static java.lang.System.out; | ||
|
||
class Example { | ||
void test() { | ||
out.println("Hello"); | ||
} | ||
} | ||
""", | ||
""" | ||
class Example { | ||
void test() { | ||
IO.println("Hello"); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replaceSystemOutPrintWithVariable() { | ||
rewriteRun( | ||
java( | ||
""" | ||
class Example { | ||
void test() { | ||
String message = "Hello World"; | ||
System.out.print(message); | ||
} | ||
} | ||
""", | ||
""" | ||
class Example { | ||
void test() { | ||
String message = "Hello World"; | ||
IO.print(message); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replaceSystemOutPrintlnEmpty() { | ||
rewriteRun( | ||
java( | ||
""" | ||
class Example { | ||
void test() { | ||
System.out.println(); | ||
} | ||
} | ||
""", | ||
""" | ||
class Example { | ||
void test() { | ||
IO.println(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replaceMultipleSystemOutCalls() { | ||
rewriteRun( | ||
java( | ||
""" | ||
class Example { | ||
void test() { | ||
System.out.print("Hello"); | ||
System.out.println(" World"); | ||
System.out.print(42); | ||
System.out.println(); | ||
} | ||
} | ||
""", | ||
""" | ||
class Example { | ||
void test() { | ||
IO.print("Hello"); | ||
IO.println(" World"); | ||
IO.print(42); | ||
IO.println(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void handlesPrintWithComplexExpressions() { | ||
rewriteRun( | ||
java( | ||
""" | ||
class Example { | ||
void test() { | ||
String name = "John"; | ||
int age = 30; | ||
System.out.print("Name: " + name + ", Age: " + age); | ||
System.out.println(String.format("Formatted: %s is %d years old", name, age)); | ||
} | ||
} | ||
""", | ||
""" | ||
class Example { | ||
void test() { | ||
String name = "John"; | ||
int age = 30; | ||
IO.print("Name: " + name + ", Age: " + age); | ||
IO.println(String.format("Formatted: %s is %d years old", name, age)); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void doesNotReplaceSystemErrCalls() { | ||
rewriteRun( | ||
java( | ||
""" | ||
class Example { | ||
void test() { | ||
System.err.print("Error message"); | ||
System.err.println("Error message"); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void doesNotReplaceOtherPrintStreams() { | ||
rewriteRun( | ||
java( | ||
""" | ||
import java.io.PrintStream; | ||
|
||
class Example { | ||
void test() { | ||
PrintStream ps = new PrintStream(System.out); | ||
ps.print("Should not change"); | ||
ps.println("Should not change"); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void doesNotReplacePrintf() { | ||
rewriteRun( | ||
java( | ||
""" | ||
class Example { | ||
void test() { | ||
System.out.printf("Hello%n"); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.