forked from JavaOPs/basejava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainReflection.java
More file actions
34 lines (30 loc) · 1.26 KB
/
MainReflection.java
File metadata and controls
34 lines (30 loc) · 1.26 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
import model.Resume;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MainReflection {
public static void main(String[] args) {
Resume r = new Resume();
System.out.println("getting first declared field...");
Field field = r.getClass().getDeclaredFields()[0];
System.out.println("setting accessible true to the field");
field.setAccessible(true);
System.out.println("name of the field: " + field.getName());
try {
System.out.println("value of the field: " + field.get(r));
System.out.println("setting new value of the field...");
field.set(r, "new_uuid");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
// invoke r.toString via reflection
// System.out.println(r);
try {
System.out.println("getting method toString()...");
Method toStringMethod = r.getClass().getMethod("toString");
System.out.println("toStringMethod invoking: " + toStringMethod.invoke(r));
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}