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