View Javadoc
1   /*
2   FinalLocalVariable
3   validateEnhancedForLoopVariable = (default)false
4   tokens = VARIABLE_DEF, PARAMETER_DEF
5   
6   
7   */
8   
9   package com.puppycrawl.tools.checkstyle.checks.coding.finallocalvariable;
10  
11  public class InputFinalLocalVariableEnhancedForLoopVariable2 {
12      public void method1()
13      {
14          final java.util.List<Object> list = new java.util.ArrayList<>();
15  
16          for(Object a : list){
17          }
18      }
19  
20      public void method2()
21      {
22          final int[] squares = {0, 1, 4, 9, 16, 25};
23          int x; // violation, "Variable 'x' should be declared final"
24          for (final int i : squares) {
25          }
26  
27      }
28      // violation below "Variable 'snippets' should be declared final"
29      public java.util.List<String> method3(java.util.List<String> snippets) {
30          // violation below "Variable 'filteredSnippets' should be declared final"
31          java.util.List<String> filteredSnippets = new java.util.ArrayList<>();
32          for (String snippet : snippets) {
33              filteredSnippets.add(snippet);
34          }
35          if (filteredSnippets.size() == 0) {
36              String snippet = snippets.get(0);
37              snippet = new String(snippet);
38              filteredSnippets.add(snippet);
39          }
40          return filteredSnippets;
41      }
42  
43      public void method4()
44      {
45          final java.util.List<Object> list = new java.util.ArrayList<>();
46  
47          for(Object a : list) {
48          }
49  
50          Object a; // violation, "Variable 'a' should be declared final"
51          if (list.isEmpty())
52          {
53              a = new String("empty");
54          }
55          else
56          {
57              a = new String("not empty");
58          }
59  
60          for(Object b : list) {
61              b = new String("b");
62          }
63      }
64  
65  }