KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > InputWhitespace


1 ////////////////////////////////////////////////////////////////////////////////
2
// Test case file for checkstyle.
3
// Created: 2001
4
////////////////////////////////////////////////////////////////////////////////
5
package com . puppycrawl
6     .tools.
7     checkstyle;
8
9 /**
10  * Class for testing whitespace issues.
11  * error missing author tag
12  **/

13 class InputWhitespace
14 {
15     /** ignore assignment **/
16     private int mVar1=1;
17     /** ignore assignment **/
18     private int mVar2 =1;
19     /** Should be ok **/
20     private int mVar3 = 1;
21
22     /** method **/
23     void method1()
24     {
25         final int a = 1;
26         int b= 1; // Ignore 1
27
b=1; // Ignore 1
28
b+=1; // Ignore 1
29
b -=- 1 + (+ b); // Ignore 2
30
b = b ++ + b --; // Ignore 1
31
b = ++ b - -- b; // Ignore 1
32
}
33
34     /** method **/
35     void method2()
36     {
37         synchronized(this) {
38         }
39         try{
40         }
41         catch(RuntimeException JavaDoc e){
42         }
43     }
44
45     /**
46        skip blank lines between comment and code,
47        should be ok
48     **/

49
50
51     private int mVar4 = 1;
52
53
54     /** test WS after void return */
55     private void fastExit()
56     {
57         boolean complicatedStuffNeeded = true;
58         if( !complicatedStuffNeeded )
59         {
60             return; // should not complain about missing WS after return
61
}
62         else
63         {
64             // do complicated stuff
65
}
66     }
67
68
69     /** test WS after non void return
70      @return 2
71     */

72     private int nonVoid()
73     {
74         if ( true )
75         {
76             return(2); // should complain about missing WS after return
77
}
78         else
79         {
80             return 2; // this is ok
81
}
82     }
83
84     /** test casts **/
85     private void testCasts()
86     {
87         Object JavaDoc o = (Object JavaDoc) new Object JavaDoc(); // ok
88
o = (Object JavaDoc)o; // error
89
o = ( Object JavaDoc ) o; // ok
90
o = (Object JavaDoc)
91             o; // ok
92
}
93
94     /** test questions **/
95     private void testQuestions()
96     {
97         boolean b = (1 == 2)?true:false;
98         b = (1==2) ? false : true;
99     }
100
101     /** star test **/
102     private void starTest()
103     {
104         int x = 2 *3* 4;
105     }
106
107     /** boolean test **/
108     private void boolTest()
109     {
110         boolean a = true;
111         boolean x = ! a;
112         int z = ~1 + ~ 2;
113     }
114
115     /** division test **/
116     private void divTest()
117     {
118         int a = 4 % 2;
119         int b = 4% 2;
120         int c = 4 %2;
121         int d = 4%2;
122         int e = 4 / 2;
123         int f = 4/ 2;
124         int g = 4 /2;
125         int h = 4/2;
126     }
127
128     /** @return dot test **/
129     private java .lang. String JavaDoc dotTest()
130     {
131         Object JavaDoc o = new java.lang.Object JavaDoc();
132         o.
133             toString();
134         o
135             .toString();
136         o . toString();
137         return o.toString();
138     }
139
140     /** assert statement test */
141     public void assertTest()
142     {
143         // OK
144
assert true;
145
146         // OK
147
assert true : "Whups";
148
149         // evil colons, should be OK
150
assert "OK".equals(null) ? false : true : "Whups";
151
152         // missing WS around assert
153
assert(true);
154
155         // missing WS around colon
156
assert true:"Whups";
157     }
158
159     /** another check */
160     void donBradman(Runnable JavaDoc aRun)
161     {
162         donBradman(new Runnable JavaDoc() {
163             public void run() {
164             }
165         });
166
167         final Runnable JavaDoc r = new Runnable JavaDoc() {
168             public void run() {
169             }
170         };
171     }
172
173     /** rfe 521323, detect whitespace before ';' */
174     void rfe521323()
175     {
176         doStuff() ;
177         // ^ whitespace
178
for (int i = 0 ; i < 5; i++) {
179             // ^ whitespace
180
}
181     }
182
183     
184     /** bug 806243 (NoWhitespaceBeforeCheck error for anonymous inner class) */
185     private int i ;
186     // ^ whitespace
187
private int i1, i2, i3 ;
188     // ^ whitespace
189
private int i4, i5, i6;
190
191     /** bug 806243 (NoWhitespaceBeforeCheck error for anonymous inner class) */
192     void bug806243()
193     {
194         Object JavaDoc o = new InputWhitespace() {
195             private int j ;
196             // ^ whitespace
197
};
198     }
199
200     void doStuff() {
201     }
202 }
203
204 /**
205  * Bug 806242 (NoWhitespaceBeforeCheck error with an interface).
206  * @author o_sukhodolsky
207  * @version 1.0
208  */

209 interface IFoo
210 {
211     void foo() ;
212     // ^ whitespace
213
}
214
215 /**
216  * Avoid Whitespace errors in for loop.
217  * @author lkuehne
218  * @version 1.0
219  */

220 class SpecialCasesInForLoop
221 {
222     void forIterator()
223     {
224         // avoid conflict between WhiteSpaceAfter ';' and ParenPad(nospace)
225
for (int i = 0; i++ < 5;) {
226         // ^ no whitespace
227
}
228
229         // bug 895072
230
// avoid confilct between ParenPad(space) and NoWhiteSpace before ';'
231
int i = 0;
232     for ( ; i < 5; i++ ) {
233     // ^ whitespace
234
}
235         for (int anInt : getSomeInts()) {
236             //Should be ignored
237
}
238     }
239
240     int[] getSomeInts() {
241         int i = (int) ( 2 / 3 );
242         return null;
243     }
244 }
245
Popular Tags