java内省的两种实现方式

来源:互联网 发布:js eval解析语句 编辑:程序博客网 时间:2024/06/06 00:52

根据百度文库的解释内省的定义是:内省(Introspector)是Java语言对Bean类属性、事件的一种缺省处理方法。也就是说,我们可以通过java.beans包里面的方法操作javabean

1.第一种实现方式:

我们先定义一个ReflectPoint类

public class ReflectPoint {    private int x;    private int y;    public int getX() {        return x;    }    public void setX(int x) {        this.x = x;    }    public int getY() {        return y;    }    public void setY(int y) {        this.y = y;    }    public ReflectPoint(int x,int y)    {            this.x=x;            this.y=y;    }}

这个是一个经典的javabean,那我们应该如何操作它呢

第一种方式

import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class IntroSpectorTest {    public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {        ReflectPoint rp=new ReflectPoint(3,4);        String propertyName="x";        Object retVal = getProperty(rp, propertyName);        System.out.println(retVal);        Object val=1;        setProperty(rp, propertyName, val);        System.out.println(rp.getX());    }    private static void setProperty(Object rp, String propertyName,            Object val) throws IntrospectionException, IllegalAccessException,            InvocationTargetException {        PropertyDescriptor pd2=new PropertyDescriptor(propertyName,rp.getClass());        Method methodSetX=pd2.getWriteMethod();        methodSetX.invoke(rp, val);    }    private static Object getProperty(Object rp, String propertyName)            throws IntrospectionException, IllegalAccessException,            InvocationTargetException {        Object retVal=null;        PropertyDescriptor pd=new PropertyDescriptor(propertyName,rp.getClass());        Method methodSetX=pd.getReadMethod();        retVal=methodSetX.invoke(rp);           return retVal;    }}

运行结果为:
这里写图片描述
这种事比较简便的方式,通过这种方式就可以实现对javabean的各种操作。

第二种:

import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class IntroSpectorTest {    public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {        ReflectPoint rp=new ReflectPoint(3,4);        String propertyName="x";        Object retVal = getProperty(rp, propertyName);        System.out.println(retVal);        Object val=1;        setProperty(rp, propertyName, val);        System.out.println(rp.getX());    }    private static void setProperty(Object rp, String propertyName,            Object val) throws IntrospectionException, IllegalAccessException,            InvocationTargetException {        BeanInfo beanInfo=Introspector.getBeanInfo(rp.getClass());        PropertyDescriptor[] pds=beanInfo.getPropertyDescriptors();        Object retVal=null;        for(PropertyDescriptor pd:pds)        {            if(pd.getName().equals(propertyName))            {                Method methodGetX=pd.getWriteMethod();                methodGetX.invoke(rp,val);                break;            }        }    }    private static Object getProperty(Object rp, String propertyName)            throws IntrospectionException, IllegalAccessException,            InvocationTargetException {        BeanInfo beanInfo=Introspector.getBeanInfo(rp.getClass());        PropertyDescriptor[] pds=beanInfo.getPropertyDescriptors();        Object retVal=null;        for(PropertyDescriptor pd:pds)        {            if(pd.getName().equals(propertyName))            {                Method methodGetX=pd.getReadMethod();                retVal=methodGetX.invoke(rp);                break;            }        }        return retVal;    }}

运行结果是
这里写图片描述
这种方式是通过Introspector获得BeanInfo对象,然后再获得PropertyDescriptor描述器对象。再进行操作,所以相对于第一种方法,第二种方法比较复杂。

0 0
原创粉丝点击