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