KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > GenericsTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.management;
24
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.HashSet JavaDoc;
29
30
31 import java.io.Serializable JavaDoc;
32
33
34 import com.sun.appserv.management.util.misc.TypeCast;
35
36 /**
37     Demo of generic do/don't issues.
38  */

39 public final class GenericsTest extends junit.framework.TestCase
40 {
41         public
42     GenericsTest()
43     {
44     }
45     
46     private static class A implements Serializable JavaDoc
47     {
48         public static final long serialVersionUID = 999;
49         
50         public A() {}
51     }
52     
53     private static final class AA extends A
54     {
55         public static final long serialVersionUID = 9999;
56         public AA() {}
57     }
58     
59     /**
60     commented out to avoid compiler warnings; code compiles and tests good,
61     and is useful in understanding generics.
62     
63     // An example of an unchecked cast where the caller had better
64     // use the correct type of Set<T>; see testCallersProblem().
65      
66         private <T extends Serializable> Set<T>
67     getSetOfSerializableT()
68     {
69         final Set<Serializable> result = new HashSet<Serializable>();
70         result.add( new String("hello") );
71         result.add( new Integer(0) );
72         result.add( new Boolean(false) );
73         
74         // "warning: [unchecked] unchecked cast: found Set<String>, required Set<T>"
75         // it's up to the *caller* to ensure that T is a valid type as per javadoc
76         // or other semantics
77         return (Set<T>)result;
78     }
79     
80     
81      // Please see how getSetOfT_ImplCast() is implemented with a cast.
82         public void
83     test_getSetOfSerializableT()
84     {
85         final Set<String> s1 = getSetOfSerializableT(); // OK, no warning
86         final Set<Integer> s2 = getSetOfSerializableT(); // OK, no warning
87         final Set<Long> s3 = getSetOfSerializableT(); // OK, no warning
88         final Set<Serializable> s4 = getSetOfSerializableT(); // OK, no warning
89         
90         // COMPILE FAILURE:
91         // final Set<Object> x = getSetOfT();
92         // final Set<?> x = getSetOfT();
93         
94         TypeCast.checkSet( s4, Serializable.class);
95     
96         try
97         {
98             TypeCast.checkSet( s1, String.class);
99             assert false;
100         }
101         catch( final ClassCastException e )
102         {
103             // should arrive here
104         }
105         
106         try
107         {
108             TypeCast.checkSet( s2, Integer.class );
109             assert false;
110         }
111         catch( final ClassCastException e )
112         {
113             // should arrive here
114         }
115         
116         try
117         {
118             TypeCast.checkSet( s3, Long.class );
119             assert false;
120         }
121         catch( final ClassCastException e )
122         {
123             // should arrive here
124         }
125       }
126     */

127     
128     /**
129         Method is "good"; no warnings.
130      */

131         private Set JavaDoc<? extends Serializable JavaDoc>
132     getSetOfSerializableUnknown()
133     {
134         final Set JavaDoc<Serializable JavaDoc> result = new HashSet JavaDoc<Serializable JavaDoc>();
135         result.add( new String JavaDoc("hello") );
136         result.add( new Integer JavaDoc(0) );
137         result.add( new Boolean JavaDoc(false) );
138         return result;
139     }
140     
141     /**
142     commented out to avoid compiler warnings; code compiles and tests good,
143     and is useful in understanding generics.
144     
145         // Please see how getSetOfT_UnknownSubclass() is implemented.
146         public void
147     test_getSetOfSerializableSubclassUnknown()
148     {
149         // s1 is effectively read-only, since nothing can be put into it
150         final Set<? extends Serializable> s1 = getSetOfSerializableUnknown();
151         
152         // warning: "unchecked cast"
153         final Set<Serializable> s2 = (Set<Serializable>)getSetOfSerializableUnknown();
154     }
155      */

156     
157     
158     /**
159         Method is "good"; no warnings.
160      */

161         private Set JavaDoc<Serializable JavaDoc>
162     getSetOfSerializable()
163     {
164         final Set JavaDoc<Serializable JavaDoc> result = new HashSet JavaDoc<Serializable JavaDoc>();
165         result.add( new String JavaDoc("hello") );
166         result.add( new Integer JavaDoc(0) );
167         result.add( new Boolean JavaDoc(false) );
168         return result;
169     }
170     
171     /**
172      */

173         public void
174     test_getSetOfSerializable()
175     {
176         // all OK. 's1' can contain any Serializable
177
final Set JavaDoc<Serializable JavaDoc> s1 = getSetOfSerializable();
178         TypeCast.checkSet( s1, Serializable JavaDoc.class );
179         
180         // all OK, no warning.
181
final Set JavaDoc<? extends Serializable JavaDoc> s2 = getSetOfSerializable();
182         TypeCast.checkSet( s2, Serializable JavaDoc.class );
183     }
184     
185     /*
186         private <T> void
187     checkValidItems( final Collection<T> c, final Class<T> theClass)
188     {
189         for( final T item : c )
190         {
191             if ( ! theClass.isAssignableFrom( item.getClass() ) )
192             {
193                 throw new ClassCastException(
194                     item.getClass() + " not assignable to " + theClass );
195             }
196         }
197     }
198     */

199        
200     
201         public void
202     testAssign()
203     {
204         final Set JavaDoc<Serializable JavaDoc> serializableSet = new HashSet JavaDoc<Serializable JavaDoc>();
205         final Set JavaDoc<String JavaDoc> stringSet = new HashSet JavaDoc<String JavaDoc>();
206         
207         // these of course must work; the types match
208
serializableSet.add( new Integer JavaDoc(0) );
209         serializableSet.add( new String JavaDoc() );
210         serializableSet.add( new Boolean JavaDoc(false) );
211         
212         stringSet.add( "hello" );
213         
214         serializableSet.addAll( stringSet );
215         
216         // This is counterintuitive"; we just added all members of
217
// 'stringSet' to 'serializableSet', but we cannot assign the Set itself:
218
// Set<Serializable> illegal = stringSet;
219

220        // However, we can make this assignment if we use a wildcard Set,
221
// no further add() calls to the set can be made.
222
Set JavaDoc<? extends Serializable JavaDoc> unknownSub1 = null;
223        unknownSub1 = stringSet;
224        unknownSub1 = serializableSet;
225     }
226     
227     
228         public void
229     testCheckedSet()
230     {
231         final Set JavaDoc<Object JavaDoc> s = new HashSet JavaDoc<Object JavaDoc>();
232         TypeCast.checkSet( s, String JavaDoc.class );
233         TypeCast.checkSet( s, Integer JavaDoc.class );
234         
235         s.add( "hello" );
236         TypeCast.checkSet( s, String JavaDoc.class );
237         TypeCast.checkSet( s, Object JavaDoc.class );
238         TypeCast.checkSet( s, Serializable JavaDoc.class );
239         
240         try { TypeCast.checkSet( s, Integer JavaDoc.class ); assert false;}
241             catch( Exception JavaDoc e ) {/*good, expected*/}
242         
243         final Set JavaDoc<String JavaDoc> ss = TypeCast.checkedStringSet( s );
244         try
245         {
246             // it's NOT a Set<Integer>, but let's verify that
247
// the exception is thrown.
248
final Set JavaDoc<Integer JavaDoc> x = TypeCast.asSet(ss);
249             x.add( new Integer JavaDoc(0) );
250             assert false;
251         }
252         catch( Exception JavaDoc e ) {/*good, expected*/}
253         
254         final Set JavaDoc<String JavaDoc> sss = TypeCast.checkedStringSet( ss );
255         // assert( sss == ss ); bummer, it's not smart enough to not wrap it twice
256

257         
258         final Set JavaDoc<Object JavaDoc> mixed = new HashSet JavaDoc<Object JavaDoc>();
259         mixed.add( "hello" );
260         mixed.add( new Integer JavaDoc(0) );
261         final Set JavaDoc<String JavaDoc> bogus = TypeCast.asSet(mixed);
262         final Set JavaDoc<String JavaDoc> checkedBogus = Collections.checkedSet( bogus, String JavaDoc.class );
263         // that worked. Our variant should reject it:
264
try
265         {
266             TypeCast.checkedStringSet( bogus );
267         }
268         catch( Exception JavaDoc e ) {/*good, expected*/}
269     }
270 }
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
Popular Tags