目录
一、对象字段
在1.6API中有java.lang.Class类。类中有方法
1、获取所有的字段
Field[] | getFields() 返回一个包含某些 Field 对象的数组,这些对象反映此 Class 对象所表示的类或接口的所有可访问公共字段。 |
2、根据名称获取单个字段:
Field | getField(String name) 返回一个 Field 对象,它反映此 Class 对象所表示的类或接口的指定公共成员字段。 |
以上两个不能拿到私有private 修饰的变量。
要拿私有的使用以下两个方法:
Field[] | getDeclaredFields() 返回 Field 对象的一个数组,这些对象反映此 Class 对象所表示的类或接口所声明的所有字段。 |
Field | getDeclaredField(String name) 返回一个 Field 对象,该对象反映此 Class 对象所表示的类或接口的指定已声明字段。 |
实例:
public static void getFileDemo() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException {
//通过字节码获得对象
Class<?> clazz = Class.forName("com.study.reflection.Person");
Field field=null;//clazz.getField("age");只能拿公有的
field= clazz.getDeclaredField("age");//只能获取本类,可以拿私有的
System.out.println(field);//private int com.study.reflection.Person.age
//对私有字段的访问取消权限检查。暴力访问。
field.setAccessible(true);
Object obj = clazz.newInstance();
field.set(obj, 89);
Object o = field.get(obj);
System.out.println(o);//89
}
}
二、获取方法
1、获取指定Class中的公共函数
/**
* 获取指定Class中的公共函数
*/
private static void getMethodDemo() throws ClassNotFoundException {
Class<?> clazz = Class.forName("com.study.reflection.Person");
//获取本类、超类的所有公有方法
Method[] methods = clazz.getMethods();
//只获取本类中所有的方法,包含私有
methods = clazz.getDeclaredMethods();
for (Method method:methods){
System.out.println(method);
}
}
输出:
public java.lang.String com.study.reflection.Person.toString()
public void com.study.reflection.Person.show()
public void com.study.reflection.Person.paramMethod(java.lang.String,int)
private void com.study.reflection.Person.privateMethod()
public static void com.study.reflection.Person.staticMethod()Process finished with exit code 0
2、获取单个的空参方法
//获取单个的空参方法
public static void getMethodDemo_2() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> clazz = Class.forName("com.study.reflection.Person");
//获取空参数一般方法
Method method=clazz.getMethod("show",null);
//通过字节码对象获取到了指定的构造函数对象。
Constructor<?> constructor = clazz.getConstructor(String.class,int.class);
//通过该构造器对象的newInstance方法进行对象的初始化。
Object obj = constructor.newInstance("小王", 25);
method.invoke(obj,null);
}
输出:
Person param run...小王:25
小王...show run...25
Process finished with exit code 0
3、获取带有指定参数的方法
/**
* 获取带有指定参数的方法
* @throws ClassNotFoundException 类无法找到异常
* @throws NoSuchMethodException 如果找不到匹配的方法,或者方法名为 "<init>" 或 "<clinit>"
* @throws IllegalAccessException 但当前正在执行的方法无法访问指定类、字段、方法或构造方法的定义时
* @throws InstantiationException 指定的类对象无法被实例化时,抛出该异常 类对象表示一个抽象类、接口、数组类、基本类型、void 类没有非 null 构造方法
* @throws InvocationTargetException
*/
public static void getMethodDemo_3() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
//通过类名获得字节码对象
Class<?> clazz = Class.forName("com.study.reflection.Person");
//by clazz get param name is paramMethod param type is String and int Method
Method method = clazz.getMethod("paramMethod", String.class, int.class);
//通过字节码获得实例
Object obj = clazz.newInstance();
method.invoke(obj,"小王",28);
}
person run
paramMethod run.....小王:28Process finished with exit code 0