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