Recipe UseMapOf generates Map.of() with nullable values or duplicate keys, causing runtime NullPointerException
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 55/100
Research direction
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.
Written by the indexing model from the issue text.
Description
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");
}
}
"""
)
);
}
- Dominant language
- Java
- Stars
- 162
- Forks
- 139
- Avg merge
- 10h 28m
- Merged PRs (30d)
- 121
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from openrewrite/rewrite-migrate-java
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 72/100
openrewrite/rewrite-migrate-java#1383 ·
Maintainers usually reply within 1 day
-
good first issue
Difficulty 4/5 3-5 days Newbie friendliness 30/100
openrewrite/rewrite-migrate-java#1330 · 6 comments ·
Maintainers usually reply within 1 day
-
UseMapOf: Skip refactoring large maps to Map.ofEntries to prevent javac compiler hangs (add maxEntries option)Possibly taken @Yuren0312 claimed this 17 days ago. Openbug
Difficulty 3/5 1-2 days Newbie friendliness 55/100
openrewrite/rewrite-migrate-java#1310 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 68/100
openrewrite/rewrite-migrate-java#1246 ·
Maintainers usually reply within 1 day
-
Feedback from a 152k-line Java 8 to 25 migration: the silent behavioural changes that cost the most, and which have no recipePossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 5/5 Over a week Newbie friendliness 32/100
openrewrite/rewrite-migrate-java#1227 · 4 comments · 1 reaction ·
Maintainers usually reply within 1 day
All issues in openrewrite/rewrite-migrate-java
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
aws/aws-emulator#30 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 4 days
-
Bug: Minor Status: Triage
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
GTNewHorizons/GT-New-Horizons-Modpack#27664 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
openrewrite/rewrite#9835 ·
Maintainers usually reply within 1 day
-
serviceability
Difficulty 1/5 Under an hour Newbie friendliness 65/100
OpenLiberty/open-liberty#36348 ·
Maintainers usually reply within 1 day