Bug and suggested fix.`ReplaceSystemOutWithIOPrint` corrupts syntax when method arguments have bounded or generic return types.
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 55/100
Hướng nghiên cứu
Start with ReplaceSystemOutWithIOPrint.java and its ReplaceSystemOutWithIOPrintTest.java tests. Run the focused test class, then add the supplied generic method invocation reproducer and check that the transformed output preserves the invocation. Done means the regression test passes without compiler placeholder artifacts.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
I checked out all relevant OpenRewrite repo's, fed the AI with existing recipe name and a very careful description. Antigravity with Vertex / Gemini was then tasked to check and test. .Also to create a ticket for it, using your default template. The result is shown beneath.
What version of OpenRewrite are you using?
- Release 8.9.30
rewrite-maven-plugin: 6.50.0rewrite-migrate-java: 3.49.0- JDK: Java 25 (
Temurin-25.0.4.1+1 LTS)
How are you running OpenRewrite?
Maven plugin (mvn org.openrewrite.maven:rewrite-maven-plugin:6.49.0:run) running the recipe org.openrewrite.java.migrate.io.ReplaceSystemOutWithIOPrint (via org.openrewrite.java.migrate.UpgradeToJava25).
What is the smallest, simplest way to reproduce the problem?
When ReplaceSystemOutWithIOPrint runs on a System.out.println(expr) call where the argument has a generic/bounded return type (such as Jackson's <T extends TreeNode> T readValueAsTree()), JavaTemplate.apply("IO.#{}(#{any()})", ...) fails synthetic parameter type attribution (__p1__).
Here is a ready-to-run regression test to add to ReplaceSystemOutWithIOPrintTest.java:
@Test
void replaceSystemOutPrintlnGenericMethodInvocation() {
rewriteRun(
java(
"""
interface TreeNode {}
interface Parser {
<T extends TreeNode> T readValueAsTree();
}
class Example {
void test(Parser p) {
System.out.println(p.readValueAsTree());
}
}
""",
"""
interface TreeNode {}
interface Parser {
<T extends TreeNode> T readValueAsTree();
}
class Example {
void test(Parser p) {
IO.println(p.readValueAsTree());
}
}
"""
)
);
}
What did you expect to see?
The generic invocation should be preserved cleanly in the transformed output:
class Example {
void test(Parser p) {
IO.println(p.readValueAsTree());
}
}
What did you see instead?
Compiler placeholder artifacts (__P__.<error>) leak directly into the generated source file:
class Example {
void test(Parser p) {
IO.println(__P__.<error> & TreeNode > pg.String & TreeNode>/*__p1__*/p());
}
}
When run against real application code using Jackson (System.out.println(p.readValueAsTree())), the recipe produces:
IO.println(__P__.<error> & com .fasterxml.jackson.core.TreeNode >/*__p1__*/p());
which subsequently causes javac compilation failures (<identifier> expected, illegal start of expression).
What is the full stack trace of any errors you encountered?
org.opentest4j.AssertionFailedError: [Unexpected result in "TreeNode.java":
expected:
IO.println(p.readValueAsTree());
but was:
IO.println(__P__.<error> & TreeNode > pg.String & TreeNode>/*__p1__*/p());
]
at app//org.openrewrite.test.RewriteTest.assertContentEquals(RewriteTest.java:702)
at app//org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:584)
at app//org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:159)
at app//org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:154)
at app//org.openrewrite.java.migrate.io.ReplaceSystemOutWithIOPrintTest.replaceSystemOutPrintlnGenericMethodInvocation(ReplaceSystemOutWithIOPrintTest.java:292)
Are you interested in contributing a fix to OpenRewrite?
Yes! We have already implemented and validated the fix locally.
Root Cause:
In ReplaceSystemOutWithIOPrint.java:
String methodName = m.getName().getSimpleName();
return m.getArguments().isEmpty() ?
JavaTemplate.apply( "IO.#{}()", getCursor(), m.getCoordinates().replace(), methodName ) :
JavaTemplate.apply( "IO.#{}(#{any()})", getCursor(), m.getCoordinates().replace(), methodName, m.getArguments().get( 0 ) );
Re-templating the arguments via JavaTemplate re-evaluates the argument's type through synthetic parameters (__p1__), which breaks on bounded type variables / intersection types.
Proposed Solution:
Since method names (print/println) and signatures are identical between System.out and IO, re-templating arguments is unnecessary. Directly replacing m.getSelect() with an IO identifier leaves the arguments AST untouched:
diff --git a/src/main/java/org/openrewrite/java/migrate/io/ReplaceSystemOutWithIOPrint.java b/src/main/java/org/openrewrite/java/migrate/io/ReplaceSystemOutWithIOPrint.java
index b8960f72..268a731d 100644
--- a/src/main/java/org/openrewrite/java/migrate/io/ReplaceSystemOutWithIOPrint.java
+++ b/src/main/java/org/openrewrite/java/migrate/io/ReplaceSystemOutWithIOPrint.java
@@ -51,13 +56,15 @@ public class ReplaceSystemOutWithIOPrint extends Recipe {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
- if (!isSystemOutMethod(m)) {
+ if (!isSystemOutMethod(m) || m.getSelect() == null) {
return m;
}
- String methodName = m.getName().getSimpleName();
- return m.getArguments().isEmpty() ?
- JavaTemplate.apply( "IO.#{}()", getCursor(), m.getCoordinates().replace(), methodName ) :
- JavaTemplate.apply( "IO.#{}(#{any()})", getCursor(), m.getCoordinates().replace(), methodName, m.getArguments().get( 0 ) );
+ JavaType.FullyQualified ioType = JavaType.ShallowClass.build("java.lang.IO");
+ J.Identifier ioIdent = new J.Identifier(Tree.randomId(), m.getSelect().getPrefix(), Markers.EMPTY, emptyList(), "IO", ioType, null);
+ JavaType.Method methodType = m.getMethodType() != null ? m.getMethodType().withDeclaringType(ioType) : null;
+ return m.withSelect(ioIdent).withMethodType(methodType);
}
Testing & Verification performed:
./gradlew test --tests org.openrewrite.java.migrate.io.ReplaceSystemOutWithIOPrintTest: All 12 unit tests pass 100% green (including all existing tests and the new generic reproducer test).- End-to-end application verification: Verified on downstream project under Java 25 (
mvn test-compileandmvn testpass with 0 errors).
- Ngôn ngữ chính
- Java
- Star
- 160
- Fork
- 133
- Merge trung bình
- 7 giờ 49 phút
- Pull request đã merge (30 ngày)
- 60
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của openrewrite/rewrite-migrate-java
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
openrewrite/rewrite-migrate-java#1305 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
openrewrite/rewrite-migrate-java#1319 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
openrewrite/rewrite-migrate-java#1314 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
openrewrite/rewrite-migrate-java#1310 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
openrewrite/rewrite-migrate-java#1307 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của openrewrite/rewrite-migrate-java
Issue tương tự
-
dev/release: verify-release-candidate.sh runs 0 Java tests with Debian's MavenCó thể đã có người làm @kou đã nhận hôm nay. Đang mởType: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
apache/arrow-adbc#4947 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
lysine-dev/okhttp#9814 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
1.20.1-FORGE bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
MehVahdJukaar/WoodGood#1348 ·
-
documentation faq
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100