What version of OpenRewrite are you using?
- rewrite-migrate-java: 3.39.0
- rewrite-core: 8.86.1
- rewrite-java: 8.86.1
What is the smallest, simplest way to reproduce the problem?
import org.junit.jupiter.api.Test;
import org.openrewrite.java.migrate.util.UseMapOf;
import org.openrewrite.test.RewriteTest;
import static org.openrewrite.java.Assertions.java;
class UseMapOfTest implements RewriteTest {
@Test
void doesNotConvertLinkedHashMapBuiltWithPutStatements() {
rewriteRun(
spec -> spec.recipe(new UseMapOf()),
//language=java
java(
"""
import java.util.LinkedHashMap;
import java.util.Map;
class Main {
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;
}
}
"""
)
);
}
}
What did you expect to see?
What did you see instead?
The statement form is still rewritten, discarding the insertion-order guarantee (and swapping the LinkedHashMap import for HashMap):
import java.util.HashMap;
import java.util.Map;
class Main {
static Map<String, String> ordered() {
Map<String, String> m =
new HashMap<>(
Map.of(
"a", "1",
"b", "2",
"c", "3"));
return m;
}
}
What version of OpenRewrite are you using?
What is the smallest, simplest way to reproduce the problem?
What did you expect to see?
No change — for the same reason as UseMapOf converts order-sensitive LinkedHashMap to Map.of, silently dropping iteration-order guarantee #1112.
LinkedHashMapguarantees insertion-order iteration;Map.of(...)makes no ordering guarantee (its iteration order is unspecified and deliberately randomized per JVM instance). ConvertingLinkedHashMap(orTreeMap) toMap.ofsilently drops the ordering contract the original code relied on.Skip
UseMapOfforHashMapsubclasses likeLinkedHashMapandTreeMap#1113 already madeUseMapOfskipLinkedHashMap/TreeMap— but only for the anonymous-class (double-brace) initializer form from UseMapOf converts order-sensitive LinkedHashMap to Map.of, silently dropping iteration-order guarantee #1112. The equivalent map built with separateput(...)statements should be skipped for the identical reason and is not.What did you see instead?
The statement form is still rewritten, discarding the insertion-order guarantee (and swapping the
LinkedHashMapimport forHashMap):