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: import junit.framework.TestCase;
024:
025: /*
026: * Created on June 03, 2006
027: *
028: * This ClassGenericsTest5 class is used to test the Core API Class, Method,
029: * Field, Constructor classes
030: *
031: */
032:
033: /**
034: * ###############################################################################
035: * ###############################################################################
036: * TODO: 1. Do clear should I use "$" instead of "." in the commented fragments?
037: * ###############################################################################
038: * ###############################################################################
039: */
040:
041: public class ClassGenericsTest5/*<X>*/extends TestCase {
042:
043: static class B1 {
044: public Object foo() {
045: return null;
046: }
047:
048: public Object bar(int i, Object... o) {
049: return null;
050: }
051:
052: public Object bar2(int i, Object[][][] o) {
053: return null;
054: }
055:
056: public Object bar3(int i, int[][][] ai1, int... ai2) {
057: return null;
058: }
059:
060: public <X> Object bar4(int i, int[][][] ai1,
061: OneParamType<Void[][][]>[] ai00,
062: OneParamType<X[][][]> ai0, Object... ai2) {
063: return null;
064: }
065:
066: public static void main1(String... s) {
067: }
068:
069: public static void main2(String[] s) {
070: }
071: }
072:
073: static class B2 extends B1 {
074: public String foo() {
075: return "";
076: }
077:
078: public String bar(int i, Object... o) {
079: return "";
080: }
081:
082: public String bar2(int i, Object[][][] o) {
083: return "";
084: }
085:
086: public Object bar3(int i, int[][][] ai1, int... ai2) {
087: return null;
088: }
089: }
090:
091: static abstract class OneParamType<X> {
092: }
093:
094: static abstract class C {
095: protected abstract <T extends Throwable> OneParamType<? extends T> go(
096: T t) throws T, Error;
097:
098: protected abstract <T extends Throwable> OneParamType<? extends String> go2(
099: T t) throws T, Error;
100: }
101:
102: static abstract strictfp class CC {
103: protected <T extends Throwable & Comparable<Throwable>> CC(
104: T num, OneParamType<? super T> l) throws T, Error {
105: }
106: }
107:
108: /**
109: * few checks for signatured methods
110: */
111: public void test_1() throws Exception {
112: //if(!C.class.getDeclaredMethod("go2",Throwable.class).toGenericString().equals("protected abstract <T> java.lang.ClassGenericsTest5$OneParamType<? extends java.lang.String> java.lang.ClassGenericsTest5$C.go2(T) throws T,java.lang.Error"))
113: // fail("test_1, case 001 FAILED: "+C.class.getDeclaredMethod("go2",Throwable.class).toGenericString());
114: if (!C.class
115: .getDeclaredMethod("go2", Throwable.class)
116: .toGenericString()
117: .equals(
118: "protected abstract <T> java.lang.ClassGenericsTest5.OneParamType<? extends java.lang.String> java.lang.ClassGenericsTest5$C.go2(T) throws T,java.lang.Error"))
119: fail("test_1, case 001 FAILED: "
120: + C.class.getDeclaredMethod("go2", Throwable.class)
121: .toGenericString());
122:
123: if (!C.class.getDeclaredMethod("go2", Throwable.class)
124: .getTypeParameters()[0].toString().equals("T"))
125: fail("test_1, case 002 FAILED: "
126: + C.class.getDeclaredMethod("go2", Throwable.class)
127: .getTypeParameters()[0].toString());
128:
129: if (!B2.class
130: .getDeclaredMethod("bar", int.class, Object[].class)
131: .toGenericString()
132: .equals(
133: "public java.lang.String java.lang.ClassGenericsTest5$B2.bar(int,java.lang.Object[])"))
134: fail("test_1, case 003 FAILED: "
135: + B2.class.getDeclaredMethod("bar", int.class,
136: Object[].class).toGenericString());
137:
138: if (!B2.class.getDeclaredMethod("bar", int.class,
139: Object[].class).getParameterTypes()[1].toString()
140: .equals("class [Ljava.lang.Object;"))
141: fail("test_1, case 004 FAILED: "
142: + B2.class.getDeclaredMethod("bar", int.class,
143: Object[].class).getParameterTypes()[1]
144: .toString());
145:
146: if (!B2.class
147: .getDeclaredMethod("bar2", int.class,
148: Object[][][].class)
149: .toGenericString()
150: .equals(
151: "public java.lang.String java.lang.ClassGenericsTest5$B2.bar2(int,java.lang.Object[][][])"))
152: fail("test_1, case 005 FAILED: "
153: + B2.class.getDeclaredMethod("bar2", int.class,
154: Object[][][].class).toGenericString());
155:
156: if (!B2.class
157: .getDeclaredMethod("bar3", int.class, int[][][].class,
158: int[].class)
159: .toGenericString()
160: .equals(
161: "public java.lang.Object java.lang.ClassGenericsTest5$B2.bar3(int,int[][][],int[])"))
162: fail("test_1, case 006 FAILED: "
163: + B2.class.getDeclaredMethod("bar3", int.class,
164: int[][][].class, int[].class)
165: .toGenericString());
166:
167: int res = 0;
168: java.lang.reflect.Method[] ms = B1.class.getDeclaredMethods();
169: for (int i = 0; i < ms.length; i++) {
170: //if(ms[i].toGenericString().equals("public <X> java.lang.Object java.lang.ClassGenericsTest5$B1.bar4(int,int[][][],java.lang.ClassGenericsTest5$OneParamType<java.lang.Void[][][]>[],java.lang.ClassGenericsTest5$OneParamType<X[][][]>,java.lang.Object[])")) res += 1;
171: if (ms[i]
172: .toGenericString()
173: .equals(
174: "public <X> java.lang.Object java.lang.ClassGenericsTest5$B1.bar4(int,int[][][],java.lang.ClassGenericsTest5.OneParamType<java.lang.Void[][][]>[],java.lang.ClassGenericsTest5.OneParamType<X[][][]>,java.lang.Object[])"))
175: res += 1;
176: if (ms[i]
177: .toGenericString()
178: .equals(
179: "public java.lang.Object java.lang.ClassGenericsTest5$B1.bar3(int,int[][][],int[])"))
180: res += 10;
181: if (ms[i]
182: .toGenericString()
183: .equals(
184: "public java.lang.Object java.lang.ClassGenericsTest5$B1.bar2(int,java.lang.Object[][][])"))
185: res += 100;
186: if (ms[i]
187: .toGenericString()
188: .equals(
189: "public java.lang.Object java.lang.ClassGenericsTest5$B1.bar(int,java.lang.Object[])"))
190: res += 1000;
191: if (ms[i]
192: .toGenericString()
193: .equals(
194: "public java.lang.Object java.lang.ClassGenericsTest5$B1.foo()"))
195: res += 10000;
196: if (ms[i]
197: .toGenericString()
198: .equals(
199: "public static void java.lang.ClassGenericsTest5$B1.main2(java.lang.String[])"))
200: res += 100000;
201: if (ms[i]
202: .toGenericString()
203: .equals(
204: "public static void java.lang.ClassGenericsTest5$B1.main1(java.lang.String[])"))
205: res += 1000000;
206: }
207: if (res != 1111111)
208: fail("test_1, case 007 FAILED: "
209: + "The uncoincedence has been detected!");
210:
211: //if(!B1.class.getDeclaredMethod("bar4",int.class, int[][][].class, OneParamType[].class, OneParamType.class, Object[].class).toGenericString().equals("public <X> java.lang.Object java.lang.ClassGenericsTest5$B1.bar4(int,int[][][],java.lang.ClassGenericsTest5$OneParamType<java.lang.Void[][][]>[],java.lang.ClassGenericsTest5$OneParamType<X[][][]>,java.lang.Object[])"))
212: // fail("test_1, case 008 FAILED: "+B1.class.getDeclaredMethod("bar4",int.class, int[][][].class, OneParamType[].class, OneParamType.class, Object[].class).toGenericString());
213: if (!B1.class
214: .getDeclaredMethod("bar4", int.class, int[][][].class,
215: OneParamType[].class, OneParamType.class,
216: Object[].class)
217: .toGenericString()
218: .equals(
219: "public <X> java.lang.Object java.lang.ClassGenericsTest5$B1.bar4(int,int[][][],java.lang.ClassGenericsTest5.OneParamType<java.lang.Void[][][]>[],java.lang.ClassGenericsTest5.OneParamType<X[][][]>,java.lang.Object[])"))
220: fail("test_1, case 008 FAILED: "
221: + B1.class.getDeclaredMethod("bar4", int.class,
222: int[][][].class, OneParamType[].class,
223: OneParamType.class, Object[].class)
224: .toGenericString());
225:
226: java.lang.reflect.Method mmm = B1.class.getDeclaredMethod(
227: "bar4", int.class, int[][][].class,
228: OneParamType[].class, OneParamType.class,
229: Object[].class);
230: if (!(mmm.getGenericParameterTypes()[1] instanceof java.lang.reflect.GenericArrayType))
231: fail("test_1, case 009 FAILED: "
232: + (mmm.getGenericParameterTypes()[1] instanceof java.lang.reflect.GenericArrayType));
233: if (!(mmm.getGenericParameterTypes()[2] instanceof java.lang.reflect.GenericArrayType))
234: fail("test_1, case 010 FAILED: "
235: + (mmm.getGenericParameterTypes()[2] instanceof java.lang.reflect.GenericArrayType));
236: if ((mmm.getGenericParameterTypes()[3] instanceof java.lang.reflect.GenericArrayType))
237: fail("test_1, case 011 FAILED: "
238: + (mmm.getGenericParameterTypes()[3] instanceof java.lang.reflect.GenericArrayType));
239:
240: //if(!CC.class.getDeclaredConstructor(Throwable.class,OneParamType.class).toGenericString().equals("protected strictfp <T> java.lang.ClassGenericsTest5$CC(T,java.lang.ClassGenericsTest5$OneParamType<? super T>) throws T,java.lang.Error")) fail("test_1, case 012 FAILED: "+CC.class.getDeclaredConstructor(Throwable.class,OneParamType.class).toGenericString());
241: if (!CC.class
242: .getDeclaredConstructor(Throwable.class,
243: OneParamType.class)
244: .toGenericString()
245: .equals(
246: "protected strictfp <T> java.lang.ClassGenericsTest5$CC(T,java.lang.ClassGenericsTest5.OneParamType<? super T>) throws T,java.lang.Error"))
247: fail("test_1, case 012 FAILED: "
248: + CC.class.getDeclaredConstructor(Throwable.class,
249: OneParamType.class).toGenericString());
250: }
251: }
|