Skip to content

Commit 7e5ec9f

Browse files
arodionovtimtebeek
andauthored
Fix missing constructor argument issue (#492)
* Fix missing constructor argument issue - fixes #356 - added tests * Remove unused JavaTemplate import --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 9c80780 commit 7e5ec9f

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

src/main/java/org/openrewrite/staticanalysis/NoDoubleBraceInitialization.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.openrewrite.*;
1919
import org.openrewrite.internal.ListUtils;
2020
import org.openrewrite.java.JavaIsoVisitor;
21-
import org.openrewrite.java.JavaTemplate;
2221
import org.openrewrite.java.search.UsesType;
2322
import org.openrewrite.java.tree.*;
2423
import org.openrewrite.marker.Markers;
@@ -123,10 +122,7 @@ public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
123122
Cursor namedVarCursor = getCursor().dropParentUntil(J.VariableDeclarations.NamedVariable.class::isInstance);
124123
namedVarCursor.putMessage("DROP_INITIALIZER", Boolean.TRUE);
125124

126-
fq = fq.getSupertype();
127-
String newInitializer = " new " + fq.getClassName() + "<>();";
128-
JavaTemplate template = JavaTemplate.builder(newInitializer).imports(fq.getFullyQualifiedName()).build();
129-
nc = template.apply(getCursor(), nc.getCoordinates().replace());
125+
nc = nc.withBody(null);
130126
initStatements = addSelectToInitStatements(initStatements, var.getName(), ctx);
131127
initStatements.add(0, new J.Assignment(Tree.randomId(), Space.EMPTY, Markers.EMPTY, var.getName().withId(UUID.randomUUID()), JLeftPadded.build(nc), fq));
132128
parentBlockCursor.computeMessageIfAbsent("INIT_STATEMENTS", v -> new HashMap<Statement, List<Statement>>()).put(varDeclsCursor.getValue(), initStatements);

src/test/java/org/openrewrite/staticanalysis/NoDoubleBraceInitializationTest.java

+64
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,70 @@ public void defaults(RecipeSpec spec) {
3030
spec.recipe(new NoDoubleBraceInitialization());
3131
}
3232

33+
@Test
34+
void dropsConstructorCollectionParameterInMethod() {
35+
rewriteRun(
36+
//language=java
37+
java(
38+
"""
39+
import java.util.*;
40+
41+
class A {
42+
void foo() {
43+
Map<String, String> a = Map.of("foo", "bar");
44+
Map<String, String> b = new HashMap<>(a) {{
45+
put("irrelevantKey", "irrelevantValue");
46+
}};
47+
}
48+
}
49+
""",
50+
"""
51+
import java.util.*;
52+
53+
class A {
54+
void foo() {
55+
Map<String, String> a = Map.of("foo", "bar");
56+
Map<String, String> b = new HashMap<>(a);
57+
b.put("irrelevantKey", "irrelevantValue");
58+
}
59+
}
60+
"""
61+
)
62+
);
63+
}
64+
65+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/356")
66+
@Test
67+
void dropsConstructorCollectionParameterInClass() {
68+
rewriteRun(
69+
//language=java
70+
java(
71+
"""
72+
import java.util.*;
73+
74+
class A {
75+
Map<String, String> a = Map.of("foo", "bar");
76+
Map<String, String> b = new HashMap<>(a) {{
77+
put("irrelevantKey", "irrelevantValue");
78+
}};
79+
}
80+
""",
81+
"""
82+
import java.util.*;
83+
84+
class A {
85+
Map<String, String> a = Map.of("foo", "bar");
86+
Map<String, String> b;
87+
{
88+
b = new HashMap<>(a);
89+
b.put("irrelevantKey", "irrelevantValue");
90+
}
91+
}
92+
"""
93+
)
94+
);
95+
}
96+
3397
@Issue("https://github.com/openrewrite/rewrite/issues/2674")
3498
@Test
3599
void possibleMistakenlyMissedAddingToCollection() {

0 commit comments

Comments
 (0)