just some code note

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class ReflectionUtils {
public Object newObjectByName(String className) throws Exception {
return Class.forName(className).newInstance();
}
public Class<?> getMemberListType(String listName, Class<?> from) throws Exception {
//Field listField = from.getDeclaredField(listName);
Field listField = getDeclaredFieldRecursive(listName, from);
ParameterizedType listType = (ParameterizedType) listField.getGenericType();
Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0];
return listClass;
}
public Field getDeclaredFieldRecursive(String fieldName, Class<?> from) {
Field field = null;
try {
field = from.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
field = getDeclaredFieldRecursive(fieldName,from.getSuperclass());
}
return field;
}
public Class<?> getMemberType(String memberName, Class<?> from) throws Exception {
Field field = from.getDeclaredField(memberName);
return field.getType();
}
public Object castObjectToType(Object o, String type) throws Exception {
Class<?> typeClass = Class.forName(type);
o = typeClass.cast(o);
return o;
}
public List<String> getGetterMethods(Class<?> from) throws Exception {
List<String> result = new ArrayList<String>();
BeanInfo info = Introspector.getBeanInfo(from);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
Method reader = pd.getReadMethod();
if (reader != null) {
String name = pd.getName();
if (!"class".equals(name)) {
result.add(name);
}
}
}
return result;
}
}