View Javadoc
1   /*
2   RequireThis
3   checkFields = (default)true
4   checkMethods = (default)true
5   validateOnlyOverlapping = false
6   
7   
8   */
9   
10  package com.puppycrawl.tools.checkstyle.checks.coding.requirethis;
11  
12  import java.util.*;
13  
14  public class InputRequireThisValidateOnlyOverlappingFalse {
15  
16      private static String fieldStatic = "fieldStatic";
17  
18      private final long fieldFinal1;
19      private final long fieldFinal2;
20      private final BitSet fieldFinal3;
21  
22      private String field1;
23      private String field2;
24      private String field3 = "some string";
25      private boolean booleanField;
26      private int intField;
27  
28      public InputRequireThisValidateOnlyOverlappingFalse(String field1) {
29          field1 = field1; // violation 'Reference to instance variable 'field1' needs "this.".'
30          fieldFinal1 = 0; // violation '.* variable 'fieldFinal1' needs "this.".'
31          fieldFinal2 = 0; // violation '.* variable 'fieldFinal2' needs "this.".'
32          fieldFinal3 = new BitSet(); // violation '.* variable 'fieldFinal3' needs "this.".'
33      }
34  
35      public InputRequireThisValidateOnlyOverlappingFalse(long value) {
36          fieldFinal1 = value; // violation '.* variable 'fieldFinal1' needs "this.".'
37          fieldFinal2 = 0; // violation '.* variable 'fieldFinal2' needs "this.".'
38          fieldFinal3 = new BitSet(); // violation '.* variable 'fieldFinal3' needs "this.".'
39      }
40  
41      public InputRequireThisValidateOnlyOverlappingFalse() {
42          fieldFinal1 = 0; // violation '.* variable 'fieldFinal1' needs "this.".'
43          long fieldFinal2 = 0L;
44          fieldFinal2 = 1L;
45          this.fieldFinal2 = fieldFinal2;
46          fieldFinal3 = new BitSet(); // violation '.* variable 'fieldFinal3' needs "this.".'
47      }
48  
49      public InputRequireThisValidateOnlyOverlappingFalse(String name, long id) {
50          fieldFinal1 = 0; // violation '.* variable 'fieldFinal1' needs "this.".'
51          long field1 = 0L;
52          field1 = field1; // violation 'Reference to instance variable 'field1' needs "this.".'
53          this.fieldFinal2 = 1L;
54          fieldFinal3 = new BitSet(); // violation '.* variable 'fieldFinal3' needs "this.".'
55      }
56  
57      public InputRequireThisValidateOnlyOverlappingFalse(int param) {
58          fieldFinal2 = 0L; // violation '.* variable 'fieldFinal2' needs "this.".'
59          fieldFinal3 = new BitSet(); // violation '.* variable 'fieldFinal3' needs "this.".'
60          long fieldFinal1 = 1L;
61          try {
62              fieldFinal1 = 2L;
63          }
64          catch (Exception ex) {}
65          this.fieldFinal1 = fieldFinal1;
66      }
67  
68      public InputRequireThisValidateOnlyOverlappingFalse(BitSet fieldFinal3) {
69          fieldFinal1 = 1L; // violation 'Reference to instance variable 'fieldFinal1' needs "this.".'
70          fieldFinal2 = 0L; // violation 'Reference to instance variable 'fieldFinal2' needs "this.".'
71          fieldFinal3 = new BitSet();
72          if (true) {
73              fieldFinal3 = (BitSet) fieldFinal3.clone();
74          }
75          this.fieldFinal3 = fieldFinal3;
76      }
77  
78      void foo1(String methodParam) {
79          methodParam = methodParam;
80      }
81  
82      void foo2() {
83          String localVar = "values";
84          localVar = localVar;
85      }
86  
87      void foo3() {
88          String field1 = "values";
89          field1 = field1; // violation 'Reference to instance variable 'field1' needs "this.".'
90      }
91  
92      void foo4(String methodParam) {
93          fieldStatic = methodParam;
94      }
95  
96      void foo5(String methodParam) {
97          methodParam = methodParam + "string";
98      }
99  
100     void foo6(String field1) {
101         field1 = this.field1 + field1;
102         this.field1 = field1 + this.field1;
103         field1 = field1 + this.field1;
104         field1 = this.field1 + this.field1;
105         this.field1 = this.field1 + this.field1;
106         this.field1 = this.field1 + field1;
107         field1 += field1;
108     }
109 
110     String addSuffixToParameter(String methodParam) {
111         return methodParam += "suffix";
112     }
113 
114     String addSuf2F(String field1) {
115         return field1 += "suffix";
116     }
117 
118     String addSuffixToThisField(String field1) {
119         return this.field1 += "suffix";
120     }
121 
122     static void foo7(String fieldStatic) {
123 //        this.fieldStatic = fieldStatic; <- fieldStatic cannot be referenced from a static context
124         fieldStatic = fieldStatic;
125     }
126 
127     void foo8(Long field1) {
128         field1 += field1; // violation 'Reference to instance variable 'field1' needs "this.".'
129     }
130 
131     void foo9(Long fieldFinal1) {
132 //        this.fieldFinal1 += fieldFinal1; <- cannot assign value to a final variable
133         fieldFinal1 += fieldFinal1;
134     }
135 
136     void method1() {
137         field1 = "2"; // violation 'Reference to instance variable 'field1' needs "this.".'
138     }
139 
140     void method2() {
141         method1() ; // violation 'Method call to 'method1' needs "this.".'
142     }
143 
144     void method3() {
145         staticFoo();
146         staticTwoArgs("message", "arg");
147         staticTwoArgs("message", 1);
148         this.method1() ;
149     }
150 
151     static void staticFoo() { }
152 
153     static void foo10() {
154         staticFoo();
155         staticTwoArgs("message", "arg");
156     }
157 
158 //    void staticFoo() {} -> compile-time error. Already defined in the scope.
159 
160     static void staticTwoArgs(String message1, String argument) {}
161 
162     void staticTwoArgs(String message1, int argument) {}
163 
164     static void foo16() {
165         long fieldFinal1 = 5L;
166 // this.fieldFinal1 = fieldFinal1; // compile-time error: cannot be referenced from a static context
167         fieldFinal1 = 11L;
168     }
169 
170     static void foo17() {
171         String fieldStatic = "";
172 // this.fieldStatic = fieldStatic; // compile-time error: cannot be referenced from a static context
173         fieldStatic = "Hello, World!";
174     }
175 
176     InputRequireThisValidateOnlyOverlappingFalse(boolean flag) {
177         fieldFinal1 = 0L; // violation 'Reference to instance variable 'fieldFinal1' needs "this.".'
178         fieldFinal2 = 0L; // violation 'Reference to instance variable 'fieldFinal2' needs "this.".'
179         fieldFinal3 = new BitSet(); // violation '.* variable 'fieldFinal3' needs "this.".'
180         long field1 = 1L;
181         field1 = field1; // violation 'Reference to instance variable 'field1' needs "this.".'
182     }
183 
184     InputRequireThisValidateOnlyOverlappingFalse(boolean flag, String name) {
185         fieldFinal1 = 0L; // violation 'Reference to instance variable 'fieldFinal1' needs "this.".'
186         fieldFinal2 = 0L; // violation 'Reference to instance variable 'fieldFinal2' needs "this.".'
187         fieldFinal3 = new BitSet(); // violation '.* variable 'fieldFinal3' needs "this.".'
188         long field1 = 1L;
189         field1 = field1; // violation 'Reference to instance variable 'field1' needs "this.".'
190         return;
191     }
192 
193     void foo18() {
194         field1 = "Hello"; // violation 'Reference to instance variable 'field1' needs "this.".'
195     }
196 
197     void foo19(String field1) {
198         field1 = "Hello"; // violation 'Reference to instance variable 'field1' needs "this.".'
199     }
200 
201     void foo20() {
202         boolean foo21 = this.foo21("");
203         if (foo21) {
204 
205         }
206     }
207 
208     boolean foo21(String s) {
209         return true;
210     }
211 
212     void foo22() {
213         long fieldFinal1 = 1L;
214 //        this.fieldFinal1 = fieldFinal1; <- cannot assign value to a final variable
215         fieldFinal1 = fieldFinal1;
216     }
217 
218     void foo23() {
219         field1 = "Hello!"; // violation 'Reference to instance variable 'field1' needs "this.".'
220     }
221 
222     void foo24() {
223         String field1 = "Hello";
224         field1 = "Java"; // No violation. Local var allowed
225         this.booleanField = true;
226         this.booleanField = booleanField; // violation '.* variable 'booleanField' needs "this.".'
227     }
228 
229     void foo25() {
230         try {
231             if (true) {
232                 String field1 = "Hello, World!";
233                 if (true) {
234                     field1 = new String(); // No violation. Local var allowed
235                 }
236                 else {
237                     field1 += field1; // violation '.* variable 'field1' needs "this.".'
238                 }
239             }
240         }
241         catch (Exception ex) {
242 
243         }
244     }
245 
246     void foo26(String field1) {
247         field1 = field1.replace('/', '*'); // violation '.* variable 'field1' needs "this.".'
248     }
249 
250     void foo27() {
251         int intField = -1;
252         if (intField == -1) {
253             intField = 20;
254         }
255         else {
256             intField = this.intField / 100;
257         }
258     }
259 
260     void foo28() {
261         boolean booleanField = true;
262         booleanField = !booleanField; // violation '.* variable 'booleanField' needs "this.".'
263     }
264 
265     static void foo29(String field1) {
266 //this.field1 = true ? "field1" : field1; /compile error: cannot be referenced from a static context
267         field1 = true ? "field1" : field1;
268     }
269 
270     void foo30(String field1) {
271         field1 = true ? "field1" : field1; // violation '.* variable 'field1' needs "this.".'
272     }
273 
274     void foo31(String field1) {
275         field1 = this.field1;
276     }
277 
278     String foo32(String field1) {
279         field1 = addSuf2F(field1); // violation 'Method call to 'addSuf2F' needs "this.".'
280         return field1;
281     }
282 
283     String foo33(String field1 ) {
284         field1 = addSuf2F(field1); // 2 violations
285         return "New String";
286     }
287 
288     String foo34(String field1) {
289         field1 = field1.replace('A', 'B');
290         if (field1.contains("C")) {
291             return field1;
292         }
293         else {
294             return field1 + 'C';
295         }
296     }
297 
298     String foo35() {
299         String field1 = "values";
300         field1 = field1;
301         return field1;
302     }
303 
304     void foo36(String field1) {
305         field1 = field1.replace('/', '*');
306         field1 = this.field1;
307     }
308 
309     String foo37(String field1) {
310         field1 += "suffix"; // violation 'Reference to instance variable 'field1' needs "this.".'
311         return "New string";
312     }
313 
314     static void foo38() {
315 //        this.fieldStatic = ""; <-- compile-time error: cannot be referenced from a static context
316         fieldStatic = "";
317     }
318 
319     static void foo39() {
320         boolean booleanField = true;
321 // this.booleanField = !booleanField; compile-time error: cannot be referenced from a static context
322         booleanField = !booleanField;
323     }
324 
325     static void foo40() {
326         try {
327             boolean booleanField = true;
328 //this.booleanField = !booleanField; <-- compile error: cannot be referenced from a static context
329             booleanField = !booleanField;
330         }
331         catch (Exception e) {}
332     }
333 
334     static {
335 //        this.fieldStatic = ""; <-- compile-time error: cannot be referenced from a static context
336         fieldStatic = "";
337     }
338 
339     {
340 //        if we assign variable to a final variable in initialization block,
341 //        it will lead to compile-time error in constructor block: variable migh have been
342 //        already assigned
343 
344 //        fieldFinal1 = 1;
345     }
346 
347     {
348         String field1 = "";
349         field1 = field1; // violation 'Reference to instance variable 'field1' needs "this.".'
350     }
351 
352     static {
353         fieldStatic = "";
354         String field1 = "";
355 //        this.field1 = field1; <-- compile-time error: cannot be referenced from a static context
356         field1 = field1;
357     }
358 
359     void foo41(long fieldFinal1) {
360 //        this.fieldFinal1 = 1L; <- cannot assign value to a final variable
361         fieldFinal1 = fieldFinal1;
362     }
363 
364     void foo42(String fieldStatic) {
365         this.fieldStatic = fieldStatic;
366     }
367 
368     void foo43(String fieldStatic) {
369         fieldStatic = fieldStatic;
370     }
371 
372     void foo44(String fieldStatic) {
373         fieldStatic = this.fieldStatic;
374     }
375 
376     private String action;
377 
378     public String getAction() {
379         return this.action;
380     }
381 
382     public String foo45() {
383         String action = getAction(); // violation 'Method call to 'getAction' needs "this.".'
384         if (true) {
385             return processAction("action"); // violation 'Method .* 'processAction' needs "this.".'
386         }
387         else if (action.endsWith("/")) {
388             if (action.startsWith("/")) {
389                 action = "" + action;
390             }
391         }
392         action = "action"; // No violation. Local var allowed
393         return processAction(action); // violation 'Method call to 'processAction' needs "this.".'
394     }
395 
396     private String processAction(String action) {
397         return "";
398     }
399 
400     public InputRequireThisValidateOnlyOverlappingFalse(long fieldFinal1, long fieldFinal2,
401                                                         BitSet fieldFinal3, boolean booleanField) {
402         this.fieldFinal1 = fieldFinal1;
403         this.fieldFinal2 = fieldFinal2;
404         this.fieldFinal3 = fieldFinal3;
405 
406         booleanField = this.booleanField;
407         if (booleanField) {
408             booleanField = "Hello, World!".equals("Hello, Checkstyle!");
409         }
410 
411         this.booleanField = booleanField;
412     }
413 
414     void foo46(boolean booleanField) {
415         booleanField = this.booleanField;
416         if (booleanField) {
417             booleanField = "Hello, World!".equals("Hello, Checkstyle!");
418         }
419 
420         this.booleanField = booleanField;
421     }
422 
423     static void foo47(String fieldStatic) {
424         fieldStatic = "Andrei";
425     }
426 
427     void foo48(long fieldFinal1) {
428         fieldFinal1 = 1L;
429     }
430 
431     private boolean foo49(boolean booleanField) {
432         boolean suppressionSourceExists = true;
433         try {
434 
435         }
436         catch (Exception ex) {
437             suppressionSourceExists = false;
438         }
439         finally {
440             if (booleanField) {
441                 try {
442                 }
443                 catch (Exception ignored) {
444                     this.booleanField = false;
445                 }
446             }
447         }
448         return suppressionSourceExists;
449     }
450 
451     void foo50(String fieldStatic) {
452         fieldStatic = fieldStatic;
453     }
454 
455     void foo51(String methodParam) {
456         fieldStatic = methodParam;
457     }
458 
459     void foo52(String fieldStatic) {
460         fieldStatic += fieldStatic;
461     }
462 
463     void foo53(String fieldStatic) {
464         fieldStatic += fieldStatic;
465     }
466 
467     void foo54(String methodParam) {
468         fieldStatic += methodParam;
469     }
470 
471     void foo55(String methodParam) {
472         fieldStatic += fieldStatic;
473     }
474 
475     void foo56(boolean booleanField) { booleanField = this.booleanField; }
476 
477     boolean foo57(boolean booleanField) { booleanField = !booleanField;  return booleanField; }
478 }
479 class Issue6264 {
480     private final String hashCode;
481 
482     Issue6264() {
483         this.hashCode = new String();
484     }
485 
486     private void hashCodeMethod(String param) {
487         hashCode();
488     }
489 }
490 class Issue7306 {
491     List<String> add = new ArrayList<>();
492     List<String> singletonList = new ArrayList<>();
493 
494     void someMethod() {
495         List<String> test = new ArrayList<>();
496         test.forEach(this.add::add);
497         test.forEach(test::add);
498         test.forEach(Collections::singletonList);
499         test.forEach(add::add); // violation 'Reference to instance variable 'add' needs "this.".'
500     }
501 }
502 class AnotherOverlappingFalse {
503     private String name;
504 
505     public void bind() {
506         new Object() {
507             @Override
508             public String toString() {
509                 String name = null;
510 
511                 if ( true ) {
512                     name = String.format(Locale.US, name);
513                 }
514 
515                 return name;
516             }
517         };
518     }
519 }