forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetFieldExampleTest.java
More file actions
110 lines (92 loc) · 3.25 KB
/
GetFieldExampleTest.java
File metadata and controls
110 lines (92 loc) · 3.25 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
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
package com.examplehub.basics.reflection;
import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import org.junit.jupiter.api.Test;
class Person {
public String name;
public Person(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String hi() {
return "hi";
}
}
class Student extends Person {
public int score;
private int grade;
public Student(String name) {
super(name);
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
class GetFieldExampleTest {
@Test
void testGetField() throws NoSuchFieldException {
Class<Student> stuCls = Student.class;
assertEquals("score", stuCls.getField("score").getName());
assertEquals("name", stuCls.getField("name").getName());
assertEquals("grade", stuCls.getDeclaredField("grade").getName());
}
@Test
void testGetFieldValue()
throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException,
NoSuchMethodException, InvocationTargetException, InstantiationException {
Class<?> cls = Person.class;
Constructor<?> constructor = cls.getConstructor(String.class);
Person person = (Person) constructor.newInstance("Jack");
Field field = cls.getDeclaredField("name");
assertEquals("Jack", field.get(person));
}
@Test
void testSetFiledValue()
throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException,
ClassNotFoundException, InvocationTargetException, InstantiationException {
Class<?> cls = Class.forName("com.examplehub.basics.reflection.Person");
Constructor<?> constructor = cls.getConstructor(String.class);
Object person = constructor.newInstance("Jack");
Field field = cls.getDeclaredField("name");
assertEquals("Jack", field.get(person).toString());
field.set(person, "Tom");
assertEquals("Tom", field.get(person).toString());
}
@Test
void testGetFunction() throws NoSuchMethodException, ClassNotFoundException {
Class<?> stuClass = Class.forName("com.examplehub.basics.reflection.Student");
assertEquals("getScore", stuClass.getDeclaredMethod("getScore").getName());
assertEquals("setScore", stuClass.getMethod("setScore", int.class).getName());
assertEquals("getGrade", stuClass.getDeclaredMethod("getGrade").getName());
assertEquals("setGrade", stuClass.getMethod("setGrade", int.class).getName());
assertEquals("getName", stuClass.getMethod("getName").getName());
assertEquals("setName", stuClass.getMethod("setName", String.class).getName());
}
@Test
void testGetAllFields() throws ClassNotFoundException {
Class<?> stuClass = Class.forName("com.examplehub.basics.reflection.Student");
Field[] fields = stuClass.getFields();
for (Field field : fields) {
System.out.println(field.getModifiers());
System.out.println(field.getType());
System.out.println(field.getName());
System.out.println("-------------");
}
}
}