参考文章:
死磕java底层(三)—反射、动态代理和注解 (juejin.cn)
1.Java反射的四个组成部分
package hk.fanshe;
public class SmallPineapple {
public String name;
public int age;
private String weight;
public SmallPineapple(){
}
public SmallPineapple(String name,int age){
this.age=age;
this.name=name;
}
public SmallPineapple(String name,int age,String weight){
this.age=age;
this.name=name;
this.weight=weight;
}
public void getInfo(){
System.out.println("["+name+"的年龄是"+age+"]");
}
private void getDetailInfo(String name,int age,String weight){
System.out.println("["+name+"的年龄是"+age+",体重是"+weight+"]");
}
}
1.1. class
任何运行在内存中的所有类都是该 Class 类的实例对象,记住一句话,通过反射干任何事,先找 Class 准没错。
//方式一
Class class1=null;
try {
class1=Class.forName("hk.fanshe.SmallPineapple");
} catch (Exception e) {
e.printStackTrace();
}
//方式二
Class class2=SmallPineapple.class;
//方式三
Class class3=new SmallPineapple().getClass();
1.2 Constructor
描述一个类的构造方法,内部包含了构造方法的所有信息,例如参数类型,参数名字,访问修饰符······
//实例化对象的二种方式
//Class 对象调用newInstance()方法
SmallPineapple instance1=null;
try {
instance1= (SmallPineapple) class1.newInstance();//newInstance() 构造实例会调用默认无参构造器。
} catch (Exception e) {
e.printStackTrace();
}
instance1.getInfo();
//Constructor 构造器调用newInstance()方法
Constructor constructor = null;
SmallPineapple instance2 = null;
try {
constructor = class2.getConstructor(String.class, int.class);
instance2 = (SmallPineapple) constructor.newInstance("小菠萝", 21);
constructor.setAccessible(true);
instance2.getInfo();
} catch (Exception e) {
e.printStackTrace();
}
1.3 Field
描述一个类的属性,内部包含了该属性的所有信息,例如数据类型,属性名,访问修饰符······
1.3.1 获取类中的变量(Field)
Field[] getFields():只能获取类中所有被public修饰的所有变量
Field getField(String name):只能根据变量名获取类中的一个变量,该变量必须被public修饰
Field[] getDeclaredFields():获取类中所有的变量,但无法获取继承下来的变量
Field getDeclaredField(String name):根据姓名获取类中的某个变量,无法获取继承下来的变量
//通过反射访问私有属性
try {
SmallPineapple instance = new SmallPineapple("小菠萝", 666,"54kg");
Class clazz = sp.getClass();
Field field = clazz.getDeclaredField("weight");
field.setAccessible(true);
System.out.println("窥觑到小菠萝的体重是:" + field.get(instance));
} catch (Exception e) {
e.printStackTrace();
}
1.3.2.根据Field获取对应的值和注解数组
- 获取field对应的值
Field field = clazz.getDeclaredField("weight");
field.setAccessible(true);
field.get(instance);
- 获取field对应的注解数组
Annotation[] annotations = targetField.getAnnotations();
1.4 Method
描述一个类的所有方法(包括抽象方法),内部包含了该方法的所有信息,与Constructor类似,不同之处是 Method 拥有返回值类型信息,因为构造方法是没有返回值的。
Method getMethod(String name, Class<?>... parameterTypes):只能获取类中public对应名字的方法
Method[] getMethods(String name, Class<?>... parameterTypes):只能获取类中public对应名字的方法数组
Method getDeclaredMethod(String name, Class<?>... parameterTypes):获取类中对应名字的方法.【private,public,protect均可以】
Method[] getDeclaredMethods(String name, Class<?>... parameterTypes):获取类中对应名字的方法数组. 【private,public,protect】
//1无参数的public方法
Method method = null;
try {
method = class3.getMethod("getInfo");
if (method != null) { //method.invoke(class3.getConstructor(String.class,int.class,String.class).newInstance("小菠萝",18,"48kg"),null);
method.invoke(instance2,null);
}
} catch (Exception e) {
e.printStackTrace();
}
//2有参数的private方法
Method method1 = null;
try {
method1 = class3.getDeclaredMethod("getDetailInfo",String.class,int.class,String.class);
method1.setAccessible(true);
if (method1 != null) {
//method.invoke(class3.getConstructor(String.class,int.class,String.class).newInstance("小菠萝",18,"48kg"),null);
method1.invoke(instance2,"大菠萝",36,"48kg");
}
} catch (Exception e) {
e.printStackTrace();
}
1.5.Method获取方法注解数组,方法参数注解二维数组,方法参数类型数组
- Anomation[] 方法注解数组
Annotation[] methodAnnotations = method.getAnnotations();
- Type[] 方法参数类型数组
Type[] types=method.getGenericParameterTypes();
- Anomation[][] 方法参数注解二维数组
Annotation[][] parameterAnnotationsArray = method.getParameterAnnotations();
示例:
@GET("app/consultationBill/list")
Observable<BMHttpResult<BMConsultationBillBean>>
bm_getConsultationBillList(@Header("Authorization") String token,
@Query("pageNum") int pageNum,
@Query("isAsc") String isAsc );
//获取方法的注解数组 [Header,Header,Header,Query,Query,Query,Query,Query]
Annotation[] methodAnnotations = method.getAnnotations();
//获取参数类型数组[String,int,String]
Type[] parameterTypes = method.getGenericParameterTypes();
//获取参数注解 二维数组 [][]
Annotation[][] parameterAnnotationsArray = method.getParameterAnnotations();
parameterAnnotationsArray[0]=[Header] ;//得到一个Header类型的注解
parameterAnnotationsArray[1]=[Query]; //得到一个Query类型的注解
parameterAnnotationsArray[2]=[Query]; //得到一个Query类型的注解
1.7.通过Annotation得到注解的值
private void parseMethodAnnotation(Annotation annotation) {
String value;
if (annotation instanceof POST) {
value= ((POST) annotation).value();
}else if(annotation instanceof Header){
value= ((Header) annotation).value();
}else if(annotation instanceof Query){
value= ((Query) annotation).value();
}else if(annotation instanceof Filed){
value=((Field) annotation).value();
}
......
}
1.8 反射的优缺点
增加了程序的灵活性,但是破坏了类的封装性,同时会带来性能的损耗。 利用反射操作对象时,编译器无法提前得知对象的类型,访问是否合法。参数传递类型是否匹配。 只有在程序运行时调用反射的代码时才会从头开始检查、调用、返回结果。
反射为什么慢?
- 反射方法在执行时参数需要拆装箱,invoke方法的参数是Object[]类型,当方法参数是基本数据类型的时候,比如int需要转换成Integer,会额外生成Integer对象,然后还要将这些参数封装成Object数组。
method.invoke(instance, 1);// 1 会变成 Integer类型
- 需要检查方法的可见性 反射是每次调用private方法时,都必须检查方法的可见性,setAccessible(true);如果是public 方法 不设置setAccessible,或者设置setAccessible(true/false)都可以访问。
- 需要校验参数 反射时,调用方法时,必须检查每个实际参数与形式参数的类型是否匹配。
- JIT(just in time)即时编译器无法优化 反射涉及到动态加载,无法进行优化。