001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Serguei S.Zapreyev
020: * @version $Revision$
021: */package java.lang;
022:
023: /**
024: * Area for supposed testing is arrays returning mrthods for:
025: * - classes, interfaces
026: * - methods
027: * - constructors
028: **/
029:
030: import java.lang.reflect.*;
031:
032: import junit.framework.TestCase;
033:
034: /*
035: * Created on May 02, 2006
036: *
037: * This ClassGenericsTest2 class is used to test the Core API Class, Method, Constructor classes
038: *
039: */
040:
041: /**
042: * ###############################################################################
043: * ###############################################################################
044: * TODO: 1.
045: * ###############################################################################
046: * ###############################################################################
047: */
048:
049: @SuppressWarnings(value={"unchecked"})
050: public class ClassGenericsTest2<X> extends TestCase {
051: class Mc201 {
052: };
053:
054: interface MI201<T0 extends java.io.Serializable> {
055: };
056:
057: interface MI202<T1 extends MI201> {
058: };
059:
060: interface MI203<T2> extends MI201 {
061: };
062:
063: interface MI204<T2> {
064: interface MI205<T21, T22> {
065: };
066: };
067:
068: public class Mc202<T3 extends ClassGenericsTest2> {
069: public class Mc204<T5 extends ClassGenericsTest2> {
070: };
071: };
072:
073: class Mc203<T4 extends Thread & java.io.Serializable & Cloneable>
074: extends
075: java.lang.ClassGenericsTest2<? super Class>.Mc202<ClassGenericsTest2>
076: implements MI202<MI203<java.io.Serializable>>,
077: MI203<MI203<Cloneable>>, MI204.MI205<Type, Type> {
078: };
079:
080: /**
081: * check immutability for results of getTypeParameters(), getGenericInterfaces() methods of generalized member class
082: * and the attendant reflect implementation methods for WildcardType(getLowerBounds(), getUpperBounds()),
083: * TypeVariable(getBounds()), ParameterizedType(getActualTypeArguments())
084: */
085: public void test_2() {
086: Type ap[];
087: TypeVariable tv;
088: Type ab[];
089: Type ai[];
090: Type aa[];
091: Type aa2[];
092: Type oc;
093: WildcardType wc;
094: Type aa3[];
095: //////////////////////////////////////////////////////////////////////////////////////////////
096: ai = Mc203.class.getInterfaces();
097: //---
098: ai[0] = null;
099: ai = Mc203.class.getInterfaces();
100: if (ai.length != 3)
101: fail("test_2, case 001 FAILED: " + ai.length);
102: if (!((Class) ai[0]).getName().equals(
103: "java.lang.ClassGenericsTest2$MI202"))
104: fail("test_2, case 002 FAILED: "
105: + ((Class) ((ParameterizedType) ai[0]).getRawType())
106: .getName());
107:
108: ap = Mc203.class.getTypeParameters();
109: tv = (TypeVariable) ap[0];
110: ab = tv.getBounds();
111: //---
112: ab[0] = null;
113: ab = tv.getBounds();
114: if (!((Class) ab[0]).getName().equals("java.lang.Thread"))
115: fail("test_2, case 003 FAILED: "
116: + ((Class) ab[0]).getName());
117: //---
118: ap[0] = null;
119: ap = Mc203.class.getTypeParameters();
120: tv = (TypeVariable) ap[0];
121: if (!tv.getName().equals("T4"))
122: fail("test_2, case 004 FAILED: " + tv.getName());
123: //---
124: ap = null;
125: ap = Mc203.class.getTypeParameters();
126: tv = (TypeVariable) ap[0];
127: if (!tv.getName().equals("T4"))
128: fail("test_2, case 005 FAILED: " + tv.getName());
129:
130: //////////////////////////////////////////////////////////////////////////////////////////////
131: //---
132: ai = Mc203.class.getGenericInterfaces();
133: aa = ((ParameterizedType) ai[2]).getActualTypeArguments();
134: aa[0] = null;
135: aa[1] = null;
136: aa = ((ParameterizedType) ai[2]).getActualTypeArguments();
137: if (aa.length != 2)
138: fail("test_2, case 006 FAILED: " + aa.length);
139: if (!((Class) aa[0]).getName().equals("java.lang.reflect.Type"))
140: fail("test_2, case 007 FAILED: "
141: + ((Class) ((ParameterizedType) aa[0]).getRawType())
142: .getName());
143: if (!((Class) aa[1]).getName().equals("java.lang.reflect.Type"))
144: fail("test_2, case 008 FAILED: "
145: + ((Class) ((ParameterizedType) aa[1]).getRawType())
146: .getName());
147: //---
148: ai = Mc203.class.getGenericInterfaces();
149: ai[2] = null;
150: ai = Mc203.class.getGenericInterfaces();
151: if (!((Class) ((ParameterizedType) ai[2]).getRawType())
152: .getName().equals(
153: "java.lang.ClassGenericsTest2$MI204$MI205"))
154: fail("test_2, case 009 FAILED: "
155: + ((Class) ((ParameterizedType) ai[2]).getRawType())
156: .getName());
157: if (!((Class) ((ParameterizedType) ai[2]).getOwnerType())
158: .getName().equals("java.lang.ClassGenericsTest2$MI204"))
159: fail("test_2, case 010 FAILED: "
160: + ((Class) ((ParameterizedType) ai[2])
161: .getOwnerType()).getName());
162: //---
163: ai = Mc203.class.getGenericInterfaces();
164: aa = ((ParameterizedType) ai[1]).getActualTypeArguments();
165: aa2 = ((ParameterizedType) aa[0]).getActualTypeArguments(); //java.lang.Cloneable
166: aa2[0] = null;
167: aa2 = ((ParameterizedType) aa[0]).getActualTypeArguments(); //java.lang.Cloneable
168: if (aa2.length != 1)
169: fail("test_2, case 009 FAILED: " + aa.length);
170: if (!((Class) aa2[0]).getName().equals("java.lang.Cloneable"))
171: fail("test_2, case 011 FAILED: "
172: + ((Class) ((ParameterizedType) aa2[0])
173: .getRawType()).getName());
174: //---
175: ai = Mc203.class.getGenericInterfaces();
176: aa = ((ParameterizedType) ai[1]).getActualTypeArguments();
177: if (aa.length != 1)
178: fail("test_2, case 012 FAILED: " + aa.length);
179: aa[0] = null;
180: aa = ((ParameterizedType) ai[1]).getActualTypeArguments();
181: if (!((Class) ((ParameterizedType) aa[0]).getRawType())
182: .getName().equals("java.lang.ClassGenericsTest2$MI203"))
183: fail("test_2, case 013 FAILED: "
184: + ((Class) ((ParameterizedType) aa[0]).getRawType())
185: .getName());
186: if (!((Class) ((ParameterizedType) aa[0]).getOwnerType())
187: .getName().equals("java.lang.ClassGenericsTest2"))
188: fail("test_2, case 014 FAILED: "
189: + ((Class) ((ParameterizedType) aa[0])
190: .getOwnerType()).getName());
191: //---
192: ai = Mc203.class.getGenericInterfaces();
193: ai[1] = null;
194: ai = Mc203.class.getGenericInterfaces();
195: if (!((Class) ((ParameterizedType) ai[1]).getRawType())
196: .getName().equals("java.lang.ClassGenericsTest2$MI203"))
197: fail("test_2, case 015 FAILED: "
198: + ((Class) ((ParameterizedType) ai[1]).getRawType())
199: .getName());
200: if (!((Class) ((ParameterizedType) ai[1]).getOwnerType())
201: .getName().equals("java.lang.ClassGenericsTest2"))
202: fail("test_2, case 016 FAILED: "
203: + ((Class) ((ParameterizedType) ai[1])
204: .getOwnerType()).getName());
205: //---
206: ai = Mc203.class.getGenericInterfaces();
207: aa = ((ParameterizedType) ai[0]).getActualTypeArguments();
208: aa2 = ((ParameterizedType) aa[0]).getActualTypeArguments(); //java.io.Serializable
209: aa2[0] = null;
210: aa2 = ((ParameterizedType) aa[0]).getActualTypeArguments(); //java.io.Serializable
211: if (aa2.length != 1)
212: fail("test_2, case 017 FAILED: " + aa.length);
213: if (!((Class) aa2[0]).getName().equals("java.io.Serializable"))
214: fail("test_2, case 018 FAILED: "
215: + ((Class) ((ParameterizedType) aa2[0])
216: .getRawType()).getName());
217: //---
218: ai = Mc203.class.getGenericInterfaces();
219: aa = ((ParameterizedType) ai[0]).getActualTypeArguments();
220: aa[0] = null;
221: aa = ((ParameterizedType) ai[0]).getActualTypeArguments();
222: if (aa.length != 1)
223: fail("test_2, case 019 FAILED: " + aa.length);
224: if (!((Class) ((ParameterizedType) aa[0]).getRawType())
225: .getName().equals("java.lang.ClassGenericsTest2$MI203"))
226: fail("test_2, case 020 FAILED: "
227: + ((Class) ((ParameterizedType) aa[0]).getRawType())
228: .getName());
229: if (!((Class) ((ParameterizedType) aa[0]).getOwnerType())
230: .getName().equals("java.lang.ClassGenericsTest2"))
231: fail("test_2, case 021 FAILED: "
232: + ((Class) ((ParameterizedType) aa[0])
233: .getOwnerType()).getName());
234: //---
235: ai = Mc203.class.getGenericInterfaces();
236: ai[0] = null;
237: ai = Mc203.class.getGenericInterfaces();
238: if (ai.length != 3)
239: fail("test_2, case 022 FAILED: " + ai.length);
240: if (!((Class) ((ParameterizedType) ai[0]).getRawType())
241: .getName().equals("java.lang.ClassGenericsTest2$MI202"))
242: fail("test_2, case 023 FAILED: "
243: + ((Class) ((ParameterizedType) ai[0]).getRawType())
244: .getName());
245: if (!((Class) ((ParameterizedType) ai[0]).getOwnerType())
246: .getName().equals("java.lang.ClassGenericsTest2"))
247: fail("test_2, case 024 FAILED: "
248: + ((Class) ((ParameterizedType) ai[0])
249: .getOwnerType()).getName());
250:
251: ai = Mc203.class.getInterfaces();
252: ai[0] = null;
253: ai = Mc203.class.getInterfaces();
254: if (ai.length != 3)
255: fail("test_2, case 025 FAILED: " + ai.length);
256: if (!((Class) ai[0]).getName().equals(
257: "java.lang.ClassGenericsTest2$MI202"))
258: fail("test_2, case 026 FAILED: "
259: + ((Class) ((ParameterizedType) ai[0]).getRawType())
260: .getName());
261: //////////////////////////////////////////////////////////////////////////////////////////////
262: aa = ((ParameterizedType) Mc203.class.getGenericSuperclass())
263: .getActualTypeArguments();
264: aa[0] = null;
265: aa = ((ParameterizedType) Mc203.class.getGenericSuperclass())
266: .getActualTypeArguments();
267: if (aa.length != 1)
268: fail("test_2, case 027 FAILED: " + aa.length);
269: if (!((Class) aa[0]).getName().equals(
270: "java.lang.ClassGenericsTest2"))
271: fail("test_2, case 028 FAILED: "
272: + ((Class) aa[0]).getName());
273: //---
274: oc = ((ParameterizedType) Mc203.class.getGenericSuperclass())
275: .getOwnerType();
276: aa = ((ParameterizedType) oc).getActualTypeArguments();
277: aa[0] = null;
278: aa = ((ParameterizedType) oc).getActualTypeArguments();
279: if (aa.length != 1)
280: fail("test_2, case 029 FAILED: " + aa.length);
281: wc = (WildcardType) aa[0];
282: //---
283: aa3 = wc.getLowerBounds();
284: aa3[0] = null;
285: aa3 = wc.getLowerBounds();
286: if (aa3.length != 1)
287: fail("test_2, case 030 FAILED: " + aa3.length);
288: if (!((Class) aa3[0]).equals(Class.class))
289: fail("test_2, case 031 FAILED: " + ((Class) aa3[0]));
290: //---
291: aa3 = wc.getUpperBounds();
292: aa3[0] = null;
293: aa3 = wc.getUpperBounds();
294: if (aa3.length != 1)
295: fail("test_2, case 032 FAILED: " + aa3.length);
296: if (!((Class) aa3[0]).equals(Object.class))
297: fail("test_2, case 033 FAILED: " + ((Class) aa3[0]));
298: }
299:
300: /**
301: * check immutability for results of getGenericExceptionTypes(), getGenericParameterTypes(), getTypeParameters() of generalized method
302: * and the attendant reflect implementation methods for WildcardType(getLowerBounds(), getUpperBounds()),
303: * TypeVariable(getBounds()), ParameterizedType(getActualTypeArguments())
304: */
305: class Mc205 extends Thread implements java.io.Serializable,
306: Cloneable {
307: private static final long serialVersionUID = 0L;
308: };
309:
310: static class Mc209 extends Throwable implements
311: java.io.Serializable, Cloneable {
312: private static final long serialVersionUID = 0L;
313: };
314:
315: public <UuUuU extends Throwable, TM1, TM2 extends Thread & java.io.Serializable & Cloneable> void foo1For_5(
316: UuUuU a1) throws UuUuU, java.io.IOException {
317: }
318:
319: public void test_5() {
320: TypeVariable tv;
321: Type ap[];
322: Type ab[];
323: Type aet[];
324: Type atp[];
325: Method m = null;
326: try {
327: java.lang.reflect.Method am[] = ClassGenericsTest2.class
328: .getDeclaredMethods();
329: for (int i = 0; i < am.length; i++) {
330: if (am[i].getName().equals("foo1For_5")) {
331: m = am[i];
332: }
333: }
334: } catch (Exception e) {
335: fail("test_5, case 001 FAILED: " + e.toString());
336: }
337: ///////
338: atp = m.getTypeParameters();
339: if (atp.length != 3)
340: fail("test_5, case 002 FAILED: " + atp.length);
341: //---
342: atp[2] = null;
343: atp = m.getTypeParameters();
344: if (atp.length != 3)
345: fail("test_5, case 003 FAILED: " + atp.length);
346: tv = (TypeVariable) atp[2];
347: if (!tv.getName().equals("TM2"))
348: fail("test_5, case 004 FAILED: " + tv.getName());
349: if (!((Method) tv.getGenericDeclaration()).getName().equals(
350: "foo1For_5"))
351: fail("test_5, case 005 FAILED: "
352: + ((Method) tv.getGenericDeclaration()).getName());
353: ab = tv.getBounds();
354: //---
355: ab[1] = null;
356: ab = tv.getBounds();
357: if (ab.length != 3)
358: fail("test_5, case 006 FAILED: " + ab.length);
359: if (!((Class) ab[0]).getName().equals("java.lang.Thread"))
360: fail("test_5, case 007 FAILED: "
361: + ((Class) ab[0]).getName());
362: if (!((Class) ab[1]).getName().equals("java.io.Serializable"))
363: fail("test_2, case 008 FAILED: "
364: + ((Class) ab[1]).getName());
365: if (!((Class) ab[2]).getName().equals("java.lang.Cloneable"))
366: fail("test_2, case 009 FAILED: "
367: + ((Class) ab[2]).getName());
368: ///////
369: ap = m.getGenericParameterTypes();
370: if (ap.length != 1)
371: fail("test_5, case 010 FAILED: " + ap.length);
372: //---
373: ap[0] = null;
374: ap = m.getGenericParameterTypes();
375: tv = (TypeVariable) ap[0];
376: if (!tv.getName().equals("UuUuU"))
377: fail("test_5, case 011 FAILED: " + tv.getName());
378: ///////
379: aet = m.getGenericExceptionTypes();
380: if (aet.length != 2)
381: fail("test_5, case 012 FAILED: " + ap.length);
382: //---
383: aet[0] = null;
384: aet = m.getGenericExceptionTypes();
385: if (aet.length != 2)
386: fail("test_5, case 013 FAILED: " + ap.length);
387: tv = (TypeVariable) aet[0];
388: if (!tv.getName().equals("UuUuU"))
389: fail("test_5, case 014 FAILED: " + tv.getName());
390: if (!((Class) aet[1]).getName().equals("java.io.IOException"))
391: fail("test_5, case 015 FAILED: "
392: + ((Class) aet[1]).getName());
393: }
394:
395: /**
396: * check immutability for results of getGenericExceptionTypes(), getGenericParameterTypes(), getTypeParameters() of generalized constructor
397: * and the attendant reflect implementation methods for WildcardType(getLowerBounds(), getUpperBounds()),
398: * TypeVariable(getBounds()), ParameterizedType(getActualTypeArguments())
399: */
400: class MC006 {
401: public <UuUuU extends Throwable, TM1, TM2 extends Thread & java.io.Serializable & Cloneable> MC006(
402: UuUuU a1) throws UuUuU, java.io.IOException {
403: }
404: }
405:
406: public void test_6() {
407: TypeVariable tv;
408: Type ap[];
409: Type ab[];
410: Type aet[];
411: Type atp[];
412: Constructor m = null;
413: try {
414: m = ClassGenericsTest2.MC006.class
415: .getConstructor(new Class[] {
416: ClassGenericsTest2.class, Throwable.class });
417: } catch (Exception e) {
418: fail("test_6, case 001 FAILED: " + e.toString());
419: }
420: ///////
421: atp = m.getTypeParameters();
422: if (atp.length != 3)
423: fail("test_6, case 002 FAILED: " + atp.length);
424: //---
425: atp[2] = null;
426: atp = m.getTypeParameters();
427: tv = (TypeVariable) atp[2];
428: if (!tv.getName().equals("TM2"))
429: fail("test_6, case 003 FAILED: " + tv.getName());
430: if (!((Constructor) tv.getGenericDeclaration()).getName()
431: .equals("java.lang.ClassGenericsTest2$MC006"))
432: fail("test_6, case 004 FAILED: "
433: + ((Constructor) tv.getGenericDeclaration())
434: .getName());
435: ab = tv.getBounds();
436: //---
437: ab[1] = null;
438: ab = tv.getBounds();
439: if (ab.length != 3)
440: fail("test_6, case 005 FAILED: " + ab.length);
441: if (!((Class) ab[0]).getName().equals("java.lang.Thread"))
442: fail("test_6, case 006 FAILED: "
443: + ((Class) ab[0]).getName());
444: if (!((Class) ab[1]).getName().equals("java.io.Serializable"))
445: fail("test_2, case 007 FAILED: "
446: + ((Class) ab[1]).getName());
447: if (!((Class) ab[2]).getName().equals("java.lang.Cloneable"))
448: fail("test_2, case 008 FAILED: "
449: + ((Class) ab[2]).getName());
450: ///////
451: ap = m.getGenericParameterTypes();
452: if (ap.length != 1)
453: fail("test_6, case 009 FAILED: " + ap.length);
454: //---
455: ap[0] = null;
456: ap = m.getGenericParameterTypes();
457: tv = (TypeVariable) ap[0];
458: if (!tv.getName().equals("UuUuU"))
459: fail("test_6, case 010 FAILED: " + tv.getName());
460: ///////
461: aet = m.getGenericExceptionTypes();
462: if (aet.length != 2)
463: fail("test_6, case 011 FAILED: " + ap.length);
464: //---
465: aet[0] = null;
466: aet = m.getGenericExceptionTypes();
467: if (aet.length != 2)
468: fail("test_6, case 012 FAILED: " + ap.length);
469: tv = (TypeVariable) aet[0];
470: if (!tv.getName().equals("UuUuU"))
471: fail("test_6, case 013 FAILED: " + tv.getName());
472: if (!((Class) aet[1]).getName().equals("java.io.IOException"))
473: fail("test_6, case 014 FAILED: "
474: + ((Class) aet[1]).getName());
475: }
476: }
|