View Javadoc
1   /*
2   ParenPad
3   option = (default)NOSPACE
4   tokens = (default)ANNOTATION, ANNOTATION_FIELD_DEF, CTOR_CALL, CTOR_DEF, DOT, \
5            ENUM_CONSTANT_DEF, EXPR, LITERAL_CATCH, LITERAL_DO, LITERAL_FOR, LITERAL_IF, \
6            LITERAL_NEW, LITERAL_SWITCH, LITERAL_SYNCHRONIZED, LITERAL_WHILE, METHOD_CALL, \
7            METHOD_DEF, QUESTION, RESOURCE_SPECIFICATION, SUPER_CTOR_CALL, LAMBDA, RECORD_DEF
8   
9   
10  */
11  
12  package com.puppycrawl.tools.checkstyle.checks.whitespace.parenpad;
13  
14  import java.io.IOException;
15  import java.io.StringWriter;
16  import java.io.Writer;
17  
18  public class InputParenPadLeftRightAndNoSpace1
19  {
20      class ParenPadNoSpace  {
21          ParenPadNoSpace() {
22              this(0);
23          }
24  
25          ParenPadNoSpace(int i) {
26              super();
27          }
28  
29          @SuppressWarnings("")
30          void method(boolean status) {
31              try (Writer writer = new StringWriter()) {
32                  do {
33                      writer.append("a");
34                  } while (status);
35              } catch (IOException e) {
36                  while (status) {
37                      for (int i = 0; i < (long) (2 * (4 / 2)); i++) {
38                          if (i > 2) {
39                              synchronized (this) {
40                                  switch (i) {
41                                      case 3:
42                                      case (4):
43                                      case 5:
44                                          break;
45                                  }
46                              }
47                          }
48                      }
49                  }
50              }
51          }
52      }
53  
54      class ParenPadSpaceLeft {
55          ParenPadSpaceLeft( ) { // 2 violations
56              this( 0); // violation
57          }
58  
59          ParenPadSpaceLeft( int i) { // violation
60              super( ); // 2 violations
61          }
62  
63          @SuppressWarnings( "") // violation
64          void method( boolean status) { // violation
65              try ( Writer writer = new StringWriter( )) { // 3 violations
66                  do {
67                      writer.append("a");
68                  } while ( status); // violation
69              } catch ( IOException e) { // violation
70                  while ( status) { // violation
71                      for ( int i = 0; i < ( long) ( 2 * ( 4 / 2)); i++) { // 3 violations
72                          if ( i > 2) { // violation
73                              synchronized ( this) { // violation
74                                  switch ( i) { // violation
75                                      case 3:
76                                      case ( 4): // violation
77                                      case 5:
78                                          break;
79                                  }
80                              }
81                          }
82                      }
83                  }
84              }
85          }
86      }
87  
88      class ParenPadSpaceRight {
89          ParenPadSpaceRight( ) { // 2 violations
90              this(0 ); // violation
91          }
92  
93          ParenPadSpaceRight(int i ) { // violation
94              super( ); // 2 violations
95          }
96  
97          @SuppressWarnings("" ) // violation
98          void method(boolean status ) { // violation
99              try (Writer writer = new StringWriter( ) ) { // 3 violations
100                 do {
101                     writer.append("a" ); // violation
102                 } while (status ); // violation
103             } catch (IOException e ) { // violation
104                 while (status ) { // violation
105                     for (int i = 0; i < (long ) (2 * (4 / 2 ) ); i++ ) { // 3 violations
106                         if (i > 2 ) { // violation
107                             synchronized (this ) { // violation
108                                 switch (i ) { // violation
109                                     case 3:
110                                     case (4 ): // violation
111                                     case 5:
112                                         break;
113                                 }
114                             }
115                         }
116                     }
117                 }
118             }
119         }
120     }
121 
122     String foo() {
123         return ( (Object // violation
124                 ) bar( ( 1 > 2 ) ? // 3 violations
125                         ( ( 3 < 4 )? false : true ) : // 4 violations
126                         ( ( 1 == 1 ) ? false : true) ) ).toString(); // 5 violations
127     }
128     @MyAnnotation1
129     public boolean bar(boolean a) {
130         assert ( true ); // 2 violations
131         return true;
132     }
133 
134     boolean fooo = this.bar(( true && false ) && true); // 2 violations
135 }
136 @interface MyAnnotation1 {
137     String someField( ) default "Hello world"; // 2 violations
138 }
139 
140 enum MyEnum1 {
141     SOME_CONSTANT( ) { // 2 violations
142         int i = (int) (2 * (4 / 2)
143                 );
144     };
145 
146     public void myMethod() {
147         String s = "test";
148         Object o = s;
149         ((String)o).length();
150         ( (String)o ).length(); // 2 violations
151     }
152 
153     public void crisRon() {
154         Object leo = "messi";
155         Object ibra = leo;
156         ((String)leo).compareTo( (String)ibra ); // 2 violations
157         Math.random();
158     }
159 
160     public void intStringConv() {
161         Object a = 5;
162         Object b = "string";
163         int w = Integer.parseInt((String)a);
164         int x = Integer.parseInt( (String)a); // violation
165         double y = Double.parseDouble((String)a ); // violation
166         float z = Float.parseFloat( (String)a ); // 2 violations
167         String d = ((String)b);
168     }
169 
170     public int something( Object o ) { // 2 violations
171         if ( o == null || !( o instanceof Float ) ) { // 4 violations
172             return -1;
173         }
174         return Integer.valueOf( 22 ).compareTo( (Integer) o ); // 4 violations
175     }
176 
177     private void launch(Integer number ) { // violation
178         String myInt = ( number.toString() + '\0' ); // 2 violations
179         boolean result = false;
180         if (number == 123)
181             result = true;
182     }
183 
184     private static String getterName( Exception t) { // violation
185         if (t instanceof ClassNotFoundException ) { // violation
186             return ( (ClassNotFoundException) t ).getMessage(); // 2 violations
187         }
188         else {
189             return "?";
190         }
191     }
192 
193     private Object exam;
194 
195     public String testing() {
196         return ( this.exam != null ) // 2 violations
197                 ? ( ( Enum )this.exam ).name() // 2 violations
198                 : null;
199     }
200 
201     Object stringReturnValue( Object result ) { // 2 violations
202         if ( result instanceof String ) { // 2 violations
203             result = ( (String) result ).length(); // 2 violations
204         }
205         return result;
206     }
207 
208 
209 
210     private void except() { // 2 violations below
211         java.util.ArrayList<Integer> arrlist = new java.util.ArrayList<Integer>( 5 );
212         arrlist.add( 20); // violation
213         arrlist.add(15 ); // violation
214         arrlist.add( 30 ); // 2 violations
215         arrlist.add(45);
216         try {
217             ( arrlist ).remove( 2); // 3 violations
218         } catch ( IndexOutOfBoundsException x ) { // 2 violations
219             x.getMessage();
220         }
221         org.junit.Assert.assertThat( "123", org.hamcrest.CoreMatchers.is( "123" ) ); // 4 violations
222         org.junit.Assert.assertThat( "Help! Integers don't work", // violation
223                 0, org.hamcrest.CoreMatchers.is( 1 ) ); // 3 violations
224     }
225 }
226 enum MyEnum21 {
227     SOME_CONSTANT( ) { // 2 violations
228         int i = (int) (2 * (4 / 2
229 )                   ); // violation
230     };
231 }