大数据学习 - 反射

实体类

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
@Data
public class Person implements Serializable {

private static final long serialVersionUID = 5772917160911675752L;

public Person(String name, Integer age){
this.name = name;
this.age = age;
}

private Person(String name){
this.name = name;
}

public Person(){

}

private String name;

private Integer age;

public String phone;

public void toStringCustom(){
System.out.println(name);
System.out.println(age);
System.out.println(phone);
}

}

常用反射方法Demo

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
public class ReflectTest {

private String className = null;

private Class personClass = null;

@Before
public void init() throws ClassNotFoundException {
className = "com.jinzl.reflect.Person";
personClass = Class.forName(className);
}

@Test
public void getClassName(){
System.out.println(personClass);
// 获取某个class文件对象的另一种方式
System.out.println(Person.class);
}

/**
* 创建一个class文件表示的实例对象, 底层会调用无参构造方法
*/
@Test
public void getNewInstance() throws IllegalAccessException, InstantiationException {
System.out.println(personClass.newInstance());
}

/**
* 反射调用公有有参构造方法
*/
@Test
public void getPublicConstructorTest() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Constructor constructor = personClass.getConstructor(String.class, Integer.class);
Person person = (Person) constructor.newInstance("jinzili", 18);
System.out.println(person);
}

/**
* 反射调用私有有参构造方法
*/
@Test
public void getPrivateConstructorTest() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Constructor constructor = personClass.getDeclaredConstructor(String.class);
constructor.setAccessible(true);
Person person = (Person) constructor.newInstance("jinzili");
System.out.println(person);
}

/**
* 访问非私有的成员变量
*/
@Test
public void getPublicField() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
Constructor constructor = personClass.getConstructor(String.class, Integer.class);
Person person = (Person) constructor.newInstance("jinzili", 18);

Field field = personClass.getField("phone");
field.set(person, "18721317074");
System.out.println(person);
}

/**
* 访问私有的成员变量
*/
@Test
public void getPrivateField() throws Exception {
Constructor constructor = personClass.getConstructor(String.class, Integer.class);
Person person = (Person) constructor.newInstance("jinzili", 18);

Field field = personClass.getDeclaredField("name");
field.setAccessible(true);
field.set(person, "jinzili");
System.out.println(person);
}

/**
* 获取非私有的成员函数
*/
@Test
public void getPublicMethod() throws Exception{
Object obj = personClass.newInstance();
Object object = personClass.getMethod("toStringCustom").invoke(obj);
System.out.println(object);
}

/**
* 获取私有的成员函数
*/
@Test
public void getPrivateMethod() throws Exception{
Constructor constructor = personClass.getConstructor(String.class, Integer.class);
Object obj = constructor.newInstance("jinzili", 18);
Method method = personClass.getDeclaredMethod("getName");
method.setAccessible(true);
Object value = method.invoke(obj);
System.out.println(value);
}

/**
* 其他方法
*/
@Test
public void getOtherMethod() throws Exception{
// 当前加载这个class文件的哪个类加载器对象
System.out.println(personClass.getClassLoader());
// 获取某个类实现的所有接口
Class[] interfaces = personClass.getInterfaces();
for(Class _interface : interfaces){
System.out.println(_interface);
}
// 反射当前这个类的直接父类
System.out.println(personClass.getGenericSuperclass());
// 判断当前的Class对象表示是否是数组
System.out.println(personClass.isArray());
System.out.println(new String[3].getClass().isArray());
// 判断当前的Class对象表示是否是枚举
System.out.println(personClass.isEnum());
// 判断当前的Class对象表示是否是接口
System.out.println(personClass.isInterface());
}

}
评论