View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.reflect;
18  
19  import java.io.Serializable;
20  import java.lang.reflect.Field;
21  import java.lang.reflect.GenericArrayType;
22  import java.lang.reflect.Method;
23  import java.lang.reflect.ParameterizedType;
24  import java.lang.reflect.Type;
25  import java.lang.reflect.TypeVariable;
26  import java.lang.reflect.WildcardType;
27  import java.net.URI;
28  import java.util.Arrays;
29  import java.util.Collection;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.TreeSet;
34  
35  import org.apache.commons.lang3.StringEscapeUtils;
36  import org.apache.commons.lang3.reflect.testbed.Foo;
37  import org.apache.commons.lang3.reflect.testbed.GenericParent;
38  import org.apache.commons.lang3.reflect.testbed.GenericTypeHolder;
39  import org.apache.commons.lang3.reflect.testbed.StringParameterizedChild;
40  import org.junit.Assert;
41  import org.junit.Test;
42  
43  /**
44   * Test TypeUtils
45   */
46  @SuppressWarnings({ "unchecked", "unused" , "rawtypes" })
47  //raw types, where used, are used purposely
48  public class TypeUtilsTest<B> {
49  
50      public interface This<K, V> {
51      }
52  
53      public class That<K, V> implements This<K, V> {
54      }
55  
56      public interface And<K, V> extends This<Number, Number> {
57      }
58  
59      public class The<K, V> extends That<Number, Number> implements And<String, String> {
60      }
61  
62      public class Other<T> implements This<String, T> {
63      }
64  
65      public class Thing<Q> extends Other<B> {
66      }
67  
68      public class Tester implements This<String, B> {
69      }
70  
71      public This<String, String> dis;
72  
73      public That<String, String> dat;
74  
75      public The<String, String> da;
76  
77      public Other<String> uhder;
78  
79      public Thing ding;
80  
81      public TypeUtilsTest<String>.Tester tester;
82  
83      public Tester tester2;
84  
85      public TypeUtilsTest<String>.That<String, String> dat2;
86  
87      public TypeUtilsTest<Number>.That<String, String> dat3;
88  
89      public Comparable<? extends Integer>[] intWildcardComparable;
90  
91      public static Comparable<String> stringComparable;
92  
93      public static Comparable<URI> uriComparable;
94  
95      public static Comparable<Integer> intComparable;
96  
97      public static Comparable<Long> longComparable;
98  
99      public static Comparable<?> wildcardComparable;
100 
101     public static URI uri;
102 
103     public void dummyMethod(final List list0, final List<Object> list1, final List<?> list2,
104             final List<? super Object> list3, final List<String> list4, final List<? extends String> list5,
105             final List<? super String> list6, final List[] list7, final List<Object>[] list8, final List<?>[] list9,
106             final List<? super Object>[] list10, final List<String>[] list11, final List<? extends String>[] list12,
107             final List<? super String>[] list13) {
108     }
109 
110     @SuppressWarnings("boxing") // deliberately used here
111     @Test
112     public void testIsAssignable() throws SecurityException, NoSuchMethodException,
113             NoSuchFieldException {
114         List list0 = null;
115         List<Object> list1 = null;
116         List<?> list2 = null;
117         List<? super Object> list3 = null;
118         List<String> list4 = null;
119         List<? extends String> list5 = null;
120         List<? super String> list6 = null;
121         List[] list7 = null;
122         List<Object>[] list8 = null;
123         List<?>[] list9 = null;
124         List<? super Object>[] list10 = null;
125         List<String>[] list11 = null;
126         List<? extends String>[] list12 = null;
127         List<? super String>[] list13;
128         final Class<?> clazz = getClass();
129         final Method method = clazz.getMethod("dummyMethod", List.class, List.class, List.class,
130                 List.class, List.class, List.class, List.class, List[].class, List[].class,
131                 List[].class, List[].class, List[].class, List[].class, List[].class);
132         final Type[] types = method.getGenericParameterTypes();
133 //        list0 = list0;
134         delegateBooleanAssertion(types, 0, 0, true);
135         list1 = list0;
136         delegateBooleanAssertion(types, 0, 1, true);
137         list0 = list1;
138         delegateBooleanAssertion(types, 1, 0, true);
139         list2 = list0;
140         delegateBooleanAssertion(types, 0, 2, true);
141         list0 = list2;
142         delegateBooleanAssertion(types, 2, 0, true);
143         list3 = list0;
144         delegateBooleanAssertion(types, 0, 3, true);
145         list0 = list3;
146         delegateBooleanAssertion(types, 3, 0, true);
147         list4 = list0;
148         delegateBooleanAssertion(types, 0, 4, true);
149         list0 = list4;
150         delegateBooleanAssertion(types, 4, 0, true);
151         list5 = list0;
152         delegateBooleanAssertion(types, 0, 5, true);
153         list0 = list5;
154         delegateBooleanAssertion(types, 5, 0, true);
155         list6 = list0;
156         delegateBooleanAssertion(types, 0, 6, true);
157         list0 = list6;
158         delegateBooleanAssertion(types, 6, 0, true);
159 //        list1 = list1;
160         delegateBooleanAssertion(types, 1, 1, true);
161         list2 = list1;
162         delegateBooleanAssertion(types, 1, 2, true);
163         list1 = (List<Object>) list2;
164         delegateBooleanAssertion(types, 2, 1, false);
165         list3 = list1;
166         delegateBooleanAssertion(types, 1, 3, true);
167         list1 = (List<Object>) list3;
168         delegateBooleanAssertion(types, 3, 1, false);
169         // list4 = list1;
170         delegateBooleanAssertion(types, 1, 4, false);
171         // list1 = list4;
172         delegateBooleanAssertion(types, 4, 1, false);
173         // list5 = list1;
174         delegateBooleanAssertion(types, 1, 5, false);
175         // list1 = list5;
176         delegateBooleanAssertion(types, 5, 1, false);
177         list6 = list1;
178         delegateBooleanAssertion(types, 1, 6, true);
179         list1 = (List<Object>) list6;
180         delegateBooleanAssertion(types, 6, 1, false);
181 //        list2 = list2;
182         delegateBooleanAssertion(types, 2, 2, true);
183         list2 = list3;
184         delegateBooleanAssertion(types, 2, 3, false);
185         list2 = list4;
186         delegateBooleanAssertion(types, 3, 2, true);
187         list3 = (List<? super Object>) list2;
188         delegateBooleanAssertion(types, 2, 4, false);
189         list2 = list5;
190         delegateBooleanAssertion(types, 4, 2, true);
191         list4 = (List<String>) list2;
192         delegateBooleanAssertion(types, 2, 5, false);
193         list2 = list6;
194         delegateBooleanAssertion(types, 5, 2, true);
195         list5 = (List<? extends String>) list2;
196         delegateBooleanAssertion(types, 2, 6, false);
197 //        list3 = list3;
198         delegateBooleanAssertion(types, 6, 2, true);
199         list6 = (List<? super String>) list2;
200         delegateBooleanAssertion(types, 3, 3, true);
201         // list4 = list3;
202         delegateBooleanAssertion(types, 3, 4, false);
203         // list3 = list4;
204         delegateBooleanAssertion(types, 4, 3, false);
205         // list5 = list3;
206         delegateBooleanAssertion(types, 3, 5, false);
207         // list3 = list5;
208         delegateBooleanAssertion(types, 5, 3, false);
209         list6 = list3;
210         delegateBooleanAssertion(types, 3, 6, true);
211         list3 = (List<? super Object>) list6;
212         delegateBooleanAssertion(types, 6, 3, false);
213 //        list4 = list4;
214         delegateBooleanAssertion(types, 4, 4, true);
215         list5 = list4;
216         delegateBooleanAssertion(types, 4, 5, true);
217         list4 = (List<String>) list5;
218         delegateBooleanAssertion(types, 5, 4, false);
219         list6 = list4;
220         delegateBooleanAssertion(types, 4, 6, true);
221         list4 = (List<String>) list6;
222         delegateBooleanAssertion(types, 6, 4, false);
223 //        list5 = list5;
224         delegateBooleanAssertion(types, 5, 5, true);
225         list6 = (List<? super String>) list5;
226         delegateBooleanAssertion(types, 5, 6, false);
227         list5 = (List<? extends String>) list6;
228         delegateBooleanAssertion(types, 6, 5, false);
229 //        list6 = list6;
230         delegateBooleanAssertion(types, 6, 6, true);
231 
232 //        list7 = list7;
233         delegateBooleanAssertion(types, 7, 7, true);
234         list8 = list7;
235         delegateBooleanAssertion(types, 7, 8, true);
236         list7 = list8;
237         delegateBooleanAssertion(types, 8, 7, true);
238         list9 = list7;
239         delegateBooleanAssertion(types, 7, 9, true);
240         list7 = list9;
241         delegateBooleanAssertion(types, 9, 7, true);
242         list10 = list7;
243         delegateBooleanAssertion(types, 7, 10, true);
244         list7 = list10;
245         delegateBooleanAssertion(types, 10, 7, true);
246         list11 = list7;
247         delegateBooleanAssertion(types, 7, 11, true);
248         list7 = list11;
249         delegateBooleanAssertion(types, 11, 7, true);
250         list12 = list7;
251         delegateBooleanAssertion(types, 7, 12, true);
252         list7 = list12;
253         delegateBooleanAssertion(types, 12, 7, true);
254         list13 = list7;
255         delegateBooleanAssertion(types, 7, 13, true);
256         list7 = list13;
257         delegateBooleanAssertion(types, 13, 7, true);
258 //        list8 = list8;
259         delegateBooleanAssertion(types, 8, 8, true);
260         list9 = list8;
261         delegateBooleanAssertion(types, 8, 9, true);
262         list8 = (List<Object>[]) list9;
263         delegateBooleanAssertion(types, 9, 8, false);
264         list10 = list8;
265         delegateBooleanAssertion(types, 8, 10, true);
266         list8 = (List<Object>[]) list10; // NOTE cast is required by Sun Java, but not by Eclipse
267         delegateBooleanAssertion(types, 10, 8, false);
268         // list11 = list8;
269         delegateBooleanAssertion(types, 8, 11, false);
270         // list8 = list11;
271         delegateBooleanAssertion(types, 11, 8, false);
272         // list12 = list8;
273         delegateBooleanAssertion(types, 8, 12, false);
274         // list8 = list12;
275         delegateBooleanAssertion(types, 12, 8, false);
276         list13 = list8;
277         delegateBooleanAssertion(types, 8, 13, true);
278         list8 = (List<Object>[]) list13;
279         delegateBooleanAssertion(types, 13, 8, false);
280 //        list9 = list9;
281         delegateBooleanAssertion(types, 9, 9, true);
282         list10 = (List<? super Object>[]) list9;
283         delegateBooleanAssertion(types, 9, 10, false);
284         list9 = list10;
285         delegateBooleanAssertion(types, 10, 9, true);
286         list11 = (List<String>[]) list9;
287         delegateBooleanAssertion(types, 9, 11, false);
288         list9 = list11;
289         delegateBooleanAssertion(types, 11, 9, true);
290         list12 = (List<? extends String>[]) list9;
291         delegateBooleanAssertion(types, 9, 12, false);
292         list9 = list12;
293         delegateBooleanAssertion(types, 12, 9, true);
294         list13 = (List<? super String>[]) list9;
295         delegateBooleanAssertion(types, 9, 13, false);
296         list9 = list13;
297         delegateBooleanAssertion(types, 13, 9, true);
298 //        list10 = list10;
299         delegateBooleanAssertion(types, 10, 10, true);
300         // list11 = list10;
301         delegateBooleanAssertion(types, 10, 11, false);
302         // list10 = list11;
303         delegateBooleanAssertion(types, 11, 10, false);
304         // list12 = list10;
305         delegateBooleanAssertion(types, 10, 12, false);
306         // list10 = list12;
307         delegateBooleanAssertion(types, 12, 10, false);
308         list13 = list10;
309         delegateBooleanAssertion(types, 10, 13, true);
310         list10 = (List<? super Object>[]) list13;
311         delegateBooleanAssertion(types, 13, 10, false);
312 //        list11 = list11;
313         delegateBooleanAssertion(types, 11, 11, true);
314         list12 = list11;
315         delegateBooleanAssertion(types, 11, 12, true);
316         list11 = (List<String>[]) list12;
317         delegateBooleanAssertion(types, 12, 11, false);
318         list13 = list11;
319         delegateBooleanAssertion(types, 11, 13, true);
320         list11 = (List<String>[]) list13;
321         delegateBooleanAssertion(types, 13, 11, false);
322 //        list12 = list12;
323         delegateBooleanAssertion(types, 12, 12, true);
324         list13 = (List<? super String>[]) list12;
325         delegateBooleanAssertion(types, 12, 13, false);
326         list12 = (List<? extends String>[]) list13;
327         delegateBooleanAssertion(types, 13, 12, false);
328 //        list13 = list13;
329         delegateBooleanAssertion(types, 13, 13, true);
330         final Type disType = getClass().getField("dis").getGenericType();
331         // Reporter.log( ( ( ParameterizedType ) disType
332         // ).getOwnerType().getClass().toString() );
333         final Type datType = getClass().getField("dat").getGenericType();
334         final Type daType = getClass().getField("da").getGenericType();
335         final Type uhderType = getClass().getField("uhder").getGenericType();
336         final Type dingType = getClass().getField("ding").getGenericType();
337         final Type testerType = getClass().getField("tester").getGenericType();
338         final Type tester2Type = getClass().getField("tester2").getGenericType();
339         final Type dat2Type = getClass().getField("dat2").getGenericType();
340         final Type dat3Type = getClass().getField("dat3").getGenericType();
341         dis = dat;
342         Assert.assertTrue(TypeUtils.isAssignable(datType, disType));
343         // dis = da;
344         Assert.assertFalse(TypeUtils.isAssignable(daType, disType));
345         dis = uhder;
346         Assert.assertTrue(TypeUtils.isAssignable(uhderType, disType));
347         dis = ding;
348         Assert.assertFalse(String.format("type %s not assignable to %s!", dingType, disType),
349                 TypeUtils.isAssignable(dingType, disType));
350         dis = tester;
351         Assert.assertTrue(TypeUtils.isAssignable(testerType, disType));
352         // dis = tester2;
353         Assert.assertFalse(TypeUtils.isAssignable(tester2Type, disType));
354         // dat = dat2;
355         Assert.assertFalse(TypeUtils.isAssignable(dat2Type, datType));
356         // dat2 = dat;
357         Assert.assertFalse(TypeUtils.isAssignable(datType, dat2Type));
358         // dat = dat3;
359         Assert.assertFalse(TypeUtils.isAssignable(dat3Type, datType));
360         final char ch = 0;
361         final boolean bo = false;
362         final byte by = 0;
363         final short sh = 0;
364         int in = 0;
365         long lo = 0;
366         final float fl = 0;
367         double du = 0;
368         du = ch;
369         Assert.assertTrue(TypeUtils.isAssignable(char.class, double.class));
370         du = by;
371         Assert.assertTrue(TypeUtils.isAssignable(byte.class, double.class));
372         du = sh;
373         Assert.assertTrue(TypeUtils.isAssignable(short.class, double.class));
374         du = in;
375         Assert.assertTrue(TypeUtils.isAssignable(int.class, double.class));
376         du = lo;
377         Assert.assertTrue(TypeUtils.isAssignable(long.class, double.class));
378         du = fl;
379         Assert.assertTrue(TypeUtils.isAssignable(float.class, double.class));
380         lo = in;
381         Assert.assertTrue(TypeUtils.isAssignable(int.class, long.class));
382         lo = Integer.valueOf(0);
383         Assert.assertTrue(TypeUtils.isAssignable(Integer.class, long.class));
384         // Long lngW = 1;
385         Assert.assertFalse(TypeUtils.isAssignable(int.class, Long.class));
386         // lngW = Integer.valueOf( 0 );
387         Assert.assertFalse(TypeUtils.isAssignable(Integer.class, Long.class));
388         in = Integer.valueOf(0);
389         Assert.assertTrue(TypeUtils.isAssignable(Integer.class, int.class));
390         final Integer inte = in;
391         Assert.assertTrue(TypeUtils.isAssignable(int.class, Integer.class));
392         Assert.assertTrue(TypeUtils.isAssignable(int.class, Number.class));
393         Assert.assertTrue(TypeUtils.isAssignable(int.class, Object.class));
394         final Type intComparableType = getClass().getField("intComparable").getGenericType();
395         intComparable = 1;
396         Assert.assertTrue(TypeUtils.isAssignable(int.class, intComparableType));
397         Assert.assertTrue(TypeUtils.isAssignable(int.class, Comparable.class));
398         final Serializable ser = 1;
399         Assert.assertTrue(TypeUtils.isAssignable(int.class, Serializable.class));
400         final Type longComparableType = getClass().getField("longComparable").getGenericType();
401         // longComparable = 1;
402         Assert.assertFalse(TypeUtils.isAssignable(int.class, longComparableType));
403         // longComparable = Integer.valueOf( 0 );
404         Assert.assertFalse(TypeUtils.isAssignable(Integer.class, longComparableType));
405         // int[] ia;
406         // long[] la = ia;
407         Assert.assertFalse(TypeUtils.isAssignable(int[].class, long[].class));
408         final Integer[] ia = null;
409         final Type caType = getClass().getField("intWildcardComparable").getGenericType();
410         intWildcardComparable = ia;
411         Assert.assertTrue(TypeUtils.isAssignable(Integer[].class, caType));
412         // int[] ina = ia;
413         Assert.assertFalse(TypeUtils.isAssignable(Integer[].class, int[].class));
414         final int[] ina = null;
415         Object[] oa;
416         // oa = ina;
417         Assert.assertFalse(TypeUtils.isAssignable(int[].class, Object[].class));
418         oa = new Integer[0];
419         Assert.assertTrue(TypeUtils.isAssignable(Integer[].class, Object[].class));
420         final Type bClassType = AClass.class.getField("bClass").getGenericType();
421         final Type cClassType = AClass.class.getField("cClass").getGenericType();
422         final Type dClassType = AClass.class.getField("dClass").getGenericType();
423         final Type eClassType = AClass.class.getField("eClass").getGenericType();
424         final Type fClassType = AClass.class.getField("fClass").getGenericType();
425         final AClass aClass = new AClass(new AAClass<String>());
426         aClass.bClass = aClass.cClass;
427         Assert.assertTrue(TypeUtils.isAssignable(cClassType, bClassType));
428         aClass.bClass = aClass.dClass;
429         Assert.assertTrue(TypeUtils.isAssignable(dClassType, bClassType));
430         aClass.bClass = aClass.eClass;
431         Assert.assertTrue(TypeUtils.isAssignable(eClassType, bClassType));
432         aClass.bClass = aClass.fClass;
433         Assert.assertTrue(TypeUtils.isAssignable(fClassType, bClassType));
434         aClass.cClass = aClass.dClass;
435         Assert.assertTrue(TypeUtils.isAssignable(dClassType, cClassType));
436         aClass.cClass = aClass.eClass;
437         Assert.assertTrue(TypeUtils.isAssignable(eClassType, cClassType));
438         aClass.cClass = aClass.fClass;
439         Assert.assertTrue(TypeUtils.isAssignable(fClassType, cClassType));
440         aClass.dClass = aClass.eClass;
441         Assert.assertTrue(TypeUtils.isAssignable(eClassType, dClassType));
442         aClass.dClass = aClass.fClass;
443         Assert.assertTrue(TypeUtils.isAssignable(fClassType, dClassType));
444         aClass.eClass = aClass.fClass;
445         Assert.assertTrue(TypeUtils.isAssignable(fClassType, eClassType));
446     }
447 
448     public void delegateBooleanAssertion(final Type[] types, final int i2, final int i1, final boolean expected) {
449         final Type type1 = types[i1];
450         final Type type2 = types[i2];
451         final boolean isAssignable = TypeUtils.isAssignable(type2, type1);
452 
453         if (expected) {
454             Assert.assertTrue("[" + i1 + ", " + i2 + "]: From "
455                     + StringEscapeUtils.escapeHtml4(String.valueOf(type2)) + " to "
456                     + StringEscapeUtils.escapeHtml4(String.valueOf(type1)), isAssignable);
457         } else {
458             Assert.assertFalse("[" + i1 + ", " + i2 + "]: From "
459                     + StringEscapeUtils.escapeHtml4(String.valueOf(type2)) + " to "
460                     + StringEscapeUtils.escapeHtml4(String.valueOf(type1)), isAssignable);
461         }
462     }
463 
464     @SuppressWarnings("boxing") // boxing is deliberate here
465     @Test
466     public void testIsInstance() throws SecurityException, NoSuchFieldException {
467         final Type intComparableType = getClass().getField("intComparable").getGenericType();
468         final Type uriComparableType = getClass().getField("uriComparable").getGenericType();
469         intComparable = 1;
470         Assert.assertTrue(TypeUtils.isInstance(1, intComparableType));
471         // uriComparable = 1;
472         Assert.assertFalse(TypeUtils.isInstance(1, uriComparableType));
473     }
474 
475     @Test
476     public void testGetTypeArguments() {
477         Map<TypeVariable<?>, Type> typeVarAssigns;
478         TypeVariable<?> treeSetTypeVar;
479         Type typeArg;
480 
481         typeVarAssigns = TypeUtils.getTypeArguments(Integer.class, Comparable.class);
482         treeSetTypeVar = Comparable.class.getTypeParameters()[0];
483         Assert.assertTrue("Type var assigns for Comparable from Integer: " + typeVarAssigns,
484                 typeVarAssigns.containsKey(treeSetTypeVar));
485         typeArg = typeVarAssigns.get(treeSetTypeVar);
486         Assert.assertEquals("Type argument of Comparable from Integer: " + typeArg, Integer.class,
487                 typeVarAssigns.get(treeSetTypeVar));
488 
489         typeVarAssigns = TypeUtils.getTypeArguments(int.class, Comparable.class);
490         treeSetTypeVar = Comparable.class.getTypeParameters()[0];
491         Assert.assertTrue("Type var assigns for Comparable from int: " + typeVarAssigns,
492                 typeVarAssigns.containsKey(treeSetTypeVar));
493         typeArg = typeVarAssigns.get(treeSetTypeVar);
494         Assert.assertEquals("Type argument of Comparable from int: " + typeArg, Integer.class,
495                 typeVarAssigns.get(treeSetTypeVar));
496 
497         final Collection<Integer> col = Arrays.asList(new Integer[0]);
498         typeVarAssigns = TypeUtils.getTypeArguments(List.class, Collection.class);
499         treeSetTypeVar = Comparable.class.getTypeParameters()[0];
500         Assert.assertFalse("Type var assigns for Collection from List: " + typeVarAssigns,
501                 typeVarAssigns.containsKey(treeSetTypeVar));
502 
503         typeVarAssigns = TypeUtils.getTypeArguments(AAAClass.BBBClass.class, AAClass.BBClass.class);
504         Assert.assertTrue(typeVarAssigns.size() == 2);
505         Assert.assertEquals(String.class, typeVarAssigns.get(AAClass.class.getTypeParameters()[0]));
506         Assert.assertEquals(String.class, typeVarAssigns.get(AAClass.BBClass.class.getTypeParameters()[0]));
507 
508         typeVarAssigns = TypeUtils.getTypeArguments(Other.class, This.class);
509         Assert.assertEquals(2, typeVarAssigns.size());
510         Assert.assertEquals(String.class, typeVarAssigns.get(This.class.getTypeParameters()[0]));
511         Assert.assertEquals(Other.class.getTypeParameters()[0], typeVarAssigns.get(This.class.getTypeParameters()[1]));
512 
513         typeVarAssigns = TypeUtils.getTypeArguments(And.class, This.class);
514         Assert.assertEquals(2, typeVarAssigns.size());
515         Assert.assertEquals(Number.class, typeVarAssigns.get(This.class.getTypeParameters()[0]));
516         Assert.assertEquals(Number.class, typeVarAssigns.get(This.class.getTypeParameters()[1]));
517 
518         typeVarAssigns = TypeUtils.getTypeArguments(Thing.class, Other.class);
519         Assert.assertEquals(2, typeVarAssigns.size());
520         Assert.assertEquals(getClass().getTypeParameters()[0], typeVarAssigns.get(getClass().getTypeParameters()[0]));
521         Assert.assertEquals(getClass().getTypeParameters()[0], typeVarAssigns.get(Other.class.getTypeParameters()[0]));
522     }
523 
524     @Test
525     public void testTypesSatisfyVariables() throws SecurityException, NoSuchFieldException,
526             NoSuchMethodException {
527         final Map<TypeVariable<?>, Type> typeVarAssigns = new HashMap<TypeVariable<?>, Type>();
528         final Integer max = TypeUtilsTest.<Integer> stub();
529         typeVarAssigns.put(getClass().getMethod("stub").getTypeParameters()[0], Integer.class);
530         Assert.assertTrue(TypeUtils.typesSatisfyVariables(typeVarAssigns));
531         typeVarAssigns.clear();
532         typeVarAssigns.put(getClass().getMethod("stub2").getTypeParameters()[0], Integer.class);
533         Assert.assertTrue(TypeUtils.typesSatisfyVariables(typeVarAssigns));
534         typeVarAssigns.clear();
535         typeVarAssigns.put(getClass().getMethod("stub3").getTypeParameters()[0], Integer.class);
536         Assert.assertTrue(TypeUtils.typesSatisfyVariables(typeVarAssigns));
537     }
538 
539     @Test
540     public void testDetermineTypeVariableAssignments() throws SecurityException,
541             NoSuchFieldException, NoSuchMethodException {
542         final ParameterizedType iterableType = (ParameterizedType) getClass().getField("iterable")
543                 .getGenericType();
544         final Map<TypeVariable<?>, Type> typeVarAssigns = TypeUtils.determineTypeArguments(TreeSet.class,
545                 iterableType);
546         final TypeVariable<?> treeSetTypeVar = TreeSet.class.getTypeParameters()[0];
547         Assert.assertTrue(typeVarAssigns.containsKey(treeSetTypeVar));
548         Assert.assertEquals(iterableType.getActualTypeArguments()[0], typeVarAssigns
549                 .get(treeSetTypeVar));
550     }
551 
552     @Test
553     public void testGetRawType() throws SecurityException, NoSuchFieldException {
554         final Type stringParentFieldType = GenericTypeHolder.class.getDeclaredField("stringParent")
555                 .getGenericType();
556         final Type integerParentFieldType = GenericTypeHolder.class.getDeclaredField("integerParent")
557                 .getGenericType();
558         final Type foosFieldType = GenericTypeHolder.class.getDeclaredField("foos").getGenericType();
559         final Type genericParentT = GenericParent.class.getTypeParameters()[0];
560         Assert.assertEquals(GenericParent.class, TypeUtils.getRawType(stringParentFieldType, null));
561         Assert
562                 .assertEquals(GenericParent.class, TypeUtils.getRawType(integerParentFieldType,
563                         null));
564         Assert.assertEquals(List.class, TypeUtils.getRawType(foosFieldType, null));
565         Assert.assertEquals(String.class, TypeUtils.getRawType(genericParentT,
566                 StringParameterizedChild.class));
567         Assert.assertEquals(String.class, TypeUtils.getRawType(genericParentT,
568                 stringParentFieldType));
569         Assert.assertEquals(Foo.class, TypeUtils.getRawType(Iterable.class.getTypeParameters()[0],
570                 foosFieldType));
571         Assert.assertEquals(Foo.class, TypeUtils.getRawType(List.class.getTypeParameters()[0],
572                 foosFieldType));
573         Assert.assertNull(TypeUtils.getRawType(genericParentT, GenericParent.class));
574         Assert.assertEquals(GenericParent[].class, TypeUtils.getRawType(GenericTypeHolder.class
575                 .getDeclaredField("barParents").getGenericType(), null));
576     }
577 
578     @Test
579     public void testIsArrayTypeClasses() {
580         Assert.assertTrue(TypeUtils.isArrayType(boolean[].class));
581         Assert.assertTrue(TypeUtils.isArrayType(byte[].class));
582         Assert.assertTrue(TypeUtils.isArrayType(short[].class));
583         Assert.assertTrue(TypeUtils.isArrayType(int[].class));
584         Assert.assertTrue(TypeUtils.isArrayType(char[].class));
585         Assert.assertTrue(TypeUtils.isArrayType(long[].class));
586         Assert.assertTrue(TypeUtils.isArrayType(float[].class));
587         Assert.assertTrue(TypeUtils.isArrayType(double[].class));
588         Assert.assertTrue(TypeUtils.isArrayType(Object[].class));
589         Assert.assertTrue(TypeUtils.isArrayType(String[].class));
590 
591         Assert.assertFalse(TypeUtils.isArrayType(boolean.class));
592         Assert.assertFalse(TypeUtils.isArrayType(byte.class));
593         Assert.assertFalse(TypeUtils.isArrayType(short.class));
594         Assert.assertFalse(TypeUtils.isArrayType(int.class));
595         Assert.assertFalse(TypeUtils.isArrayType(char.class));
596         Assert.assertFalse(TypeUtils.isArrayType(long.class));
597         Assert.assertFalse(TypeUtils.isArrayType(float.class));
598         Assert.assertFalse(TypeUtils.isArrayType(double.class));
599         Assert.assertFalse(TypeUtils.isArrayType(Object.class));
600         Assert.assertFalse(TypeUtils.isArrayType(String.class));
601     }
602 
603     @Test
604     public void testIsArrayGenericTypes() throws Exception {
605         final Method method = getClass().getMethod("dummyMethod", List.class, List.class, List.class,
606                 List.class, List.class, List.class, List.class, List[].class, List[].class,
607                 List[].class, List[].class, List[].class, List[].class, List[].class);
608 
609         final Type[] types = method.getGenericParameterTypes();
610 
611         Assert.assertFalse(TypeUtils.isArrayType(types[0]));
612         Assert.assertFalse(TypeUtils.isArrayType(types[1]));
613         Assert.assertFalse(TypeUtils.isArrayType(types[2]));
614         Assert.assertFalse(TypeUtils.isArrayType(types[3]));
615         Assert.assertFalse(TypeUtils.isArrayType(types[4]));
616         Assert.assertFalse(TypeUtils.isArrayType(types[5]));
617         Assert.assertFalse(TypeUtils.isArrayType(types[6]));
618         Assert.assertTrue(TypeUtils.isArrayType(types[7]));
619         Assert.assertTrue(TypeUtils.isArrayType(types[8]));
620         Assert.assertTrue(TypeUtils.isArrayType(types[9]));
621         Assert.assertTrue(TypeUtils.isArrayType(types[10]));
622         Assert.assertTrue(TypeUtils.isArrayType(types[11]));
623         Assert.assertTrue(TypeUtils.isArrayType(types[12]));
624         Assert.assertTrue(TypeUtils.isArrayType(types[13]));
625     }
626 
627     @Test
628     public void testGetPrimitiveArrayComponentType() throws Exception {
629         Assert.assertEquals(boolean.class, TypeUtils.getArrayComponentType(boolean[].class));
630         Assert.assertEquals(byte.class, TypeUtils.getArrayComponentType(byte[].class));
631         Assert.assertEquals(short.class, TypeUtils.getArrayComponentType(short[].class));
632         Assert.assertEquals(int.class, TypeUtils.getArrayComponentType(int[].class));
633         Assert.assertEquals(char.class, TypeUtils.getArrayComponentType(char[].class));
634         Assert.assertEquals(long.class, TypeUtils.getArrayComponentType(long[].class));
635         Assert.assertEquals(float.class, TypeUtils.getArrayComponentType(float[].class));
636         Assert.assertEquals(double.class, TypeUtils.getArrayComponentType(double[].class));
637 
638         Assert.assertNull(TypeUtils.getArrayComponentType(boolean.class));
639         Assert.assertNull(TypeUtils.getArrayComponentType(byte.class));
640         Assert.assertNull(TypeUtils.getArrayComponentType(short.class));
641         Assert.assertNull(TypeUtils.getArrayComponentType(int.class));
642         Assert.assertNull(TypeUtils.getArrayComponentType(char.class));
643         Assert.assertNull(TypeUtils.getArrayComponentType(long.class));
644         Assert.assertNull(TypeUtils.getArrayComponentType(float.class));
645         Assert.assertNull(TypeUtils.getArrayComponentType(double.class));
646     }
647 
648     @Test
649     public void testGetArrayComponentType() throws Exception {
650         final Method method = getClass().getMethod("dummyMethod", List.class, List.class, List.class,
651                 List.class, List.class, List.class, List.class, List[].class, List[].class,
652                 List[].class, List[].class, List[].class, List[].class, List[].class);
653 
654         final Type[] types = method.getGenericParameterTypes();
655 
656         Assert.assertNull(TypeUtils.getArrayComponentType(types[0]));
657         Assert.assertNull(TypeUtils.getArrayComponentType(types[1]));
658         Assert.assertNull(TypeUtils.getArrayComponentType(types[2]));
659         Assert.assertNull(TypeUtils.getArrayComponentType(types[3]));
660         Assert.assertNull(TypeUtils.getArrayComponentType(types[4]));
661         Assert.assertNull(TypeUtils.getArrayComponentType(types[5]));
662         Assert.assertNull(TypeUtils.getArrayComponentType(types[6]));
663         Assert.assertEquals(types[0], TypeUtils.getArrayComponentType(types[7]));
664         Assert.assertEquals(types[1], TypeUtils.getArrayComponentType(types[8]));
665         Assert.assertEquals(types[2], TypeUtils.getArrayComponentType(types[9]));
666         Assert.assertEquals(types[3], TypeUtils.getArrayComponentType(types[10]));
667         Assert.assertEquals(types[4], TypeUtils.getArrayComponentType(types[11]));
668         Assert.assertEquals(types[5], TypeUtils.getArrayComponentType(types[12]));
669         Assert.assertEquals(types[6], TypeUtils.getArrayComponentType(types[13]));
670     }
671 
672     @Test
673     public void testLang820() throws Exception {
674         final Type[] typeArray = {String.class, String.class};
675         final Type[] expectedArray = {String.class};
676         Assert.assertArrayEquals(expectedArray, TypeUtils.normalizeUpperBounds(typeArray));
677     }
678 
679     @Test
680     public void testParameterize() throws Exception {
681         final ParameterizedType stringComparableType = TypeUtils.parameterize(Comparable.class, String.class);
682         Assert.assertTrue(TypeUtils.equals(getClass().getField("stringComparable").getGenericType(),
683             stringComparableType));
684         Assert.assertEquals("java.lang.Comparable<java.lang.String>", stringComparableType.toString());
685     }
686     
687     @Test
688     public void testParameterizeWithOwner() throws Exception {
689         final Type owner = TypeUtils.parameterize(TypeUtilsTest.class, String.class);
690         final ParameterizedType dat2Type = TypeUtils.parameterizeWithOwner(owner, That.class, String.class, String.class);
691         Assert.assertTrue(TypeUtils.equals(getClass().getField("dat2").getGenericType(), dat2Type));
692     }
693     
694     @Test
695     public void testWildcardType() throws Exception {
696         final WildcardType simpleWildcard = TypeUtils.wildcardType().withUpperBounds(String.class).build();
697         final Field cClass = AClass.class.getField("cClass");
698         Assert.assertTrue(TypeUtils.equals(((ParameterizedType) cClass.getGenericType()).getActualTypeArguments()[0],
699             simpleWildcard));
700         Assert.assertEquals(String.format("? extends %s", String.class.getName()), TypeUtils.toString(simpleWildcard));
701         Assert.assertEquals(String.format("? extends %s", String.class.getName()), simpleWildcard.toString());
702     }
703 
704     @Test
705     public void testUnboundedWildcardType() {
706         final WildcardType unbounded = TypeUtils.wildcardType().withLowerBounds((Type) null).withUpperBounds().build();
707         Assert.assertTrue(TypeUtils.equals(TypeUtils.WILDCARD_ALL, unbounded));
708         Assert.assertArrayEquals(new Type[] { Object.class }, TypeUtils.getImplicitUpperBounds(unbounded));
709         Assert.assertArrayEquals(new Type[] { null }, TypeUtils.getImplicitLowerBounds(unbounded));
710         Assert.assertEquals("?", TypeUtils.toString(unbounded));
711         Assert.assertEquals("?", unbounded.toString());
712     }
713 
714     @Test
715     public void testLowerBoundedWildcardType() {
716        final WildcardType lowerBounded = TypeUtils.wildcardType().withLowerBounds(java.sql.Date.class).build();
717        Assert.assertEquals(String.format("? super %s", java.sql.Date.class.getName()), TypeUtils.toString(lowerBounded));
718        Assert.assertEquals(String.format("? super %s", java.sql.Date.class.getName()), lowerBounded.toString());
719 
720        final TypeVariable<Class<Iterable>> iterableT0 = Iterable.class.getTypeParameters()[0];
721        final WildcardType lowerTypeVariable = TypeUtils.wildcardType().withLowerBounds(iterableT0).build();
722        Assert.assertEquals(String.format("? super %s", iterableT0.getName()), TypeUtils.toString(lowerTypeVariable));
723        Assert.assertEquals(String.format("? super %s", iterableT0.getName()), lowerTypeVariable.toString());
724     }
725 
726     @Test
727     public void testLang1114() throws Exception {
728         final Type nonWildcardType = getClass().getDeclaredField("wildcardComparable").getGenericType();
729         final Type wildcardType = ((ParameterizedType)nonWildcardType).getActualTypeArguments()[0];
730 
731         Assert.assertFalse(TypeUtils.equals(wildcardType, nonWildcardType));
732         Assert.assertFalse(TypeUtils.equals(nonWildcardType, wildcardType));
733     }
734 
735     @Test
736     public void testGenericArrayType() throws Exception {
737         final Type expected = getClass().getField("intWildcardComparable").getGenericType();
738         final GenericArrayType actual =
739             TypeUtils.genericArrayType(TypeUtils.parameterize(Comparable.class, TypeUtils.wildcardType()
740                 .withUpperBounds(Integer.class).build()));
741         Assert.assertTrue(TypeUtils.equals(expected, actual));
742         Assert.assertEquals("java.lang.Comparable<? extends java.lang.Integer>[]", actual.toString());
743     }
744 
745     @Test
746     public void testToLongString() {
747         Assert.assertEquals(getClass().getName() + ":B", TypeUtils.toLongString(getClass().getTypeParameters()[0]));
748     }
749 
750     @Test
751     public void testWrap() {
752         final Type t = getClass().getTypeParameters()[0];
753         Assert.assertTrue(TypeUtils.equals(t, TypeUtils.wrap(t).getType()));
754 
755         Assert.assertEquals(String.class, TypeUtils.wrap(String.class).getType());
756     }
757 
758     public Iterable<? extends Map<Integer, ? extends Collection<?>>> iterable;
759 
760     public static <G extends Comparable<G>> G stub() {
761         return null;
762     }
763 
764     public static <G extends Comparable<? super G>> G stub2() {
765         return null;
766     }
767 
768     public static <T extends Comparable<? extends T>> T stub3() {
769         return null;
770     }
771 }
772 
773 class AAClass<T> {
774 
775     public class BBClass<S> {
776     }
777 }
778 
779 class AAAClass extends AAClass<String> {
780     public class BBBClass extends BBClass<String> {
781     }
782 }
783 
784 @SuppressWarnings("rawtypes")
785 //raw types, where used, are used purposely
786 class AClass extends AAClass<String>.BBClass<Number> {
787 
788     public AClass(final AAClass<String> enclosingInstance) {
789         enclosingInstance.super();
790     }
791 
792     public class BClass<T> {
793     }
794 
795     public class CClass<T> extends BClass {
796     }
797 
798     public class DClass<T> extends CClass<T> {
799     }
800 
801     public class EClass<T> extends DClass {
802     }
803 
804     public class FClass extends EClass<String> {
805     }
806 
807     public class GClass<T extends BClass<? extends T> & AInterface<AInterface<? super T>>> {
808     }
809 
810     public BClass<Number> bClass;
811 
812     public CClass<? extends String> cClass;
813 
814     public DClass<String> dClass;
815 
816     public EClass<String> eClass;
817 
818     public FClass fClass;
819 
820     public GClass gClass;
821 
822     public interface AInterface<T> {
823     }
824 }