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