Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta
#1,307 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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");
              }
          }
          """
      )
    );
}
Lingua principale
Java
Stelle
159
Fork
130
Merge medio
7h 49m
PR unite (30g)
60

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di openrewrite/rewrite-migrate-java

Tutte le issue di openrewrite/rewrite-migrate-java

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.