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