Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/org/openrewrite/java/migrate/util/UseMapOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ private String matchingTargetName(J.VariableDeclarations decl) {
if (!NEW_HASH_MAP.matches(nc)) {
return null;
}
// Skip `HashMap` subclasses like `LinkedHashMap`/`TreeMap`: `Map.of(..)` makes no
// iteration-order guarantee, so absorbing their `put(..)` chain would silently drop
// the ordering contract the original code relied on (issue #1163, matching #1113).
if (!TypeUtils.isOfClassType(nc.getClazz() != null ? nc.getClazz().getType() : null, "java.util.HashMap")) {
return null;
}
if (nc.getBody() != null) {
return null;
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/openrewrite/java/migrate/util/UseMapOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,30 @@ class Test {
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/1163")
@Test
void doNotChangeLinkedHashMapBuiltWithPutStatements() {
//language=java
rewriteRun(
java(
"""
import java.util.LinkedHashMap;
import java.util.Map;

class Test {
static Map<String, String> ordered() {
Map<String, String> m = new LinkedHashMap<>();
m.put("a", "1");
m.put("b", "2");
m.put("c", "3");
return m;
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/566")
@Test
void changeDoubleBraceInitForNonStringTypes() {
Expand Down
Loading