Java反射机制详解

2020-05-25  本文已影响0人  晚路歌

什么是反射机制

反射机制可以帮助我们在动态运行的时候,可以获得其所有的方法(包括 public protected private 默认状态的),所有的变量 (包括 public protected private 默认状态的)。

获取构造方法(Constructor,Declared)

         logConstructor("com.breeze.diy.recyclermuster.adapter.LineAdapter");
    //获取所有构造方法
    public void logConstructor(String className) {
        try {
            Class<?> aClass = Class.forName(className);
            Log.d("Constructor", "aClass: " + aClass);
            //获取所有的public权限构造方法
            Constructor<?>[] constructors = aClass.getConstructors();
            Log.d("Constructor", "getConstructors: " + Arrays.toString(constructors));
            //获取所有的构造方法,包括public,private,protected,默认权限的
            Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
            Log.d("Constructor", "getDeclaredConstructors: " + Arrays.toString(declaredConstructors));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
      getConstructor("com.breeze.diy.recyclermuster.adapter.LineAdapter", LineAdapter.OnItemClickListener.class);
    //获取指定的构造方法
    public void getConstructor(String className, Class<?>... cl) {
        try {
            Class<?> aClass = Class.forName(className);
            Log.d("Constructor", "aClass: " + aClass);
            //获取指定的public权限构造函数
            Constructor<?> constructor = aClass.getConstructor(cl);
            Log.d("Constructor", "getConstructor: " + constructor);
            //获取指定的构造函数,包括public,private,protected,默认权限的
            Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(cl);
            declaredConstructor.setAccessible(true);
            Log.d("Constructor", "getDeclaredConstructor: " + declaredConstructor);
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

注意事项

如果该方法,或者该变量不是 public 访问权限的,我们应该调用相应的 setAccessible(true) 方法,才能访问得到

//if Constructor is not public,you should call this
declaredConstructor.setAccessible(true);

获取成员变量(Field,Declared)

logConstructor("com.breeze.diy.recyclermuster.adapter.LineAdapter");
    //获取所有成员变量
    public void logConstructor(String className) {
        try {
            Class<?> aClass = Class.forName(className);
            Log.d("Constructor", "aClass: " + aClass);
 
            Field[] fields = aClass.getFields();
            Log.d("Constructor", "getConstructors: " + Arrays.toString(fields));

            Field[] declaredFields = aClass.getDeclaredFields();
            Log.d("Constructor", "getDeclaredConstructors: " + Arrays.toString(declaredFields));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
getConstructor("com.breeze.diy.recyclermuster.adapter.LineAdapter", "mStrings");
    //获取指定成员变量
      public void getConstructor(String className, String s) {
        try {
            Class<?> aClass = Class.forName(className);
            Log.d("Constructor", "aClass: " + aClass);

            Field field = aClass.getField(s);
            Log.d("Constructor", "getConstructor: " + field);

            Field declaredField = aClass.getDeclaredField(s);
            Log.d("Constructor", "getDeclaredConstructor: " + declaredField);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

获取方法(Method,Declared)

        //Person 为className的类
        Person person=new Person();
        Method method = ReflectHelper.getMethod(CLASS_NAME,
                "setCountry", String.class);
        try {
           // 执行方法,结果保存在 person 中
            Object o = method.invoke(person, CHINA);
           // 拿到我们传递进取的参数 country 的值 China          
            String country=person.country;
            PrintUtils.print(country);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

上一篇下一篇

猜你喜欢

热点阅读