Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Recipe UseMapOf generates Map.of() with nullable values or duplicate keys, causing runtime NullPointerException

オープン
#1,307 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
55/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
java
領域
backend

調査の方向性

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 の本文から書いたものです。

説明

bug

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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

openrewrite/rewrite-migrate-java のほかの issue

openrewrite/rewrite-migrate-java の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。