Recipe UseMapOf generates Map.of() with nullable values or duplicate keys, causing runtime NullPointerException
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ó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 55/100
Hướng nghiên cứu
Start with the UseMapOf recipe and UseMapOfTest.java, which the issue names, and trace how put arguments are checked before conversion to Map.of. Review the existing null check and add coverage for nullable values and duplicate keys; the issue includes a parameter example and expected behavior. Done means unsafe maps are left unchanged while safe maps still rewrite, with the regression tests passing.
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?
- rewrite-core: 8.93.0
- rewrite-maven-plugin: 6.50.0
How are you running OpenRewrite?
Maven plugin with recipe org.openrewrite.java.migrate.util.UseMapOf.
What is the smallest, simplest way to reproduce the problem?
UseMapOf currently only checks !J.Literal.isLiteralValue(arg, null). When parameters, method calls, or duplicate keys are passed to .put(k, v), it rewrites to Map.of(...), which strictly rejects nulls (NPE per JEP 269) and duplicate keys (IllegalArgumentException):
import java.util.HashMap;
import java.util.Map;
class Test {
Map<String, Object> create(String dataOwner) {
Map<String, Object> map = new HashMap<>();
map.put("owner", dataOwner); // nullable parameter
map.put("id", "123");
return map;
}
}
What did you expect to see?
Do not rewrite into Map.of(...) unless keys and values are statically guaranteed non-null, and keys are distinct.
What did you see instead?
Rewritten to new HashMap<>(Map.of("owner", dataOwner, "id", "123")), throwing NullPointerException at runtime when dataOwner is null:
java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "val" is null
at java.base/java.util.KeyValueHolder.<init>(KeyValueHolder.java:54)
at java.base/java.util.Map.of(Map.java:1320)
Solution in UseMapOf.java
Abort rewrite unless both key and value satisfy isKnownNonNull(Expression) and keys are unique:
if (!isKnownNonNull(key) || !isKnownNonNull(value)) {
hasUnsafePut = true;
break;
}
isKnownNonNull returns true only for:
- Non-null J.Literal, primitives, J.NewClass, J.NewArray, String concatenation (+)
- Class literals (X.class), Enums, and non-null static final constants
Regression Test in UseMapOfTest.java
@Test
void parameterInPutNotRewritten() {
rewriteRun(
java(
"""
import java.util.HashMap;
import java.util.Map;
class Test {
void m(String dataOwner) {
Map<String, Object> map = new HashMap<>();
map.put("dataOwner", dataOwner);
map.put("id", "6");
}
}
"""
)
);
}
- Ngôn ngữ chính
- Java
- Star
- 159
- Fork
- 130
- 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ó 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ự
-
human-required sync-failed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Azure/osdu-spi-file#63 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
frontend quick win
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
CodeForPhilly/benefit-decision-toolkit#571 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
approved
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
checkstyle/checkstyle#22129 ·
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 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Add Hardcharger JDBC driverCó thể đã có người làm @prmoore77 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày