Skip to content

Commit b015e4d

Browse files
authored
Enhance ExplicitInitialization to handle anonymous classes (#522)
* test: Add test for removing explicit initialization in anonymous subclasses * refactor: Enhance type checks for class declarations and new classes in ExplicitInitializationVisitor * test: Add test to ignore explicit initialization in anonymous subclasses
1 parent 909a22d commit b015e4d

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,18 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
4848
return v;
4949
} else {
5050
J maybeClassDecl = maybeBlockOrGType
51-
.getParentTreeCursor() // maybe J.ClassDecl
51+
.getParentTreeCursor() // maybe J.ClassDecl or J.NewClass
5252
.getValue();
53-
if (!(maybeClassDecl instanceof J.ClassDeclaration) ||
54-
J.ClassDeclaration.Kind.Type.Class != ((J.ClassDeclaration) maybeClassDecl).getKind() ||
55-
!(variableDeclsCursor.getValue() instanceof J.VariableDeclarations)) {
53+
if (!(maybeClassDecl instanceof J.ClassDeclaration || maybeClassDecl instanceof J.NewClass)) {
54+
return v;
55+
}
56+
57+
if (!(maybeClassDecl instanceof J.NewClass) &&
58+
J.ClassDeclaration.Kind.Type.Class != ((J.ClassDeclaration) maybeClassDecl).getKind()) {
59+
return v;
60+
}
61+
62+
if (!(variableDeclsCursor.getValue() instanceof J.VariableDeclarations)) {
5663
return v;
5764
}
5865
}

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

+79
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,83 @@ class Test {
195195
)
196196
);
197197
}
198+
199+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/109")
200+
@Test
201+
void removeExplicitInitializationInAnonymousSubClasses() {
202+
rewriteRun(
203+
//language=java
204+
java(
205+
"""
206+
class Test {
207+
Object o = new Object() {
208+
private int a = 0;
209+
private long b = 0L;
210+
private short c = 0;
211+
private int d = 1;
212+
private long e = 2L;
213+
private int f;
214+
private char g = '\\0';
215+
216+
private boolean h = false;
217+
private boolean i = true;
218+
219+
private Object j = new Object();
220+
private Object k = null;
221+
222+
int[] l = null;
223+
int[] m = new int[0];
224+
225+
private final Long n = null;
226+
};
227+
}
228+
""",
229+
"""
230+
class Test {
231+
Object o = new Object() {
232+
private int a;
233+
private long b;
234+
private short c;
235+
private int d = 1;
236+
private long e = 2L;
237+
private int f;
238+
private char g;
239+
240+
private boolean h;
241+
private boolean i = true;
242+
243+
private Object j = new Object();
244+
private Object k;
245+
246+
int[] l;
247+
int[] m = new int[0];
248+
249+
private final Long n = null;
250+
};
251+
}
252+
"""
253+
)
254+
);
255+
}
256+
257+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/109")
258+
@Test
259+
void ignoreInAnonymousSubClasses() {
260+
rewriteRun(
261+
//language=java
262+
java(
263+
"""
264+
class Test {
265+
Object o = new Object() {
266+
private final boolean b = false;
267+
268+
private void method() {
269+
int i = 0;
270+
}
271+
};
272+
}
273+
"""
274+
)
275+
);
276+
}
198277
}

0 commit comments

Comments
 (0)