Java 反射机制
基本反射
/**
* getClass 方法
*/
String name = "bi wen bo";
Class c = name.getClass();
System.out.println("name.getClass(): " + name.getClass() + " - " + "c.getName(): " + c.getName());
/**
* Class.forName
*/
String sub = "com.invoke.RealSubject";
Class c2 = null;
try {
c2 = Class.forName(sub);
System.out.println("c2.getName(): " + c2.getName() + " c2 父类: " + c2.getSuperclass());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
结果:
若传的字符串命名不合法,抛出异常:
获取基本类型
/**
* TYPE
*/
System.out.println("Integer: " + Integer.TYPE + " Byte: " + Byte.TYPE + " Boolean: " + Boolean.TYPE + " Float: " + Float.TYPE);
结果:
获取类的成员
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
/**
* java 反射
*
* @author bobo
* @since 2020/8/11 11:33
*/
public class Reflection {
private String name;
private String sex;
private int phone;
private int password;
public Reflection () {}
public Reflection (String name) {
this.name = name;
}
private Reflection (String name, String sex, int phone) {
this.name = name;
this.sex = sex;
this.phone = phone;
}
public Reflection (String name, String sex, int phone, int password) {
this.name = name;
this.sex = sex;
this.phone = phone;
this.password = password;
}
public static void main(String[] args) {
Reflection reflect = new Reflection();
Class ref = reflect.getClass();
Constructor[] constructors = ref.getDeclaredConstructors();
for (int i = 0; i < constructors.length; i ++) {
System.out.println(Modifier.toString(constructors[i].getModifiers()) + "参数: ");
Class[] types = constructors[i].getParameterTypes();
for (int j = 0; j < types.length; j ++) {
System.out.println(types[j].getName() + " ");
}
System.out.println("");
}
}
}
结果:
获取指定的构造方法
try {
Constructor singleConstructor = ref.getDeclaredConstructor();
System.out.println("对应构造方法类型: " + Modifier.toString(singleConstructor.getModifiers()) + " ");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
调用构造方法
打印构造方法:
private Reflection (String name, String sex, int phone) {
this.name = name;
this.sex = sex;
this.phone = phone;
System.out.println("name: " + name + " sex: " + sex + " phone : " + phone);
}
/**
* 调用构造方法
*/
Class[] p = {String.class, String.class, int.class};
Constructor newConstructors = ref.getDeclaredConstructor(p);
newConstructors.newInstance("bwb", "girl", 123456);
结果:
== 调用私有构造方法时,要设置constructors.setAccessible(true);==
调用私有构造方法:
/**
* 调用私有构造方法
*/
Class[] p1 = {String.class};
newConstructors = ref.getDeclaredConstructor(p1);
newConstructors.setAccessible(true);
newConstructors.newInstance("hei");
类的私有方法:
/**
* 私有方法
*
* @param tips
*/
private void welcome (String tips) {
System.out.println(tips);
}
调用类的私有方法:
/**
* 调用类的私有方法
* 首先通过 getDeclaredMethod方法获取到这个私有方法,第一个参数是方法名,第二个参数是参数类型
* 然后通过invoke方法执行,invoke需要两个参数一个是类的实例,一个是方法参数
*/
Class[] newp = {String.class};
Method method = ref.getDeclaredMethod("welcome", newp);
method.setAccessible(true);
Object arg[] = {"欢迎"};
method.invoke(reflect, arg);
结果:
获取类的私有字段并修改值
Reflection reflect = new Reflection();
Class ref = reflect.getClass();
/**
* 获取类的私有字段并修改值
*/
Field field = ref.getDeclaredField("name");
field.setAccessible(true);
field.set(reflect, "okk");
System.out.println(field.get(reflect).toString());