1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.utils; |
20 | |
|
21 | |
import java.lang.reflect.Array; |
22 | |
import java.lang.reflect.Field; |
23 | |
import java.lang.reflect.GenericArrayType; |
24 | |
import java.lang.reflect.ParameterizedType; |
25 | |
import java.lang.reflect.Type; |
26 | |
import java.lang.reflect.TypeVariable; |
27 | |
|
28 | |
import java.util.ArrayList; |
29 | |
import java.util.HashMap; |
30 | |
import java.util.List; |
31 | |
import java.util.Map; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class ReflectionUtils { |
39 | |
|
40 | |
|
41 | |
|
42 | 0 | private ReflectionUtils() { } |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public static Class<?> getClass(Type type) { |
52 | 424 | if (type instanceof Class) { |
53 | 296 | return (Class<?>) type; |
54 | 128 | } else if (type instanceof ParameterizedType) { |
55 | 101 | return getClass(((ParameterizedType) type).getRawType()); |
56 | 27 | } else if (type instanceof GenericArrayType) { |
57 | 0 | Type componentType = |
58 | |
((GenericArrayType) type).getGenericComponentType(); |
59 | 0 | Class<?> componentClass = getClass(componentType); |
60 | 0 | if (componentClass != null) { |
61 | 0 | return Array.newInstance(componentClass, 0).getClass(); |
62 | |
} else { |
63 | 0 | return null; |
64 | |
} |
65 | |
} else { |
66 | 27 | return null; |
67 | |
} |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public static <T> List<Class<?>> getTypeArguments( |
80 | |
Class<T> baseClass, Class<? extends T> childClass) { |
81 | 44 | Map<Type, Type> resolvedTypes = new HashMap<Type, Type>(); |
82 | 44 | Type type = childClass; |
83 | |
|
84 | 152 | while (! getClass(type).equals(baseClass)) { |
85 | 108 | if (type instanceof Class) { |
86 | |
|
87 | |
|
88 | 46 | type = ((Class<?>) type).getGenericSuperclass(); |
89 | |
} else { |
90 | 62 | ParameterizedType parameterizedType = (ParameterizedType) type; |
91 | 62 | Class<?> rawType = (Class<?>) parameterizedType.getRawType(); |
92 | |
|
93 | 62 | Type[] actualTypeArguments = |
94 | |
parameterizedType.getActualTypeArguments(); |
95 | 62 | TypeVariable<?>[] typeParameters = rawType.getTypeParameters(); |
96 | 307 | for (int i = 0; i < actualTypeArguments.length; i++) { |
97 | 245 | resolvedTypes.put(typeParameters[i], |
98 | |
actualTypeArguments[i]); |
99 | |
} |
100 | |
|
101 | 62 | if (!rawType.equals(baseClass)) { |
102 | 62 | type = rawType.getGenericSuperclass(); |
103 | |
} |
104 | 62 | } |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
Type[] actualTypeArguments; |
111 | 44 | if (type instanceof Class) { |
112 | 5 | actualTypeArguments = ((Class<?>) type).getTypeParameters(); |
113 | |
} else { |
114 | 39 | actualTypeArguments = |
115 | |
((ParameterizedType) type).getActualTypeArguments(); |
116 | |
} |
117 | 44 | List<Class<?>> typeArgumentsAsClasses = new ArrayList<Class<?>>(); |
118 | |
|
119 | 215 | for (Type baseType: actualTypeArguments) { |
120 | 416 | while (resolvedTypes.containsKey(baseType)) { |
121 | 245 | baseType = resolvedTypes.get(baseType); |
122 | |
} |
123 | 171 | typeArgumentsAsClasses.add(getClass(baseType)); |
124 | |
} |
125 | 44 | return typeArgumentsAsClasses; |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public static void setField(Object target, String fieldname, Object value) |
136 | |
throws NoSuchFieldException, IllegalAccessException { |
137 | 16 | Field field = findDeclaredField(target.getClass(), fieldname); |
138 | 16 | field.setAccessible(true); |
139 | 16 | field.set(target, value); |
140 | 16 | } |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
private static Field findDeclaredField(Class<?> inClass, String fieldname) |
151 | |
throws NoSuchFieldException { |
152 | 64 | while (!Object.class.equals(inClass)) { |
153 | 168 | for (Field field : inClass.getDeclaredFields()) { |
154 | 120 | if (field.getName().equalsIgnoreCase(fieldname)) { |
155 | 16 | return field; |
156 | |
} |
157 | |
} |
158 | 48 | inClass = inClass.getSuperclass(); |
159 | |
} |
160 | 0 | throw new NoSuchFieldException(); |
161 | |
} |
162 | |
} |