反射
使用反射可以直接獲取class
字節碼文件中的類型、屬性、方法。
演示代碼:
新建一個名爲User
的類作爲反射的操作對象
public class User {
private int id;
private String name;
private String password;
public User() {
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
}
}
演示
類
獲取類
Object u = new User();
Class class1 = u.getClass();
獲取類名
class1.getName()
獲取類訪問權限
int modifier = class1.getModifiers();
boolean flag = Modifier.isPublic(modifier);
System.out.println("是public?: "+flag);
屬性
獲取類所有屬性
訪問權限私有的也可以獲取到
Field[] arr = class1.getDeclaredFields();
for (Field field:arr) {
System.out.println ("類中的屬性:" + field);
}
獲取類所有屬性的值
私有屬性默認無法獲取,但是可以通過不檢查訪問權限來直接獲取。
// 獲取所有屬性的值
for (Field field:arr) {
// 不檢查訪問權限
field.setAccessible(true);
// 獲取u對象中field的值
Object o = field.get(u);
System.out.println("類中的屬性值:" +o);
}
指定屬性名獲取屬性
Field f = class1.getDeclaredField("name");
f.setAccessible(true);
f.set(u, "張三");
Object o = f.get(u);
System.out.println("name: " + o);
方法
獲取所有方法(包含從父類繼承的方法)
Method[] allMethods = class1.getMethods();
System.out.println("類的所有方法:");
for (Method method:allMethods) {
System.out.println(method);
}
獲取類自己的方法
Method[] onlyMethods = class1.getDeclaredMethods();
for (Method method:onlyMethods) {
System.out.println(method);
}
調用無參帶返回值方法
Method method2 = class1.getDeclaredMethod("getName");
String str = (String)(method2.invoke(u));
System.out.println ("返回值:"+ str);
調用有參無返回值方法
Method method = class1.getDeclaredMethod("setName", String.class);
method.invoke(u, "李四");
完整演示代碼:
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class ReflectTest {
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, NoSuchMethodException, InvocationTargetException {
Object u = new User();
// 獲取到User的類
Class class1 = u.getClass();
System.out.println("類名:"+class1.getName());
// 獲取類訪問權限
int modifier = class1.getModifiers();
boolean flag = Modifier.isPublic(modifier);
System.out.println("是public?: "+flag);
// 獲取所有屬性
Field[] arr = class1.getDeclaredFields();
for (Field field:arr) {
System.out.println ("類中的屬性:" + field);
}
// 獲取所有屬性的值
for (Field field:arr) {
// 不檢查訪問權限
field.setAccessible(true);
// 獲取u對象中field的值
Object o = field.get(u);
System.out.println("類中的屬性值:" +o);
}
// 指定字段名稱獲取屬性
Field f = class1.getDeclaredField("name");
f.setAccessible(true);
f.set(u, "張三");
Object o = f.get(u);
System.out.println("name: " + o);
// 獲取所有方法 (包含繼承方法)
Method[] allMethods = class1.getMethods();
System.out.println("類的所有方法:");
for (Method method:allMethods) {
System.out.println(method);
}
// 獲取當前類的自己的方法
Method[] onlyMethods = class1.getDeclaredMethods();
for (Method method:onlyMethods) {
System.out.println(method);
}
// 調用有參方法
Method method = class1.getDeclaredMethod("setName", String.class);
method.invoke(u, "李四");
// 調用無參有返回值方法
Method method2 = class1.getDeclaredMethod("getName");
String str = (String)(method2.invoke(u));
System.out.println ("返回值:"+ str);
}
}