View Javadoc
1   /*
2   HiddenField
3   ignoreFormat = (default)null
4   ignoreConstructorParameter = true
5   setterCanReturnItsClass = (default)false
6   ignoreAbstractMethods = (default)false
7   tokens = (default)VARIABLE_DEF, PARAMETER_DEF, PATTERN_VARIABLE_DEF, LAMBDA, RECORD_COMPONENT_DEF
8   
9   
10  */
11  
12  package com.puppycrawl.tools.checkstyle.checks.coding.hiddenfield;
13  
14  ///////////////////////////////////////////////////////////////////////////////////////////////
15  // Test case file for checkstyle.
16  // Created: 2002
17  ///////////////////////////////////////////////////////////////////////////////////////////////
18  
19  /**
20   * Test case for hidden fields
21   * @author Rick Giles
22   **/
23  class InputHiddenField6
24  {
25      private int hidden = 0;
26  
27      public InputHiddenField6()
28      {
29          int hidden = 0; // violation
30      }
31  
32      public InputHiddenField6(int hidden) //parameter shadows field
33      {
34      }
35  
36      public void shadow()
37      {
38          int hidden = 0; //shadows field // violation
39      }
40  
41      public void shadowFor()
42      {
43          for (int hidden = 0; hidden < 1; hidden++) { //shadows field // violation
44          }
45      }
46  
47      public void shadowParam(int hidden) //parameter shadows field // violation
48      {
49      }
50  
51      public class Inner
52      {
53          private int innerHidden = 0;
54  
55          public Inner()
56          {
57              int innerHidden = 0; //shadows field // violation
58          }
59  
60          public Inner(int innerHidden) //shadows field
61          {
62          }
63  
64          private void innerShadow()
65          {
66              int innerHidden = 0; //shadows inner field // violation
67              int hidden = 0; //shadows outer field // violation
68          }
69  
70          private void innerShadowFor()
71          {
72              for (int innerHidden = 0; innerHidden < 1; innerHidden++) { // violation
73              }
74              //shadows outer field
75              for (int hidden = 0; hidden < 1; hidden++) { // violation
76              }
77          }
78  
79          private void shadowParam(
80              int innerHidden, //parameter shadows inner field // violation
81              int hidden //parameter shadows outer field // violation
82          )
83          {
84          }
85  
86          {
87              int innerHidden = 0;//shadows inner field // violation
88              int hidden = 0; //shadows outer field // violation
89          }
90      }
91  
92      {
93          int hidden = 0;//shadows field // violation
94      }
95  }
96  
97  interface NothingHidden6
98  {
99      public static int notHidden = 0;
100 
101     // not a violation
102     public void noShadow(int notHidden);
103 }
104 
105 /** tests ignoring the parameter of a property setter method */
106 class PropertySetter16
107 {
108     private int prop;
109 
110     /** setter */
111     public void setProp(int prop) // violation
112     {
113         this.prop = prop;
114     }
115 
116     /** violation - incorrect method name */
117     public void setprop(int prop) // violation
118     {
119         this.prop = prop;
120     }
121 
122     /** violation - more than one parameter */
123     public void setProp(int prop, int extra) // violation
124     {
125         this.prop = prop;
126     }
127 }
128 
129 /** tests a non-void method */
130 class PropertySetter26
131 {
132     private int prop;
133 
134     /** violation - not a void method */
135     public int setProp(int prop) // violation
136     {
137         this.prop = prop;
138         return 0;
139     }
140 }
141 
142 /** tests for static fields */
143 class StaticFields6
144 {
145     private static int hidden;
146 
147     public static void staticMethod()
148     {
149         int hidden; // violation
150     }
151 
152     public void method()
153     {
154         int hidden; // violation
155     }
156 
157     static
158     {
159         int hidden; // violation
160     }
161 
162     {
163         int hidden; // violation
164     }
165 }
166 
167 /** tests static methods & initializers */
168 class StaticMethods6
169 {
170     private int notHidden;
171 
172     public static void method()
173     {
174         // local variables of static methods don't hide instance fields.
175         int notHidden;
176     }
177 
178     static
179     {
180         // local variables of static initializers don't hide instance fields.
181         int notHidden;
182     }
183 
184     private int x;
185     private static int y;
186     static class Inner {
187         void useX(int x) {
188             x++;
189         }
190         void useY(int y) { // violation
191             y++;
192         }
193     }
194 }
195 
196 enum HiddenEnum16
197 {
198     A(129),
199     B(283),
200     C(1212)
201     {
202         /**
203          * Should not be flagged as violation as we don't check
204          * hidden class level fields
205          */
206         int hidden;
207 
208         public void doSomething()
209         {
210             //Should be flagged as hiding enum constant member
211             int hidden = 0; // violation
212         }
213     };
214 
215     int hidden;
216     static int hiddenStatic;
217 
218     /**
219      * ctor parameter hides member
220      */
221     HiddenEnum16(int hidden)
222     {
223     }
224 
225     public void doSomething()
226     {
227         //Should be flagged as hiding static member
228         int hidden = 0; // violation
229     }
230 
231     public static void doSomethingStatic()
232     {
233         //Should be flagged as hiding static member
234         int hiddenStatic = 0; // violation
235     }
236 }
237 
238 // we should ignore this if user wants (ignoreAbstractMethods is true)
239 abstract class InputHiddenFieldBug10845126 {
240     String x;
241     public abstract void methodA(String x); // violation
242 }
243 
244 class Bug33709466 {
245     private int xAxis;
246 
247     public void setxAxis(int xAxis) { // violation
248         this.xAxis = xAxis;
249     }
250 }
251 
252 /** tests chain-setter */
253 class PropertySetter36
254 {
255     private int prop;
256 
257     /**
258      * if setterCanReturnItsClass == false then
259      *     violation - not a void method
260      *
261      * if setterCanReturnItsClass == true then
262      *     success as it is then considered to be a setter
263      */
264     public PropertySetter36 setProp(int prop) // violation
265     {
266         this.prop = prop;
267         return this;
268     }
269 }
270 
271 /** tests setters (both regular and the chain one) on the enum */
272 enum PropertySetter46 {
273     INSTANCE;
274 
275     private int prop;
276     private int prop2;
277 
278     public void setProp(int prop) { // violation
279         this.prop = prop;
280     }
281 
282     /**
283      * if setterCanReturnItsClass == false then
284      *     violation - not a void method
285      *
286      * if setterCanReturnItsClass == true then
287      *     success as it is then considered to be a setter
288      */
289     public PropertySetter46 setProp2(int prop2) // violation
290     {
291         this.prop2 = prop2;
292         return this;
293     }
294 }
295 
296 /** Tests setter for one letter field (issue #730). */
297 class OneLetterField6
298 {
299     int i;
300 
301     void setI(int i) // violation
302     {
303         this.i = i;
304     }
305     enum Inner {}
306 }
307 
308 class DuplicateFieldFromPreviousClass6
309 {
310     public void method() {
311         int i = 0;
312     }
313 }
314 
315 class NestedEnum6 {
316     enum Test { A, B, C; int i; }
317 
318     void method(int i) {}
319 }