View Javadoc
1   package com.google.checkstyle.test.chapter4formatting.rule462horizontalwhitespace;
2   
3   /**
4    * Class for testing whitespace issues.
5    * violation missing author tag
6    **/
7   class InputWhitespaceAroundBasic
8   {
9       /** warn **/
10      private int mVar1= 1; // warn
11      /** warn **/
12      private int mVar2 =1; // warn
13      /** Should be ok **/
14      private int mVar3 = 1;
15  
16      /** method **/
17      void method1()
18      {
19          final int a = 1;
20          int b= 1; // warn
21          b= 1; // warn
22          b +=1; // warn
23          b -=- 1 + (+ b); // warn
24          b = b ++ + b --; // ok
25          b = ++ b - -- b; // ok
26      }
27  
28      /** method **/
29      void method2()
30      {
31          synchronized(this) { //warn
32          }
33          try {//warn
34          }
35          catch (RuntimeException e) {//warn
36          }
37      }
38  
39      /**
40         skip blank lines between comment and code,
41         should be ok
42      **/
43  
44  
45      private int mVar4 = 1;
46  
47  
48      /** test WS after void return */
49      private void fastExit()
50      {
51          boolean complicatedStuffNeeded = true;
52          if( !complicatedStuffNeeded) //warn
53          {
54              return; // should not complain about missing WS after return
55          }
56          else
57          {
58              // do complicated stuff
59          }
60      }
61  
62  
63      /** test WS after non void return
64       @return 2
65      */
66      private int nonVoid()
67      {
68          if ( true )
69          {
70              return(2); // //warn
71          }
72          else
73          {
74              return 2; // this is ok
75          }
76      }
77  
78      /** test casts **/
79      private void testCasts()
80      {
81          Object o = (Object) new Object(); // ok
82          o = (Object)o; // ok
83          o = ( Object ) o; // ok
84          o = (Object)
85              o; // ok
86      }
87  
88      /** test questions **/
89      private void testQuestions()
90      {
91  
92          boolean b = (1 ==2) ? false : true; //warn
93      }
94  
95      /** star test **/
96      private void starTest()
97      {
98          int x = 2 * 3* 4; //warn
99      }
100 
101     /** boolean test **/
102     private void boolTest()
103     {
104         boolean a = true;
105         boolean x = ! a;
106         int z = ~1 + ~ 2;
107     }
108 
109     /** division test **/
110     private void divTest()
111     {
112         int a = 4 % 2;
113         int b = 4% 2;//warn
114         int c = 4 %2;//warn
115         int d = 4% 2;//warn
116         int e = 4 / 2;
117         int f = 4/ 2;//warn
118         int g = 4 /2;//warn
119 
120     }
121 
122     /** @return dot test **/
123     private java .lang.  String dotTest()
124     {
125         Object o = new java.lang.Object();
126         o.
127             toString();
128         o
129             .toString();
130         o . toString();
131         return o.toString();
132     }
133 
134     /** assert statement test */
135     public void assertTest()
136     {
137         // OK
138         assert true;
139 
140         // OK
141         assert true : "Whups";
142 
143         // evil colons, should be OK
144         assert "OK".equals(null) ? false : true : "Whups";
145 
146         // missing WS around assert
147         assert(true);//warn
148 
149         // missing WS around colon
150         assert true: "Whups";//warn
151     }
152 
153     /** another check */
154     void donBradman(Runnable aRun)
155     {
156         donBradman(new Runnable() {
157             public void run() {
158             }
159         });
160 
161         final Runnable r = new Runnable() {
162             public void run() {
163             }
164         };
165     }
166 
167     /** rfe 521323, detect whitespace before ';' */
168     void rfe521323()
169     {
170         doStuff() ;
171         //       ^ whitespace
172         for (int i = 0 ; i < 5; i++) {
173             //        ^ whitespace
174         }
175     }
176 
177 
178     /** bug  806243 (NoWhitespaceBeforeCheck violation for anonymous inner class) */
179     private int i ;
180     //           ^ whitespace
181     private int i1, i2, i3 ;
182     //                    ^ whitespace
183     private int i4, i5, i6;
184 
185     /** bug  806243 (NoWhitespaceBeforeCheck violation for anonymous inner class) */
186     void bug806243()
187     {
188         Object o = new InputWhitespaceAroundBasic() {
189             private int j ;
190             //           ^ whitespace
191         };
192     }
193 
194     void doStuff() {
195     }
196 }
197 
198 /**
199  * Bug 806242 (NoWhitespaceBeforeCheck violation with an interface).
200  * @author o_sukhodolsky
201  * @version 1.0
202  */
203 interface IFoo
204 {
205     void foo() ;
206     //        ^ whitespace
207 }
208 
209 /**
210  * Avoid Whitespace violations in for loop.
211  * @author lkuehne
212  * @version 1.0
213  */
214 class SpecialCasesInForLoop
215 {
216     void forIterator()
217     {
218         // avoid conflict between WhiteSpaceAfter ';' and ParenPad(nospace)
219         for (int i = 0; i++ < 5;) {
220         //                  ^ no whitespace
221     }
222 
223         // bug 895072
224     // avoid conflict between ParenPad(space) and NoWhiteSpace before ';'
225     int i = 0;
226     for ( ; i < 5; i++ ) {
227     //   ^ whitespace
228     }
229         for (int anInt : getSomeInts()) {
230             //Should be ignored
231         }
232     }
233 
234     int[] getSomeInts() {
235         int i = (int) ( 2 / 3 );
236         return null;
237     }
238 
239     void forColon() {
240         int ll[] = new int[10];
241         for (int x:ll) {} // warn
242         for (int x :ll) {} // warn
243         for (int x: ll) {} // warn
244         for (int x : ll) {} // ok
245     }
246 }
247 
248 /**
249  * Operators mentioned in Google Coding Standards 2016-07-12
250  */
251 class NewGoogleOperators
252 {
253     NewGoogleOperators()
254     {
255        Runnable l;
256 
257        l = ()-> { }; //warn
258        l = () ->{ }; //warn
259        l = () -> { }; //ok
260        l = () -> {}; //ok
261 
262        java.util.Arrays.sort(null, String :: compareToIgnoreCase);
263        java.util.Arrays.sort(null, String::compareToIgnoreCase);
264 
265        new Object().toString();
266        new Object() . toString();
267     }
268 }