KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > InputSemantic


1 ////////////////////////////////////////////////////////////////////////////////
2
// Test case file for checkstyle.
3
// Created: 2001
4
////////////////////////////////////////////////////////////////////////////////
5
package com.puppycrawl.tools.checkstyle;
6
7 import java.io.*; // star import for instantiation tests
8
import java.awt.Dimension JavaDoc; // explicit import for instantiation tests
9
import java.awt.Color JavaDoc;
10
11 /**
12  * Test case for detecting simple semantic errors.
13  * @author Lars Kühne
14  **/

15 class InputSemantic
16 {
17     /* Boolean instantiation in a static initializer */
18     static {
19         Boolean JavaDoc x = new Boolean JavaDoc(true);
20     }
21
22     /* Boolean instantiation in a non-static initializer */
23     {
24         Boolean JavaDoc x = new Boolean JavaDoc(true);
25         Boolean JavaDoc[] y = new Boolean JavaDoc[]{Boolean.TRUE, Boolean.FALSE};
26     }
27
28     /** fully qualified Boolean instantiation in a method. **/
29     Boolean JavaDoc getBoolean()
30     {
31         return new java.lang.Boolean JavaDoc(true);
32     }
33
34     void otherInstantiations()
35     {
36         // instantiation of classes in the same package
37
Object JavaDoc o1 = new InputBraces();
38         Object JavaDoc 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 JavaDoc dim = new Dimension JavaDoc();
44         Color JavaDoc col = new Color JavaDoc(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 JavaDoc emptyCatchIsAlwaysAnError) {
53         }
54         catch (NullPointerException JavaDoc ex) {
55             // can never happen, but only commentig this is currently an error
56
// Possible future enhancement: allowEmptyCatch="commented"
57
}
58         catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
59             ;
60             // can never happen, semicolon makes checkstyle happy
61
// this is a workaround for above problem
62
}
63         catch (NegativeArraySizeException JavaDoc ex) {
64             {
65             }
66             // can never happen, empty compound statement is another workaround
67
}
68         catch (UnsupportedOperationException JavaDoc handledException) {
69             System.out.println(handledException.getMessage());
70         }
71         catch (SecurityException JavaDoc ex) { /* hello */ }
72         catch (StringIndexOutOfBoundsException JavaDoc ex) {}
73         catch (IllegalArgumentException JavaDoc 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 JavaDoc a) // flag
127
{
128             return true;
129         }
130     }
131
132     public class EqualsVsHashCode3
133     {
134         public boolean equals(Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc<A> a) // flag, weven with generics
192
{
193             return true;
194         }
195     }
196
197 }
198
Popular Tags