Java反射机制

12 阅读4分钟

参考文章:

学会反射后,我被录取了(干货) (juejin.cn)

死磕java底层(三)—反射、动态代理和注解 (juejin.cn)

一.Java反射的四个组成部分

反射允许对成员变量,成员方法和构造方法的信息进行编程访问.

image.png

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.获取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();

2.获取Constructor构造方法

6102f9e125efd3abf2d2ffb3edf8771e.jpg

描述一个类的构造方法,内部包含了构造方法的所有信息,例如参数类型,参数名字,访问修饰符······

         //实例化对象的二种方式
        //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);
            //暴力反射:表示临时取消权限检查
            constructor.setAccessible(true);
            instance2 = (SmallPineapple) constructor.newInstance("小菠萝", 21);
            instance2.getInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }

3.获取Field 成员变量

  • 获取类中的变量(Field)

1de6454495000889cadce21d5a261ce7.jpg

Field[] getFields():只能获取类中所有被public修饰的所有变量

Field getField(String name):只能根据变量名获取类中的一个变量,该变量必须被public修饰

Field[] getDeclaredFields():获取类中所有的变量,但无法获取继承下来的变量

Field getDeclaredField(String name):根据姓名获取类中的某个变量,无法获取继承下来的变量

            //访问私有属性
            try {
                SmallPineapple instance = new SmallPineapple("小菠萝", 666,"54kg");
                Class clazz = instance.getClass();
                Field field = clazz.getDeclaredField("weight");
                //获取成员变量的名字 weight
                String name= field.getName();
                //获取成员变量的类型
                class<?> type= name.getType();
                //暴力反射:表示临时取消权限检查
                field.setAccessible(true);
                System.out.println("窥觑到小菠萝的体重是:" + field.get(instance));
            } catch (Exception e) {
                e.printStackTrace();
            }
  • 获取field对应的注解数组
 Annotation[] annotations = targetField.getAnnotations();
 

4.获取Method成员方法

6a66ceeec7ca8b87019d8ee8e2090e77.jpg

(1).获取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):获取类中所有方法的数组.


           //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();
            }

(2).根据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类型的注解
  

(3).通过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();
          }
         ......
     }

二. 反射的优缺点

增加了程序的灵活性,但是破坏了类的封装性,同时会带来性能的损耗。 利用反射操作对象时,编译器无法提前得知对象的类型,访问是否合法。参数传递类型是否匹配。 只有在程序运行时调用反射的代码时才会从头开始检查、调用、返回结果。

反射为什么慢?
  • 反射方法在执行时参数需要拆装箱,invoke方法的参数是Object[]类型,当方法参数是基本数据类型的时候,比如int需要转换成Integer,会额外生成Integer对象,然后还要将这些参数封装成Object数组。
method.invoke(instance, 1);//  1 会变成 Integer类型
  • 需要检查方法的可见性 反射是每次调用private方法时,都必须检查方法的可见性,setAccessible(true);如果是public 方法 不设置setAccessible,或者设置setAccessible(true/false)都可以访问。
  • 需要校验参数 反射时,调用方法时,必须检查每个实际参数与形式参数的类型是否匹配。
  • JIT(just in time)即时编译器无法优化 反射涉及到动态加载,无法进行优化。

三.反射应用

2.1.ButterKnife框架源码分析
2.2.框架原理之EventBus