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
- java内省的两种实现方式
- java内省机制 + 内省是什么 + 内省实现方式 + 和反射的区别
- 内省调用JavaBean实现getXxx和setXxx的两种方式
- Java实现多线程的两种方式
- Java定时器 实现的两种方式
- Java定时器 实现的两种方式
- Java实现多线程的两种方式
- Java多线程实现的两种方式
- Java 多线程实现的两种方式
- Java多线程的两种实现方式
- java实现线程的两种方式
- Java实现线程的两种方式
- Java实现线程的两种方式
- JAVA多线程实现的两种方式
- java 实现多线程的两种方式
- java实现同步的两种方式
- Java栈的两种实现方式
- Java实现多线程的两种方式
- APUE读书笔记------16章 网络IPC:套接字
- 设计模式-讲解
- hdu1078(记忆化搜索)
- 确定windows操作系统的默认编码格式
- android Resources getDimension getDimensionPixelSize getDimensionPixelOffset
- java内省的两种实现方式
- iOS 常用的正则表达式和谓词的配合使用
- TL081CP JFET输入运算放大器 [JFET-INPUT OPERATIONAL AMPLIFIERS]
- .Net开源之corefx、coreclr初探(二)
- oracle[点滴积累]
- 树与二叉树的性质
- java8__stream初体验
- MongoDB 3.0即将于3月发布
- 推荐系统的架构