| 1 | |
package org.jtheque.utils; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
import java.lang.reflect.Array; |
| 20 | |
import java.lang.reflect.GenericArrayType; |
| 21 | |
import java.lang.reflect.GenericDeclaration; |
| 22 | |
import java.lang.reflect.ParameterizedType; |
| 23 | |
import java.lang.reflect.Type; |
| 24 | |
import java.lang.reflect.TypeVariable; |
| 25 | |
import java.util.ArrayList; |
| 26 | |
import java.util.Collection; |
| 27 | |
import java.util.HashMap; |
| 28 | |
import java.util.Map; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
public final class GenericsUtils { |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
private GenericsUtils() { |
| 41 | 0 | super(); |
| 42 | 0 | } |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public static <T> Collection<Class<?>> getTypeArguments(final Class<T> baseClass, |
| 54 | |
final Type childClass) { |
| 55 | 0 | Map<Type, Type> resolvedTypes = new HashMap<Type, Type>(10); |
| 56 | 0 | Type type = childClass; |
| 57 | |
|
| 58 | 0 | while (!getClass(type).equals(baseClass)) { |
| 59 | 0 | if (type instanceof Class) { |
| 60 | |
|
| 61 | |
|
| 62 | 0 | type = ((Class<?>) type).getGenericSuperclass(); |
| 63 | |
} else { |
| 64 | 0 | ParameterizedType parameterizedType = (ParameterizedType) type; |
| 65 | 0 | Class<?> rawType = (Class<?>) parameterizedType.getRawType(); |
| 66 | |
|
| 67 | 0 | Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); |
| 68 | 0 | TypeVariable<?>[] typeParameters = rawType.getTypeParameters(); |
| 69 | |
|
| 70 | 0 | for (int i = 0; i < actualTypeArguments.length; i++) { |
| 71 | 0 | resolvedTypes.put(typeParameters[i], actualTypeArguments[i]); |
| 72 | |
} |
| 73 | |
|
| 74 | 0 | if (!rawType.equals(baseClass)) { |
| 75 | 0 | type = rawType.getGenericSuperclass(); |
| 76 | |
} |
| 77 | 0 | } |
| 78 | |
} |
| 79 | |
|
| 80 | 0 | return determineRawClasses(resolvedTypes, type); |
| 81 | |
} |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
private static Collection<Class<?>> determineRawClasses(Map<Type, Type> resolvedTypes, Type type) { |
| 91 | 0 | Type[] actualTypeArguments = type instanceof Class ? ((GenericDeclaration) type).getTypeParameters() : ((ParameterizedType) type).getActualTypeArguments(); |
| 92 | |
|
| 93 | 0 | Collection<Class<?>> typeArgumentsAsClasses = new ArrayList<Class<?>>(actualTypeArguments.length); |
| 94 | |
|
| 95 | 0 | for (Type baseType : actualTypeArguments) { |
| 96 | 0 | while (resolvedTypes.containsKey(baseType)) { |
| 97 | 0 | baseType = resolvedTypes.get(baseType); |
| 98 | |
} |
| 99 | |
|
| 100 | 0 | typeArgumentsAsClasses.add(getClass(baseType)); |
| 101 | |
} |
| 102 | 0 | return typeArgumentsAsClasses; |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
private static Class<?> getClass(Type type) { |
| 113 | 0 | Class<?> typeClass = null; |
| 114 | |
|
| 115 | 0 | if (type instanceof Class) { |
| 116 | 0 | typeClass = (Class<?>) type; |
| 117 | 0 | } else if (type instanceof ParameterizedType) { |
| 118 | 0 | typeClass = getClass(((ParameterizedType) type).getRawType()); |
| 119 | 0 | } else if (type instanceof GenericArrayType) { |
| 120 | 0 | Type componentType = ((GenericArrayType) type).getGenericComponentType(); |
| 121 | 0 | Class<?> componentClass = getClass(componentType); |
| 122 | |
|
| 123 | 0 | if (componentClass != null) { |
| 124 | 0 | typeClass = Array.newInstance(componentClass, 0).getClass(); |
| 125 | |
} |
| 126 | |
} |
| 127 | |
|
| 128 | 0 | return typeClass; |
| 129 | |
} |
| 130 | |
} |