记录一下经常使用的反射API
构建一个实体类
package com.hsh.maoduo;
import lombok.Data;
@Data
public class Student {
private int id;
public Student() {
}
public Student(Integer id, String name) {
this.id = id;
this.name = name;
}
public String name;
private void study() {
System.out.println("学习中。。。");
}
public void play() {
System.out.println(name + "正在玩");
}
}
获取反射类(Class对象)
//指定类的类型获取反射对象
Class clazz = Student.class;
//根据对象获取反射对象
Class clazz2 = new Student().getClass();
//根据类的引用路径获取反射对象,找不到该类时
try {
Class clazz3 = Class.forName("com.hsh.maoduo.Student");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
获取方法
//获取本类以及父类或者父接口中所有的公共方法(public修饰符修饰的)c
Method[] methods = clazz.getMethods();
//获取本类中的所有方法,包括私有的(private、protected、默认以及public)的方法
Method[] declaredMethods = clazz.getDeclaredMethods();
//获取本类以及父类或者父接口中指定的方法(public修饰符修饰的)
try {
Method play = clazz.getMethod("play");
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
//获取本类中指定的方法(可以是 private、protected、默认以及public 修饰)
try {
Method study = clazz.getDeclaredMethod("study");
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
获取字段
//获取类的所有公共属性(public修饰符修饰的)
Field[] fields = clazz.getFields();
//获取类的所有属性(可以是 private、protected、默认以及public 修饰)
Field[] declaredFields = clazz.getDeclaredFields();
//允许操作私有属性,一般搭配getDeclaredField()和getDeclaredFields()使用
Arrays.stream(declaredFields ).forEach(v->v.setAccessible(true));
//获取类的指定属性(可以是 private、protected、默认以及public 修饰)
try {
Field field = clazz.getField("name");
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
//获取类的指公共属性(public修饰符修饰的)
try {
Field declaredField = clazz.getDeclaredField("id");
//允许操作私有属性,一般搭配getDeclaredField()和getDeclaredFields()使用
declaredField.setAccessible(true);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
获取反射类对象
//通过 Class 对象的 newInstance() 方法,结果相当于 new Student()
try {
Student student = (Student) clazz.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
//获得该类的无参构造器,通过构造器的newInstance()方法获取对象,结果相当于 new Student()
try {
Constructor constructor = clazz.getConstructor();
Student student = (Student) constructor.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
//获取指定的该类的有参构造器,通过构造器的newInstance()方法获取对象,相当于 new Student(1,"张三")
try {
Constructor constructor = clazz.getConstructor(Integer.class, String.class);
Student student = (Student) constructor.newInstance(1,"张三");
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}