UseMapOf: Skip refactoring large maps to Map.ofEntries to prevent javac compiler hangs (add maxEntries option)
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ó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 55/100
Hướng nghiên cứu
Start with src/main/java/org/openrewrite/java/migrate/util/UseMapOf.java and its tests in src/test/java/org/openrewrite/java/migrate/util/UseMapOfTest.java; read how the recipe collects put statements for both initialization patterns. Check the existing tests and add coverage for a configurable entry limit, including maps just above and at the limit. Done when oversized maps are skipped and maps within the configured limit still rewrite as expected.
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.
UseMapOf: Skip refactoring large maps to Map.ofEntries to prevent javac compiler hangs (add maxEntries option)
What version of OpenRewrite are you using?
I am using:
- OpenRewrite v8.93.0
rewrite-maven-pluginv6.50.0
How are you running OpenRewrite?
I am using the Maven plugin in a multi-module project:
What is the smallest, simplest way to reproduce the problem?
When refactoring large static lookup maps (e.g. >50–100 entries):
import java.util.HashMap;
import java.util.Map;
class LargeLookup {
private static final Map<String, Integer> CODE_MAP = new HashMap<>();
static {
CODE_MAP.put("k1", 1);
CODE_MAP.put("k2", 2);
// ... 150+ more entries
CODE_MAP.put("k150", 150);
}
}
What did you expect to see?
Either:
- Maps exceeding a reasonable size threshold (e.g. 50 entries) are left as
HashMap.put(..)statements, or - An optional configuration parameter (e.g.
maxEntries, default50) allows capping the recipe threshold.
What did you see instead?
UseMapOf converts the sequence into:
private static final Map<String, Integer> CODE_MAP = new HashMap<>(Map.ofEntries(
Map.entry("k1", 1),
Map.entry("k2", 2),
// ... 150+ varargs entries
));
With 100+ generic Map.entry(..) varargs arguments, javac generic type inference suffers exponential slowdowns during compilation (com.sun.tools.javac.comp.Attr), causing downstream builds and CI pipelines to hang or time out.
What is the full stack trace of any errors you encountered?
N/A (Compilation hang in downstream javac, not an exception thrown by OpenRewrite).
Proposed Fix & Tested Patch
Add an optional @Option parameter maxEntries (default 50) to UseMapOf to guard both anonymous-class and prose-pattern initializations.
Below is a tested, self-contained git patch against openrewrite/rewrite-migrate-java (tested locally with 100% green test suite):
diff --git a/src/main/java/org/openrewrite/java/migrate/util/UseMapOf.java b/src/main/java/org/openrewrite/java/migrate/util/UseMapOf.java
--- a/src/main/java/org/openrewrite/java/migrate/util/UseMapOf.java
+++ b/src/main/java/org/openrewrite/java/migrate/util/UseMapOf.java
@@ -18,6 +18,8 @@ package org.openrewrite.java.migrate.util;
import lombok.Getter;
+import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
+import org.openrewrite.Option;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
@@ -35,6 +37,7 @@ public class UseMapOf extends Recipe {
private static final MethodMatcher MAP_PUT = new MethodMatcher("java.util.Map put(..)", true);
private static final String PROSE_REWRITES_KEY = "use-map-of.prose-rewrites";
+ private static final int DEFAULT_MAX_ENTRIES = 50;
+
+ @Option(displayName = "Maximum entries",
+ description = "The maximum number of entries to convert into `Map.of(..)` or `Map.ofEntries(..)`. " +
+ "Maps containing more entries than this threshold will not be refactored, " +
+ "preventing javac generic type inference compiler hangs and excessive varargs allocations. Defaults to 50.",
+ example = "50",
+ required = false)
+ @Nullable
+ Integer maxEntries;
@Getter
final String displayName = "Prefer `Map.of(..)`";
@@ -49,6 +52,14 @@ public class UseMapOf extends Recipe {
"- A `new HashMap<>()` declaration followed by a chain of `target.put(k, v)` statements, " +
"which is collapsed to `new HashMap<>(Map.of(..))` (or `new HashMap<>(Map.ofEntries(..))`) — " +
"preserving the mutable `HashMap`.";
+ public UseMapOf() {
+ this(null);
+ }
+
+ public UseMapOf(@Nullable final Integer maxEntries) {
+ this.maxEntries = maxEntries;
+ }
+
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
@@ -118,6 +129,10 @@ public class UseMapOf extends Recipe {
if (isAssignedToConcreteHashMap()) {
return n;
}
List<Statement> putStatements = ((J.Block) statement).getStatements();
+ final int limit = maxEntries != null ? maxEntries : DEFAULT_MAX_ENTRIES;
+ if (putStatements.size() > limit) {
+ return n;
+ }
List<Expression> args = new ArrayList<>();
@@ -321,7 +336,8 @@ public class UseMapOf extends Recipe {
break;
}
}
- if (puts.size() >= 2 && initializer != null) {
+ final int limit = maxEntries != null ? maxEntries : DEFAULT_MAX_ENTRIES;
+ if (puts.size() >= 2 && puts.size() <= limit && initializer != null) {
rewrites.put(initializer.getId(), puts);
absorbedPutIds.addAll(absorbedHere);
i = j;
diff --git a/src/test/java/org/openrewrite/java/migrate/util/UseMapOfTest.java b/src/test/java/org/openrewrite/java/migrate/util/UseMapOfTest.java
--- a/src/test/java/org/openrewrite/java/migrate/util/UseMapOfTest.java
+++ b/src/test/java/org/openrewrite/java/migrate/util/UseMapOfTest.java
@@ -574,4 +574,86 @@ class UseMapOfTest implements RewriteTest {
)
);
}
+
+ @Test
+ void customMaxEntriesThresholdHonored() {
+ //language=java
+ rewriteRun(
+ spec -> spec.recipe(new UseMapOf(3)),
+ java(
+ """
+ import java.util.HashMap;
+ import java.util.Map;
+
+ class Test {
+ void m() {
+ Map<String, Integer> map = new HashMap<>();
+ map.put("k1", 1);
+ map.put("k2", 2);
+ map.put("k3", 3);
+ map.put("k4", 4);
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void customMaxEntriesWithinThresholdRewritten() {
+ //language=java
+ rewriteRun(
+ spec -> spec.recipe(new UseMapOf(3)),
+ java(
+ """
+ import java.util.HashMap;
+ import java.util.Map;
+
+ class Test {
+ void m() {
+ Map<String, Integer> map = new HashMap<>();
+ map.put("k1", 1);
+ map.put("k2", 2);
+ map.put("k3", 3);
+ }
+ }
+ """,
+ """
+ import java.util.HashMap;
+ import java.util.Map;
+
+ class Test {
+ void m() {
+ Map<String, Integer> map = new HashMap<>(Map.of(
+ "k1", 1,
+ "k2", 2,
+ "k3", 3));
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void proseExceedsDefaultMaxEntriesNotRewritten() {
+ final StringBuilder puts = new StringBuilder();
+ for (int i = 0; i < 51; i++) {
+ puts.append(" map.put(\"k").append(i).append("\", ").append(i).append(");\n");
+ }
+ //language=java
+ rewriteRun(
+ java(
+ "import java.util.HashMap;\n" +
+ "import java.util.Map;\n\n" +
+ "class Test {\n" +
+ " void m() {\n" +
+ " Map<String, Integer> map = new HashMap<>();\n" +
+ puts +
+ " }\n" +
+ "}\n"
+ )
+ );
+ }
}
- 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ó 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
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 55/100
openrewrite/rewrite-migrate-java#1304 ·
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ự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
checkstyle/checkstyle#22145 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
rset command throws NPE in ResultsFormat.guessSyntaxCó thể đã có người làm @Ostrzyciel đã nhận hôm nay. Đang mở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
-
enhancement good first issue help wanted
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
objectionary/eo#9593 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100