View Javadoc
1   /*
2   FinalLocalVariable
3   validateEnhancedForLoopVariable = true
4   tokens = VARIABLE_DEF, PARAMETER_DEF
5   
6   
7   */
8   
9   package com.puppycrawl.tools.checkstyle.checks.coding.finallocalvariable;
10  
11  public class InputFinalLocalVariableEnhancedForLoopVariable {
12      public void method1()
13      {
14          final java.util.List<Object> list = new java.util.ArrayList<>();
15  
16          for(Object a : list){ // violation, "Variable 'a' should be declared final"
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          // violation below "Variable 'snippet' should be declared final"
33          for (String snippet : snippets) {
34              filteredSnippets.add(snippet);
35          }
36          if (filteredSnippets.size() == 0) {
37              String snippet = snippets.get(0);
38              snippet = new String(snippet);
39              filteredSnippets.add(snippet);
40          }
41          return filteredSnippets;
42      }
43  
44      public void method4()
45      {
46          final java.util.List<Object> list = new java.util.ArrayList<>();
47  
48          for(Object a : list) { // violation, "Variable 'a' should be declared final"
49          }
50  
51          Object a; // violation, "Variable 'a' should be declared final"
52          if (list.isEmpty())
53          {
54              a = new String("empty");
55          }
56          else
57          {
58              a = new String("not empty");
59          }
60  
61          for(Object b : list) {
62              b = new String("b");
63          }
64      }
65  
66  }