java反射

来源:互联网 发布:模拟投资基金软件 编辑:程序博客网 时间:2024/06/09 14:30

什么是反射

 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制。

反射基本用法

1.获取运行时任何对象所属类

   Class class1 = null;        Class class2 = null;        Class class3 = null;        try {            //获取Class对象最常用方法            class1 = Class.forName("com.arvin.androidpluginbase.TestBean");        } catch (ClassNotFoundException e) {            e.printStackTrace();        }        class2 = new TestBean().getClass();        class3 = TestBean.class;        System.out.println(class1.getName());        System.out.println(class2.getName());        System.out.println(class3.getName());打印结果:com.arvin.androidpluginbase.TestBeancom.arvin.androidpluginbase.TestBeancom.arvin.androidpluginbase.TestBean

2. 获取类的构造器

   Class class1= TestBean.class;        // 第二种方法 取得全部的构造函数 使用构造函数赋值        Constructor cons[] = class1.getConstructors();        // 查看每个构造方法需要的参数        for (int i = 0; i < cons.length; i++) {            Class clazzs[] = cons[i].getParameterTypes();//h获取当前构造器的构造参数            System.out.print(class1.getName()+" (");            for (int j = 0; j < clazzs.length; j++) {                if (j == clazzs.length - 1)                    System.out.print(clazzs[j].getName());                else                    System.out.print(clazzs[j].getName() + ",");            }            System.out.println(")");        }打印结果:com.arvin.androidpluginbase.TestBean ()com.arvin.androidpluginbase.TestBean (java.lang.String,java.lang.String)com.arvin.androidpluginbase.TestBean (java.lang.String,java.lang.String,java.lang.Object)

3.获取方法

获取方法  Class class1 = TestBean.class;        Method[] methods = class1.getDeclaredMethods();        for(Method method :methods){            System.out.println(method.toString());        }    //调用该方法        Method method = null;        try {           Object o = class1.newInstance();           method = class1.getMethod("testMethod",  new Class[]{}); //第二个参数是调用的这个方法参数类型           method.invoke(o,null);//调用方法的需要设置的参数值        } catch (NoSuchMethodException e) {            e.printStackTrace();        } catch (InvocationTargetException e) {            e.printStackTrace();        } catch (IllegalAccessException e) {            e.printStackTrace();        } catch (InstantiationException e) {            e.printStackTrace();        }打印结果:public void com.arvin.androidpluginbase.TestBean.testMethodPaterner(java.lang.String)public void com.arvin.androidpluginbase.TestBean.testMethod()调用了TestMethod方法

4.获取属性

       //获取属性        Class class1 = TestBean.class;        Field [] fields= class1.getDeclaredFields();//参数加上名字获取指定的参数         class1.getDeclaredFields("")        for(Field field :fields){            System.out.println("属性的名字:"+field.getName());        }        try {            Object object = class1.newInstance();            Field field =class1.getDeclaredField("testName");            int testName = Integer.parseInt(field.get(object).toString());            System.out.println("属性初始值:"+testName);            //设置新的            field.set(object, 99999);            int newValue =  Integer.parseInt(field.get(object).toString());            System.out.println("新的值 " +newValue);        } catch (NoSuchFieldException e) {            e.printStackTrace();        } catch (InstantiationException e) {            e.printStackTrace();        } catch (IllegalAccessException e) {            e.printStackTrace();        }//打印结果属性的名字:testName属性初始值:99新的值 99999

5.获取注解

   Class class1 = null;       try {           class1 = Class.forName("com.arvin.androidpluginbase.TestBean");       } catch (ClassNotFoundException e) {           e.printStackTrace();       }       Annotation []annotations = class1.getAnnotations();       for(Annotation annotation1:annotations){           System.out.println(annotation1.toString());       }        //获取方法的注解        Method[] methods = class1.getDeclaredMethods();        for(Method method : methods)        {            annotations = method.getAnnotations();            for(Annotation a : annotations)            {                System.out.println(a.toString());            }            Annotation[][] paraAnnotations = method.getParameterAnnotations();            for(int i = 0; i < paraAnnotations.length; i++)            {                for (Annotation a : paraAnnotations[i])                {                    System.out.println(a.toString());                }            }        }        //获取属性注解        Field[] fields = class1.getFields();        for (Field field : fields)        {            annotations = field.getAnnotations();            for(Annotation a : annotations)            {                System.out.println(a.toString());            }        }        //类似的获取构造器注解等。还可以指定某个注解类获取        class1.getAnnotation(Test1.class);        打印结果:        @com.arvin.androidpluginbase.Test(name=title, age=33)        @com.arvin.androidpluginbase.Test4(name=参数注解, age=33)        @com.arvin.androidpluginbase.Test2(name=方法注解, age=33)        @com.arvin.androidpluginbase.Test1(name=属性注解, age=33)

反射的应用

1.调用系统api隐藏的一些方法
例如: android中获取系统状态栏高度

  int statusBarHeight = -1;                try {                    Class<?> clazz = Class.forName("com.android.internal.R$dimen");                    Object object = clazz.newInstance();                    int height = Integer.parseInt(clazz.getField("status_bar_height")                            .get(object).toString());                    statusBarHeight = getResources().getDimensionPixelSize(height);                } catch (Exception e) {                    e.printStackTrace();                }

2.动态代理

动态代理在处理业务操作中一些事情很方便 ,例如 时间消耗。日志处理等一些公用操作的处理, 事物的分类处理等。
下面就用一个记录方法时间消耗来说明:
创建InvocationHandler 进行触发

class TimeProxyInvocationHandler implements InvocationHandler {    private Object target;    private BaseInterceptor interceptor;    public TimeProxyInvocationHandler(Object target, BaseInterceptor interceptor) {        this.target = target;        this.interceptor = interceptor;    }    @Override    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {        interceptor.start(method, args);        Object returnValue = method.invoke(target, args); //此处使用反射调用对应的方法        interceptor.end(method, args);        return returnValue;    }}public interface BaseTest{      void testTime();}  public interface BaseInterceptor {   void start(Method method ,Object[]args);    void end(Method method ,Object[]args);}class TimeInterceptor implements BaseInterceptor{    @Override    public void start(Method method, Object[] args) {        System.out.println(method.getName()+"方法开始执行的时间"+System.currentTimeMillis());    }    @Override    public void end(Method method, Object[] args) {        System.out.println(method.getName()+"方法执行结束的时间"+System.currentTimeMillis());    }}package com.arvin.androidpluginbase;/** * Created by Administrator on 2017/1/11. */@Test(name = "title",age=33)public class TestBean implements  BaseTest {    @Test1(name = "属性注解",age=33)     int testName =99 ;    public TestBean(){    }    @Test3(name = "构造注解",age=33)    public TestBean(String s,String s1){    }    public TestBean(String s,String s1,Object o){    }    @Test2(name = "方法注解",age=33)    public void testMethod(){        System.out.println("调用了TestMethod方法");    }    public void testMethodPaterner(@Test4(name = "参数注解",age=33) String s){    }    @Override    public void testTime() {    }}//最后调用TimeInterceptor timeInterceptor = new TimeInterceptor();TestBean testBean = new TestBean();InvocationHandler timeProxyInvocationHandler = new TimeProxyInvocationHandler(testBean,timeInterceptor);        BaseTest proxy = (BaseTest) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),TestBean.class.getInterfaces(), timeProxyInvocationHandler);        proxy.testTime();

3.扩展框架的灵活性:
如工厂模式
Class.forName(ClassName).newInstance();
可以通过传递对应子类的包名创建对应对象而不用创建很多个工厂类
//可以在一个泛型为int的
ArrayList list = new ArrayList();
try {
Method method = list.getClass().getMethod(“add”, Object.class);
method.invoke(list, “Java反射机制实例。”);
System.out.println(list.get(0));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}

反射的优缺点

缺点:
1.由于是在运行时动态的获取,没有预编译时效率高,由于是动态获取很容易出现很多异常需要捕获,降低了程序的效率。
2.代码行数增大,正常情况需要1行或者两行的代码,反射需要更多的代码量。

优点:
增大代码的灵活性和扩展性
可以调用一些系统隐藏的API。

对于反射来说在android 中如果要了解热更新,和热修复等也是必不可少的。

0 0
原创粉丝点击