Skip to content

Skip UseMapOf for HashMap subclasses like LinkedHashMap and TreeMap#1113

Merged
timtebeek merged 1 commit into
mainfrom
tim/issue-1112-migrate-java
May 28, 2026
Merged

Skip UseMapOf for HashMap subclasses like LinkedHashMap and TreeMap#1113
timtebeek merged 1 commit into
mainfrom
tim/issue-1112-migrate-java

Conversation

@timtebeek
Copy link
Copy Markdown
Member

@timtebeek timtebeek commented May 28, 2026

Map.of(..) makes no iteration-order guarantee, so converting an order-sensitive LinkedHashMap (or TreeMap) silently drops the ordering contract callers rely on. The NEW_HASH_MAP matcher used matchOverrides=true to catch the new HashMap<>() {{...}} anonymous-subclass idiom, which also caused it to match non-anonymous HashMap subclasses. Added a TypeUtils.isOfClassType check on the declared class so only java.util.HashMap is rewritten, with regression tests for LinkedHashMap and TreeMap.

…eeMap`

`Map.of(..)` does not preserve iteration order, so converting order-sensitive
maps silently drops their ordering contract.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite May 28, 2026
@timtebeek timtebeek merged commit baceee1 into main May 28, 2026
1 check passed
@timtebeek timtebeek deleted the tim/issue-1112-migrate-java branch May 28, 2026 10:23
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

UseMapOf converts order-sensitive LinkedHashMap to Map.of, silently dropping iteration-order guarantee

1 participant