-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjava反射.java
More file actions
executable file
·72 lines (61 loc) · 2.72 KB
/
Copy pathjava反射.java
File metadata and controls
executable file
·72 lines (61 loc) · 2.72 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) throws Exception {
Student stu = new Student();
stu.setId(10);
stu.setAge(30);
stu.setEmail("simtice");
stu.setName("xia");
stu.setSex("nan");
Field fields[] = stu.getClass().getDeclaredFields();
final Test test = new Test();
for (Field field : fields) {
System.out.println(field.getName());
if (field.getGenericType().toString().equals("class java.lang.String")) { // 如果type是类类型,则前面包含"class ",后面跟类名
// 拿到该属性的gettet方法
/**
* 这里需要说明一下:他是根据拼凑的字符来找你写的getter方法的
* 在Boolean值的时候是isXXX(默认使用ide生成getter的都是isXXX)
* 如果出现NoSuchMethod异常 就说明它找不到那个gettet方法 需要做个规范
*/
Method m = (Method) stu.getClass().getMethod("get" + test.getMethodName(field.getName()));
String val = (String) m.invoke(stu);// 调用getter方法获取属性值
if (val != null) {
System.out.println("String type:" + val);
}
}
// Method[] methods = stu.getClass().getDeclaredMethods();
// for (Method m : methods) {
// System.out.println(m.getName());
// Integer val = (Integer) m.invoke(stu);
// System.out.println(val);
// }
}
}
private static String getMethodName(String fildeName) throws Exception{
byte[] items = fildeName.getBytes();
items[0] = (byte) ((char) items[0] - 'a' + 'A');
return new String(items);
}
}
//通过反射获取构造方法初始化子类对象
CellAppEnter cellAppEnter = ndAppObjects.get(appCoordinate.getCellViewClassName());
if (cellAppEnter == null) {
Class<?> clazz = null;
try {
clazz = Class.forName(appCoordinate.getCellViewClassName());
} catch (Exception e) {
e.printStackTrace();
}
if (clazz == null) {
clazz = RootNDView.class;
}
Constructor<?> constructor = clazz.getConstructor(new Class[] { Context.class, getClass() });
cellAppEnter = (CellAppEnter) constructor.newInstance(new Object[] { mLauncher, this });
ndAppObjects.put(appCoordinate.getCellViewClassName(), cellAppEnter);
} else if (cellAppEnter instanceof WeatherView) {
WeatherView weatherView = (WeatherView) cellAppEnter;
weatherView.onDestroy();
}
RootNDView继承了CellAppEnter