Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

未关闭
#1,307 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
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
星标
160
派生
133
平均合并
7 小时 49 分钟
30 天内合并 PR
60

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

openrewrite/rewrite-migrate-java 的其他 Issue

查看 openrewrite/rewrite-migrate-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。