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
19 changes: 19 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 @@ -111,6 +111,12 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
TypeUtils.isOfClassType(n.getClazz() != null ? n.getClazz().getType() : null, "java.util.HashMap")) {
Statement statement = body.getStatements().get(0);
if (statement instanceof J.Block) {
// Skip when the result is assigned to a concrete `HashMap` declared type rather
// than `Map`: the immutable `Map.of(..)` is not assignable to `HashMap`, and
// `HashMap`-only methods may be invoked on the variable later (issue #1148).
if (isAssignedToConcreteHashMap()) {
return n;
}
List<Statement> putStatements = ((J.Block) statement).getStatements();
List<Expression> args = new ArrayList<>();
boolean useEntries = putStatements.size() > 10;
Expand Down Expand Up @@ -194,6 +200,19 @@ private J reattachPairPrefixes(J applied, List<J.MethodInvocation> puts, boolean
return nc.withArguments(Collections.singletonList(mapCall.withArguments(withPrefixes)));
}

/**
* Returns {@code true} when the {@code new HashMap<>() {{ ... }}} currently being visited is
* the initializer of a variable whose declared type is the concrete {@code java.util.HashMap}
* rather than the {@code Map} interface. In that case the immutable {@code Map.of(..)} result
* would not be assignable, and {@code HashMap}-specific methods may be used later, so the
* rewrite is skipped (issue #1148).
*/
private boolean isAssignedToConcreteHashMap() {
Object parent = getCursor().getParentTreeCursor().getValue();
return parent instanceof J.VariableDeclarations.NamedVariable &&
TypeUtils.isOfClassType(((J.VariableDeclarations.NamedVariable) parent).getType(), "java.util.HashMap");
}

@Override
public J visitBlock(J.Block block, ExecutionContext ctx) {
Map<UUID, List<J.MethodInvocation>> rewrites = new HashMap<>();
Expand Down
42 changes: 42 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 @@ -205,6 +205,48 @@ class Test {
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/1148")
@Test
void doNotChangeConcreteHashMapField() {
//language=java
rewriteRun(
spec -> spec.allSources(s -> s.markers(javaVersion(25))),
java(
"""
import java.util.HashMap;

class Main {
private static final HashMap<String, String> VALUES = new HashMap<String, String>() {{
put("key", "value");
}};
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/1148")
@Test
void doNotChangeConcreteHashMapLocalVariable() {
//language=java
rewriteRun(
java(
"""
import java.util.HashMap;

class Main {
void m() {
HashMap<String, Integer> ages = new HashMap<>() {{
put("Bob", 42);
put("alice", 30);
}};
}
}
"""
)
);
}

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