Skip to content

Commit 262d47b

Browse files
committed
UseMapOf
1 parent 7e55833 commit 262d47b

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.lang;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Recipe;
20+
import org.openrewrite.java.JavaTemplate;
21+
import org.openrewrite.java.JavaVisitor;
22+
import org.openrewrite.java.MethodMatcher;
23+
import org.openrewrite.java.tree.Expression;
24+
import org.openrewrite.java.tree.J;
25+
import org.openrewrite.java.tree.Statement;
26+
27+
import java.util.ArrayList;
28+
import java.util.List;
29+
import java.util.StringJoiner;
30+
31+
public class UseMapOf extends Recipe {
32+
private static final MethodMatcher NEW_HASH_MAP = new MethodMatcher("java.util.HashMap <constructor>()", true);
33+
private static final MethodMatcher MAP_PUT = new MethodMatcher("java.util.Map put(..)", true);
34+
35+
@Override
36+
public String getDisplayName() {
37+
return "Use `Map.of(..)` where possible";
38+
}
39+
40+
@Override
41+
public String getDescription() {
42+
return "This succinct syntax was introduced in Java 10.";
43+
}
44+
45+
@Override
46+
protected JavaVisitor<ExecutionContext> getVisitor() {
47+
return new JavaVisitor<ExecutionContext>() {
48+
@Override
49+
public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
50+
J.NewClass n = (J.NewClass) super.visitNewClass(newClass, ctx);
51+
J.Block body = n.getBody();
52+
if (NEW_HASH_MAP.matches(n) && body != null) {
53+
if (body.getStatements().size() == 1) {
54+
Statement statement = body.getStatements().get(0);
55+
if (statement instanceof J.Block) {
56+
List<Expression> args = new ArrayList<>();
57+
StringJoiner mapOf = new StringJoiner(", ", "Map.of(", ")");
58+
for (Statement stat : ((J.Block) statement).getStatements()) {
59+
if (stat instanceof J.MethodInvocation && MAP_PUT.matches((Expression) stat)) {
60+
J.MethodInvocation put = (J.MethodInvocation) stat;
61+
args.addAll(put.getArguments());
62+
mapOf.add("#{}");
63+
mapOf.add("#{}");
64+
} else {
65+
return n;
66+
}
67+
}
68+
69+
maybeRemoveImport("java.util.HashMap");
70+
maybeAddImport("java.util.Map");
71+
return autoFormat(n.withTemplate(
72+
JavaTemplate
73+
.builder(this::getCursor, mapOf.toString())
74+
.imports("java.util.Map")
75+
.build(),
76+
n.getCoordinates().replace(),
77+
args.toArray()
78+
), ctx);
79+
}
80+
}
81+
}
82+
return n;
83+
}
84+
};
85+
}
86+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.lang
17+
18+
import org.junit.jupiter.api.Test
19+
import org.openrewrite.Recipe
20+
import org.openrewrite.java.JavaRecipeTest
21+
22+
class UseMapOfTest: JavaRecipeTest {
23+
override val recipe: Recipe
24+
get() = UseMapOf()
25+
26+
@Test
27+
fun anonymousClass() = assertChanged(
28+
before = """
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
32+
class Test {
33+
Map<String, String> m = new HashMap<>() {{
34+
put("stru", "menta");
35+
put("mod", "erne");
36+
}};
37+
}
38+
""",
39+
after = """
40+
import java.util.Map;
41+
42+
class Test {
43+
Map<String, String> m = Map.of("stru", "menta", "mod", "erne");
44+
}
45+
"""
46+
)
47+
}

0 commit comments

Comments
 (0)