View Javadoc
1   /*
2   LocalFinalVariableName
3   format = (default)^[a-z][a-zA-Z0-9]*$
4   tokens = (default)VARIABLE_DEF, PARAMETER_DEF, RESOURCE
5   
6   
7   */
8   
9   package com.puppycrawl.tools.checkstyle.checks.naming.localfinalvariablename;
10  import java.io.*;
11  /**
12   * Contains simple mistakes:
13   * - Long lines
14   * - Tabs
15   * - Format of variables and parameters
16   * - Order of modifiers
17   * @author Oliver Burn
18   **/
19  final class InputLocalFinalVariableName
20  {
21      // Long line ----------------------------------------------------------------
22      // Contains a tab ->        <-
23      // Contains trailing whitespace ->
24  
25      // Name format tests
26      //
27      /** Invalid format **/
28      public static final int badConstant = 2;
29      /** Valid format **/
30      public static final int MAX_ROWS = 2;
31  
32      /** Invalid format **/
33      private static int badStatic = 2;
34      /** Valid format **/
35      private static int sNumCreated = 0;
36  
37      /** Invalid format **/
38      private int badMember = 2;
39      /** Valid format **/
40      private int mNumCreated1 = 0;
41      /** Valid format **/
42      protected int mNumCreated2 = 0;
43  
44      /** commas are wrong **/
45      private int[] mInts = new int[] {1,2, 3,
46                                       4};
47  
48      //
49      // Accessor tests
50      //
51      /** should be private **/
52      public static int sTest1;
53      /** should be private **/
54      protected static int sTest3;
55      /** should be private **/
56      static int sTest2;
57  
58      /** should be private **/
59      int mTest1;
60      /** should be private **/
61      public int mTest2;
62  
63      //
64      // Parameter name format tests
65      //
66  
67      /**
68       * @return hack
69       * @param badFormat1 bad format
70       * @param badFormat2 bad format
71       * @param badFormat3 bad format
72       * @throws java.lang.Exception abc
73       **/
74      int test1(int badFormat1,int badFormat2,
75                final int badFormat3)
76          throws java.lang.Exception
77      {
78          return 0;
79      }
80  
81      /** method that is 20 lines long **/
82      private void longMethod()
83      {
84          // a line
85          // a line
86          // a line
87          // a line
88          // a line
89          // a line
90          // a line
91          // a line
92          // a line
93          // a line
94          // a line
95          // a line
96          // a line
97          // a line
98          // a line
99          // a line
100         // a line
101         // a line
102     }
103 
104     /** constructor that is 10 lines long **/
105     private InputLocalFinalVariableName()
106     {
107         // a line
108         // a line
109         // a line
110         // a line
111         // a line
112         // a line
113         // a line
114         // a line
115     }
116 
117     /** test local variables */
118     private void localVariables()
119     {
120         // normal decl
121         int abc = 0;
122         int ABC = 0;
123 
124         // final decls
125         final int cde = 0;
126         final int CDE = 0; // violation
127 
128         // decl in for loop init statement
129         for (int k = 0; k < 1; k++)
130         {
131             String innerBlockVariable = "";
132         }
133         for (int I = 0; I < 1; I++)
134         {
135             String InnerBlockVariable = "";
136         }
137     }
138 
139     /** test method pattern */
140     void ALL_UPPERCASE_METHOD()
141     {
142     }
143 
144     /** test illegal constant **/
145     private static final int BAD__NAME = 3;
146 
147     // A very, very long line that is OK because it matches the regexp "^.*is OK.*regexp.*$"
148     // long line that has a tab ->        <- and would be OK if tab counted as 1 char
149     // tabs that count as one char because of their position ->        <-   ->        <-, OK
150 
151     /** some lines to test the violation column after tabs */
152     void errorColumnAfterTabs()
153     {
154         // with tab-width 8 all statements below start at the same column,
155         // with different combinations of ' ' and '\t' before the statement
156                 int tab0 =1;
157                 int tab1 =1;
158                  int tab2 =1;
159                 int tab3 =1;
160                     int tab4 =1;
161                   int tab5 =1;
162     }
163 
164     // MEMME:
165     /* MEMME: a
166      * MEMME:
167      * OOOO
168      */
169     /* NOTHING */
170     /* YES */ /* MEMME: x */ /* YES!! */
171 
172     /** test long comments **/
173     void veryLong()
174     {
175         /*
176           blah blah blah blah
177           blah blah blah blah
178           blah blah blah blah
179           blah blah blah blah
180           blah blah blah blah
181           blah blah blah blah
182           blah blah blah blah
183           blah blah blah blah
184           blah blah blah blah
185           blah blah blah blah
186           blah blah blah blah
187           blah blah blah blah
188           blah blah blah blah
189           blah blah blah blah
190           blah blah blah blah
191           enough talk */
192     }
193 
194     /**
195      * @see to lazy to document all args. Testing excessive # args
196      **/
197     void toManyArgs(int aArg1, int aArg2, int aArg3, int aArg4, int aArg5,
198                     int aArg6, int aArg7, int aArg8, int aArg9)
199     {
200     }
201 }
202 
203 /** Test class for variable naming in for each clause. */
204 class InputLocalFinalVariableName2
205 {
206     /** Some more Javadoc. */
207     public void doSomething()
208     {
209         //"O" should be named "o"
210         for (Object O : new java.util.ArrayList())
211         {
212 
213         }
214     }
215 }
216 
217 /** Test enum for member naming check */
218 enum InputLocalFinalVariableNameEnum1
219 {
220     /** ABC constant */
221     ABC,
222 
223     /** XYZ constant */
224     XYZ;
225 
226     /** Should be mSomeMember */
227     private int someMember;
228 }