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