Skip to content

UseMapOf still converts LinkedHashMap to Map.of for the statement/put() form #1163

Description

@protocol7

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;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions