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