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