Recipe UseMapOf generates Map.of() with nullable values or duplicate keys, causing runtime NullPointerException
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 55/100
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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");
}
}
"""
)
);
}
- Lenguaje dominante
- Java
- Estrellas
- 160
- Forks
- 133
- Merge medio
- 7 h 49 min
- PR fusionados (30 d)
- 60
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de openrewrite/rewrite-migrate-java
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
openrewrite/rewrite-migrate-java#1305 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 3/5 1-2 días Aptitud para principiantes 72/100
openrewrite/rewrite-migrate-java#1319 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 3/5 1-2 días Aptitud para principiantes 55/100
openrewrite/rewrite-migrate-java#1314 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 3/5 1-2 días Aptitud para principiantes 55/100
openrewrite/rewrite-migrate-java#1310 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 55/100
openrewrite/rewrite-migrate-java#1304 ·
Los mantenedores suelen responder en 1 día
Todos los issues de openrewrite/rewrite-migrate-java
Issues similares
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
adorsys/keycloak-config-cli#1709 ·
Los mantenedores suelen responder en 1 día
-
! and-bugs
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
osmandapp/OsmAnd#26769 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
NationalSecurityAgency/ghidra#9997 · 2 comentarios ·
Los mantenedores suelen responder en 2 días
-
[Bug] DefaultSerializationExceptionWrapper does not implement Cleanable, so cleanup() is never invoked by codecsPosiblemente ocupada @binbin0325 la tomó hoy. Abiertocomponent/need-triage type/need-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
Los mantenedores suelen responder en 1 día
-
fix: tagged PDF fails with ClassCastException when a Form and its Image share an XObject namePosiblemente ocupada @hnc-jglee la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
opendataloader-project/opendataloader-pdf#830 ·
Los mantenedores suelen responder en 1 día