Recipe UseMapOf generates Map.of() with nullable values or duplicate keys, causing runtime NullPointerException
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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");
}
}
"""
)
);
}
- 主要言語
- Java
- スター
- 159
- フォーク
- 130
- 平均マージ
- 7時間 49分
- マージ済み PR(30日)
- 60
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
openrewrite/rewrite-migrate-java のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
openrewrite/rewrite-migrate-java#1305 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
openrewrite/rewrite-migrate-java#1319 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
openrewrite/rewrite-migrate-java#1314 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
openrewrite/rewrite-migrate-java#1310 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 55/100
openrewrite/rewrite-migrate-java#1304 ·
メンテナーはふだん 1 日以内に返信
openrewrite/rewrite-migrate-java の issue をすべて見る
似ている issue
-
human-required sync-failed
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
Azure/osdu-spi-file#63 ·
メンテナーはふだん 1 日以内に返信
-
frontend quick win
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
CodeForPhilly/benefit-decision-toolkit#571 ·
メンテナーはふだん 1 日以内に返信
-
approved
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
checkstyle/checkstyle#22129 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
Add Hardcharger JDBC driver対応中かも @prmoore77 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信