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.Modifier;
22
23 import org.apache.giraph.conf.ContextSettable;
24 import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
25 import org.apache.hadoop.mapreduce.Mapper;
26 import org.jodah.typetools.TypeResolver;
27
28
29
30
31
32
33 public class ReflectionUtils {
34
35
36
37 private ReflectionUtils() { }
38
39
40
41
42
43
44
45 public static String getPackagePath(Object object) {
46 return getPackagePath(object.getClass());
47 }
48
49
50
51
52
53
54
55 public static String getPackagePath(Class klass) {
56 return klass.getPackage().getName().replaceAll("\\.", "/");
57 }
58
59
60
61
62
63
64
65
66
67
68 public static <T> Class<?>[] getTypeArguments(
69 Class<T> baseClass, Class<? extends T> childClass) {
70 return TypeResolver.resolveArguments(childClass, baseClass);
71 }
72
73
74
75
76
77
78
79
80 @SuppressWarnings("unchecked")
81 public static <T> T newInstance(Class<T> theClass) {
82 try {
83 return theClass.newInstance();
84 } catch (InstantiationException e) {
85 throw new IllegalStateException(
86 "newInstance: Couldn't instantiate " + theClass.getName(), e);
87 } catch (IllegalAccessException e) {
88 throw new IllegalStateException(
89 "newInstance: Illegal access " + theClass.getName(), e);
90 }
91 }
92
93
94
95
96
97
98
99
100
101 @SuppressWarnings("unchecked")
102 public static <T> T newInstance(
103 Class<T> theClass,
104 ImmutableClassesGiraphConfiguration configuration) {
105 T result;
106 try {
107 result = theClass.newInstance();
108 } catch (InstantiationException e) {
109 throw new IllegalStateException(
110 "newInstance: Couldn't instantiate " + theClass.getName(), e);
111 } catch (IllegalAccessException e) {
112 throw new IllegalStateException(
113 "newInstance: Illegal access " + theClass.getName(), e);
114 }
115 ConfigurationUtils.configureIfPossible(result, configuration);
116 return result;
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130 public static <T> T newInstance(
131 Class<T> theClass,
132 ImmutableClassesGiraphConfiguration configuration,
133 Mapper<?, ?, ?, ?>.Context context) {
134 T result = newInstance(theClass, configuration);
135 if (result instanceof ContextSettable) {
136 ((ContextSettable) result).setContext(context);
137 }
138 return result;
139 }
140
141
142
143
144
145
146
147
148
149
150
151 public static void verifyTypes(Class<?> concreteChild, Class<?> parent,
152 String typeDesc, Class<?> mainClass) {
153
154 if (parent == TypeResolver.Unknown.class) {
155 parent = Object.class;
156 }
157
158 verifyConcrete(concreteChild, typeDesc);
159
160 if (!parent.isAssignableFrom(concreteChild)) {
161 throw new IllegalStateException("verifyTypes: " + typeDesc + " types " +
162 "don't match, in " + mainClass.getName() + " " + concreteChild +
163 " expected, but " + parent + " found");
164 }
165 }
166
167
168
169
170
171
172
173 public static void verifyConcrete(
174 Class<?> concrete, String typeDesc) {
175 if (concrete.isInterface()) {
176 throw new IllegalStateException("verifyTypes: " +
177 "Type " + typeDesc + " must be concrete class " + concrete);
178 }
179 if (Modifier.isAbstract(concrete.getModifiers())) {
180 throw new IllegalStateException("verifyTypes: " +
181 "Type " + typeDesc + "can't be abstract class" + concrete);
182 }
183 }
184 }