A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 如梦初醒 中级黑马   /  2012-4-3 23:41  /  1893 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

听方立勋老师的视频内省操作javabean的属性,我对反射以及面向对象有了更新的认识,
我认为任何对象都可看做Bean,因为任何对象都继承自Object类,而Object类上有一个
getClass()方法和一个class属性。内省可获得Bean上的任何属性,为了更加的深入理解内省操作bean,于是我写了一个程序,经过运行后我发现,内省操作Bean获得其属性描述符
(PropertyDescriptor)有两种方式:
一种是:
BeanInfo beanInfo=Introspector.getBeanInfo(O.class);
PropertyDescriptor[]  pds=beanInfo.getPropertyDescriptors();
这种方式能获得一个Bean对象上的所有属性描述符,但是如果只需获得Bean对象上的某个特定的属性描述符,用这种方式还要对属性描述符数组进行遍历然后得到特定的属性描述符,这样做比较麻烦,所以为了得到Bean的对象上的某个特定的属性描述符,sun公司提供了获得Bean上的某个特定的属性描述符的方式,
第二种:
PropertyDescriptor pd=new PropertyDescriptor("XXX",O.class);
这种方式就能获得O对象上的XXX属性的属性描述符,
但是我通过下面的程序发现,第二种方式有缺陷,那就是这个O对象上的getXXX()方法和setXXX()方法必须成对显示出现,这种方式才能获得O对象上的属性信息,下面通过程序来说明。
package com.crowndint.javabean;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import org.junit.Test;
public class propertyDescriptorTest {
   @Test       
   public void test() throws Exception{
           PropertyDescriptor[] propertyDescriptors=Introspector.getBeanInfo(O.class).getPropertyDescriptors();
           for(PropertyDescriptor  property : propertyDescriptors)
           {
                    System.out.format("Type : %s\nName : %s\n", property.getPropertyType()/*.getName()*/,property.getName());
                    System.out.println(property.getReadMethod());
                    System.out.println(property.getWriteMethod());          
           }
           System.out.println("==============================================");
           PropertyDescriptor pd=new PropertyDescriptor("XXX",O.class);
           System.out.format(" Type:%60s\n Name:%60s\n ReadMethod:%s\n WriteMethod:%s\n",
                           pd.getPropertyType(),pd.getName(),pd.getReadMethod(),pd.getWriteMethod());
  }}
class O{
        //private int xxx;
        public int getXXX(){ return 0; }
        public void setXXX(int xxx){}
}
程序运行结果:
Type : int
Name : XXX
public int com.crowndint.javabean.O.getXXX()
public void com.crowndint.javabean.O.setXXX(int)
Type : class java.lang.Class
Name : class
public final native java.lang.Class java.lang.Object.getClass()
null
=========================================================
Type:                           int
Name:                           XXX
ReadMethod:public int com.crowndint.javabean.O.getXXX()
WriteMethod:public void com.crowndint.javabean.O.setXXX(int)
**********************************************************************************************************************************
可以看出PropertyDescriptor pd=new PropertyDescriptor("XXX",O.class);
能获得O对象上的XXX属性信息,因为这时候的O对象上的getXXX()方法和setXXX()方法是成对出现的,但是当把O对象上的getXXX()方法和setXXX()方法其中一个注释掉之后,运行结果为:
Type : int
Name : XXX
public int com.crowndint.javabean.O.getXXX()
public void com.crowndint.javabean.O.setXXX(int)
Type : class java.lang.Class
Name : class
public final native java.lang.Class java.lang.Object.getClass()
null
=========================================================
juint4抛出了一堆异常,异常首行为:
java.beans.IntrospectionException: Method not found: setXXX

从注释掉setXXX()方法之后的运行结果可看出,
PropertyDescriptor pd=new PropertyDescriptor("XXX",O.class);
并不能获得O对象上的XXX属性信息。
可见PropertyDescriptor pd=new PropertyDescriptor("XXX",O.class);
这种方式来获取Bean对象上的特定属性是有局限性的。
以上是我学习中的一点发现,如有不当之处请各位指正。

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马