-
Notifications
You must be signed in to change notification settings - Fork 725
Expand file tree
/
Copy pathStaticMethodCheckSample.java
More file actions
444 lines (361 loc) · 10.7 KB
/
StaticMethodCheckSample.java
File metadata and controls
444 lines (361 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package checks;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.annotation.Nullable;
class GenericClass1<R extends Record> {
private final int recursiveMethod(int x) { // Noncompliant {{Make "recursiveMethod" a "static" method.}}
if (x <= 0) {
return 0;
} else {
return x * recursiveMethod(x - 1);
}
}
}
class Utilities {
private static String magicWord = "magic";
private static String string = magicWord; // coverage
private String otherWord = "other";
public Utilities() {
}
private void register(final Class<?> clazz, final Object converter) {
otherWord = "";
}
private String getMagicWord() { // Noncompliant {{Make "getMagicWord" a "static" method.}}
// ^^^^^^^^^^^^
return magicWord;
}
private static String getMagicWordOK() {
return magicWord;
}
public final String magicWord() { // Noncompliant {{Make "magicWord" a "static" method.}}
// ^^^^^^^^^
return magicWord;
}
public static final String magicWordOK() {
return magicWord;
}
public final String getOtherWordFinal() {
return this.otherWord;
}
private void setMagicWord(String value) { // Noncompliant {{Make "setMagicWord" a "static" method.}}
magicWord = value;
}
private static void setMagicWordOK(String value) {
magicWord = value;
}
private String getClassName() {
return getClass().getSimpleName();
}
private void checkClassLoader() throws IllegalArgumentException {
if (getClass().getClassLoader() != null) {
throw new IllegalArgumentException ("invalid address type");
}
}
private String getOtherWord() {
return otherWord;
}
private int getOtherWordLength() {
return otherWord.length();
}
private String getOtherWord2() {
return this.otherWord;
}
private String getOtherWord3() {
return super.toString();
}
private void setOtherWord(String value) {
otherWord = value;
// coverage
otherWord = value;
}
private int useOnlyArguments(int a, int b) { // Noncompliant
return a + b;
}
private String methodOnlyOnArgument(Object obj) { // Noncompliant
return (obj == null ? null : obj.toString());
}
private String attributeOnArgument(Utilities obj) { // Noncompliant
return obj.otherWord;
}
class Inner {
public Inner(String a, String b) {
}
public final String getMagicWord() {
return "a";
}
public String getOuterOtherWord() {
return Utilities.this.getOtherWord();
}
}
static class Nested {
private String getAWord() { // Noncompliant {{Make "getAWord" a "static" method.}}
return "a";
}
}
public void publicMethod() {
}
public int localAccessViasClass() { // Compliant
return Integer.valueOf(otherWord);
}
private Utilities.Inner createInner() { // Compliant because there is a reference to an inner, non-static class
return new Utilities.Inner("", "");
}
private Utilities.Nested createNested() { // Noncompliant
return new Utilities.Nested();
}
private Map newMap() { // Noncompliant
return new HashMap();
}
private static final int BOOLEAN_TYPE = 1;
private void writeAnyClass(final Class<?> clazz) { // Noncompliant
int primitiveType = 0;
if (Boolean.TYPE.equals(clazz)) {
primitiveType = BOOLEAN_TYPE;
}
}
private <T> int sizeOfMap(Map<T, ?> map) { // Noncompliant
return map.size();
}
private void callMethodOfStaticClass() { // Noncompliant
new checks.Inner.FooBar().myHash();
}
}
class UtilitiesExtension extends Utilities {
public UtilitiesExtension() {
}
private void method() { // Compliant
publicMethod();
}
}
class SerializableExclusions implements Serializable {
private void writeObject(java.io.ObjectOutputStream out) throws IOException {}
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {}
private void readObjectNoData() throws ObjectStreamException {}
private void other() {} // Compliant, empty method
private void recursive() { // Noncompliant
recursive();
}
private void delegateOther() { // Compliant since other() is not static (although it should...)
other();
}
private void readResolve() throws ObjectStreamException {
System.out.println("foo");
}
}
class Inner {
static class FooBar {
enum MyEnum{
FOO;
}
private void plop() { // Noncompliant
Object o = MyEnum.FOO;
}
int myHash() {
return hashCode();
}
}
static class FooBarQix {
private int instanceVariable;
public void instanceMethod() {}
private void foo() { // Compliant: foo cannot be static because it references a non-static method
new Plopp(){
void plop1(){
instanceMethod();
}
};
}
private void init() { // Compliant: foo cannot be static because it references a non-static field
new Plopp(){
void plop1(){
instanceVariable = 0;
}
};
}
}
}
class Plopp {
Plopp(){}
void plop1(){}
}
class SuperClass {
public int bar;
}
class EnclosingInstance extends SuperClass {
interface I { boolean gul(); }
private int foo;
private void foo1() { // Compliant: use of 'EnclosingInstance.this'
new I() {
@Override
public boolean gul() {
return EnclosingInstance.this.foo == 0;
}
};
}
private void foo2() { // Compliant: use of 'EnclosingInstance.super'
new I() {
@Override
public boolean gul() {
return EnclosingInstance.super.bar == 0;
}
};
}
private void foo3() { // Compliant: use of 'EnclosingInstance.this' with fully qualified name
new I() {
@Override
public boolean gul() {
return EnclosingInstance.this.foo == 0;
}
};
}
private void foo4() { // Compliant: use of 'EnclosingInstance.super' with fully qualified name
new I() {
@Override
public boolean gul() {
return EnclosingInstance.super.bar == 0;
}
};
}
}
class ParentClass {
int value;
public int getMagicNumber() {
return value;
}
}
final class ChildClass extends ParentClass {
@Override
public int getMagicNumber() { // OK, overrides parent method
return 42;
}
}
final class FinalClass {
static int magicNumber = 42;
public int getMagicNumber() { // Noncompliant {{Make "getMagicNumber" a "static" method.}} [[quickfixes=qf_public_in_final]]
// ^^^^^^^^^^^^^^
// fix@qf_public_in_final {{Make static}}
// edit@qf_public_in_final [[sc=10;ec=10]]{{static }}
return magicNumber;
}
int getMagicNumber2() { // Noncompliant [[quickfixes=qf_public_in_final2]]
// ^^^^^^^^^^^^^^^
// fix@qf_public_in_final2 {{Make static}}
// edit@qf_public_in_final2 [[sc=3;ec=3]]{{static }}
return magicNumber;
}
synchronized int getMagicNumber3() { // Noncompliant [[quickfixes=qf_public_in_final3]]
// ^^^^^^^^^^^^^^^
// fix@qf_public_in_final3 {{Make static}}
// edit@qf_public_in_final3 [[sc=3;ec=3]]{{static }}
return magicNumber;
}
public static int getMagicNumberOK() {
return magicNumber;
}
}
enum SomeEnum {
A,
B,
C;
final SomeEnum getOne() {
return A;
}
}
class StaticMethodCheckSampleQuickFix {
private static String magicWord = "magic";
private String getMagicWord() { // Noncompliant [[quickfixes=qf_private]]
// ^^^^^^^^^^^^
// fix@qf_private {{Make static}}
// edit@qf_private [[sc=11;ec=11]]{{static }}
return magicWord;
}
@Nullable
private String getMagicWord2() { // Noncompliant [[quickfixes=qf_private2]]
// ^^^^^^^^^^^^^
// fix@qf_private2 {{Make static}}
// edit@qf_private2 [[sc=11;ec=11]]{{static }}
return magicWord;
}
public final String magicWord() { // Noncompliant [[quickfixes=qf_public_final]]
// ^^^^^^^^^
// fix@qf_public_final {{Make static}}
// edit@qf_public_final [[sc=10;ec=10]]{{static }}
return magicWord;
}
private synchronized String magicWordSynchronized() { // Noncompliant [[quickfixes=qf_private_synchronized]]
// ^^^^^^^^^^^^^^^^^^^^^
// fix@qf_private_synchronized {{Make static}}
// edit@qf_private_synchronized [[sc=11;ec=11]]{{static }}
return magicWord;
}
// Order is not following the convention: add it before the first modifier that should come after static
synchronized private String magicWordSynchronized2() { // Noncompliant [[quickfixes=qf_private_synchronized2]]
// ^^^^^^^^^^^^^^^^^^^^^^
// fix@qf_private_synchronized2 {{Make static}}
// edit@qf_private_synchronized2 [[sc=3;ec=3]]{{static }}
return magicWord;
}
private @Nullable synchronized String magicWordSynchronized3() { // Noncompliant [[quickfixes=qf_private_synchronized3]]
// ^^^^^^^^^^^^^^^^^^^^^^
// fix@qf_private_synchronized3 {{Make static}}
// edit@qf_private_synchronized3 [[sc=21;ec=21]]{{static }}
return magicWord;
}
}
class ReturnTypeCheck {
static class SingleTypeVar<T> {
private List<T> requiresTypeVar() { // Compliant
return List.of();
}
private List<String> doesNotRequireTypeVar() { // Noncompliant
return List.of();
}
private static List<String> staticDoesNotRequireTypeVar() { // Compliant
return List.of();
}
private <U> List<U> requiresAnotherTypeVar() { // Noncompliant
return List.of();
}
private <T> List<T> requiresAnotherTypeVarShadowed() { // Noncompliant
return List.of();
}
}
static class TwoTypeVar<T1, T2> {
private List<T1> oneUsed1() { // Compliant
return List.of();
}
private <U> Map<T1, U> oneUsed2() { // Compliant
return Map.of();
}
private static <T1, U> Map<T1, U> bothShadowed() { // Compliant
return Map.of();
}
private List<String> noneUsed() { // Noncompliant
return List.of();
}
private static List<String> staticNoneUsed() { // Compliant
return List.of();
}
}
static class Nested<T> {
private List<List<T>> requiresNestedTypeVar() { // Compliant
return List.of();
}
}
}
// Note `final` on the class indicating that main methods will not be overridden.
// Without it, the check would apply only to private methods.
final public class StaticMethodCheckSample {
void main() { // Compliant
System.out.println("StaticMethodCheckSample no arg main");
}
void main(String[] args) { // Compliant
System.out.println("StaticMethodCheckSample main with args");
}
void main(int i) { // Noncompliant
System.out.println("i = " + i);
}
}