File tree 2 files changed +90
-4
lines changed
main/java/org/openrewrite/staticanalysis
test/java/org/openrewrite/staticanalysis
2 files changed +90
-4
lines changed Original file line number Diff line number Diff line change @@ -48,11 +48,18 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
48
48
return v ;
49
49
} else {
50
50
J maybeClassDecl = maybeBlockOrGType
51
- .getParentTreeCursor () // maybe J.ClassDecl
51
+ .getParentTreeCursor () // maybe J.ClassDecl or J.NewClass
52
52
.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 )) {
56
63
return v ;
57
64
}
58
65
}
Original file line number Diff line number Diff line change @@ -195,4 +195,83 @@ class Test {
195
195
)
196
196
);
197
197
}
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
+ }
198
277
}
You can’t perform that action at this time.
0 commit comments