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