View Javadoc
1   /*
2   RegexpSinglelineJava
3   format = System\\.(out)|(err)\\.print(ln)?\\(
4   message = (default)(null)
5   ignoreCase = (default)false
6   minimum = (default)0
7   maximum = (default)0
8   ignoreComments = (default)false
9   
10  
11  */
12  
13  package com.puppycrawl.tools.checkstyle.checks.regexp.regexpsinglelinejava;
14  
15  import java.awt.*;
16  import java.io.ByteArrayOutputStream;
17  import java.io.File;
18  
19  /**
20   * Test case for detecting simple semantic violations.
21   * @author Lars Kühne
22   **/
23  class InputRegexpSinglelineJavaSemantic
24  {
25      /* Boolean instantiation in a static initializer */
26      static {
27          Boolean x = new Boolean(true);
28      }
29  
30      /* Boolean instantiation in a non-static initializer */
31      {
32          Boolean x = new Boolean(true);
33          Boolean[] y = new Boolean[]{Boolean.TRUE, Boolean.FALSE};
34      }
35  
36      /** fully qualified Boolean instantiation in a method. **/
37      Boolean getBoolean()
38      {
39          return new Boolean(true);
40      }
41  
42      void otherInstantiations()
43      {
44          // instantiation of classes in the same package
45          Object o1 = new InputBraces();
46          Object o2 = new InputModifier();
47          // classes in another package with .* import
48          ByteArrayOutputStream s = new ByteArrayOutputStream();
49          File f = new File("/tmp");
50          // classes in another package with explicit import
51          Dimension dim = new Dimension();
52          Color col = new Color(0, 0, 0);
53      }
54  
55      void exHandlerTest()
56      {
57          try {
58              ; // do stuff and don't handle exceptions in some cases
59          }
60          catch (IllegalStateException emptyCatchIsAlwaysAnError) {
61          }
62          catch (NullPointerException ex) {
63              // can never happen, but only commenting this is currently a problem
64              // Possible future enhancement: allowEmptyCatch="commented"
65          }
66          catch (ArrayIndexOutOfBoundsException ex) {
67              ;
68              // can never happen, semicolon makes checkstyle happy
69              // this is a workaround for above problem
70          }
71          catch (NegativeArraySizeException ex) {
72              {
73              }
74              // can never happen, empty compound statement is another workaround
75          }
76          catch (UnsupportedOperationException handledException) { // violation below
77              System.out.println(handledException.getMessage());
78          }
79          catch (SecurityException ex) { /* hello */ }
80          catch (StringIndexOutOfBoundsException ex) {}
81          catch (IllegalArgumentException ex) { }
82  
83          try {
84          }
85          finally {
86          }
87          try {
88          // something
89          }
90          finally {
91              // something
92          }
93          try {
94              ; // something
95          }
96          finally {
97              ; // statement
98          }
99      }
100 
101     /** test **/
102     private static final long IGNORE = 666l + 666L;
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124     public class EqualsVsHashCode1
125     {
126         public boolean equals(int a) // wrong arg type, don't flag
127         {
128             return a == 1;
129         }
130     }
131 
132     public class EqualsVsHashCode2
133     {
134         public boolean equals(String a) // flag
135         {
136             return true;
137         }
138     }
139 
140     public class EqualsVsHashCode3
141     {
142         public boolean equals(Object a) // don't flag
143         {
144             return true;
145         }
146 
147         public int hashCode()
148         {
149             return 0;
150         }
151     }
152 
153     public class EqualsVsHashCode4
154     {
155         // in anon inner class
156         ByteArrayOutputStream bos1 = new ByteArrayOutputStream()
157         {
158             public boolean equals(Object a) // don't flag
159             {
160                 return true;
161             }
162 
163             public int hashCode()
164             {
165                 return 0;
166             }
167         };
168 
169         ByteArrayOutputStream bos2 = new ByteArrayOutputStream()
170         {
171             public boolean equals(Object a) // flag
172             {
173                 return true;
174             }
175         };
176     }
177 
178     public void triggerEmptyBlockWithoutBlock()
179     {
180         // an if statement without a block to increase test coverage
181         if (true)
182             return;
183     }
184 
185     // empty instance initializer
186     {
187     }
188 
189     public class EqualsVsHashCode5
190     {
191         public <A> boolean equals(int a) // wrong arg type, don't flag even with generics
192         {
193             return a == 1;
194         }
195     }
196 
197     public class EqualsVsHashCode6
198     {
199         public <A> boolean equals(Comparable<A> a) // flag, weven with generics
200         {
201             return true;
202         }
203     }
204 
205     private class InputBraces {
206 
207     }
208 
209     private class InputModifier {
210 
211     }
212 
213     synchronized void foo() {
214         synchronized (this) {} // not OK
215         synchronized (Class.class) { // OK
216             synchronized (new Object()) {
217                 // not OK if checking statements
218             }
219         }
220     }
221 
222 
223     static {
224 
225     int a = 0;}
226 
227     static {
228 
229     }
230 }