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