【Java 进阶】Java 反射

反射

反射:获取Class中所有字段(Field)与方法(Method),并实现调用(invoke)

Java 反射简单使用(获取Person类中Field与Method):

Person 类:

package com.jerry.reflect;
public class Person {
    private String mName = "LiuDongBing";
    private int mAge = 28;
   
    protected String getName() {
        LogUtils.d("getName :");
        return mName;
    }
    private void setName(String name) {
        LogUtils.d("setName :" + name);
        mName = name;
    }
    private int getAge() {
        return mAge;
    }
    public void setAge(int age) {
        mAge = age;
    }
}

反射方案:

//获取Person类
Class mPersonClass = Class.forName("com.jerry.reflec.Person");

//获取Person实例
Object mPersonObject = mPersonClass.newInstance();

//获取字段(Field)
Field mPersonField = mPersonClass.getDeclaredField("mNAme");
String mName = mPersonField.getName();

//获取方法(Method)
Method mPersonMethod01 = mPersonClass.getDeclaredMethod("getName");//获取无参数方法
Method mPersonMethod02 = mPersonClass.getDeclaredMethod("setName",String.class);//获取String参数的方法
//调用方法
String Name = mPersonMethod01.invoke(mPersonObject);//无参数
mPersonMethod02.invoke(mPersonObject,“JerryLiu”);//调用有参数方法,“JeryyLiu”即为传递参数

注意:

获取字段与方法有多种方案,仅以获取方法讲解,(字段Field类似)

//获取方法4种实现方案(只需使用一种):
Field mPersonMethod01 = personClass.getMethod("getNme");

Field mPersonMethod02 = personClass.getDeclaredMethod("setName",String.class);
 
//获取Person子类,父类,与其自己所有的public 属性的方法         
Field[] mPersonMethod03 = personClass.getMethods();

//获取Person类中所有字段
Field[] mPersonMethod04 = personClass.getDeclaredMethods();

//打印所有字段
for (int i = 0; i < mPersonField.length ; i++) {
    LogUtils.d("Field ["+i+"]="+mPersonField[i].getName());
}

Android 系统 Jar 包方法与字段使用@Hide注解,正常方案不能使用,附属使用android.jar 中@Hide属性获取代码:

 public static String getSystemProperties(String key) {
        Class<?> mSystemProperties;
        try {
            mSystemProperties = Class.forName("android.os.SystemProperties");
            Method method = mSystemProperties.getDeclaredMethod("get", String.class);
            return (String) method.invoke(mSystemProperties.newInstance(), key);
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        catch (InstantiationException e) {
            e.printStackTrace();
        }
        return "";
    }

参考文章

Android中反射的简单应用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,466评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • 你是贪官的话,你会把钱藏到哪里?总不会藏到别人家吧?那藏到自己家,你还能有多少个家?且是能经常住的家,毕竟不会找个...
    柯依生阅读 271评论 0 0
  • 几乎是每年,我们总会听到一些骇人听闻的新闻,都是那些学习压力、人际交往、与父母怄气的孩子,最后选择轻生的道路。 每...
    0e644b38f764阅读 237评论 0 0
  • 好纠结,现在的PHP集成环境都好丑呀 一个个都不想用,准备开发一个傻瓜式的PHP集成环境包 首先他要界面美观 他要...
    某人A阅读 265评论 0 0