java内省的两种实现方式

根据百度文库的解释内省的定义是:内省(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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值