To get details of the fields declared in a class, and details of annotations, reflection can be used. Using reflection, current values of the fields can be retrieved and new values can be set.
Content of TestMain.java
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
package com.t4b.test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class TestMain {
static Class<?> c;
public static void main(String[] args) {
try {
c = Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Fields:");
for (Field field : c.getDeclaredFields()) {
System.out.println(field.getName());
System.out.println(field.getType());
}
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.