JAVA源码解析(6)-java.beans.PropertyDescriptor

来源:互联网 发布:淘宝网羽绒服女款套装 编辑:程序博客网 时间:2024/04/28 18:58

Jdk版本:1.8.0_131
作者出于学习阶段,如有问题请指正

来自API的解释:PropertyDescriptor 描述 Java Bean 通过一对存储器方法导出的一个属性。

PropertyDescriptor有一个常用的功能:包装一个属性的Get、Set方法,通过反射调取相应的方法,举例:

构造一个PropertyDescriptor -> 获取get/set方法 -> invoke

在整个PropertyDescriptor中,有两个重要的概念:readMethod和writeMethod,理解起来,可以当做是目前流行的Get方法和Set方法

目前有如下构造器:

构造器中,若传入了Class,则会验证class,否则会忽略classPropertyDescriptor(String propertyName, Class<?> beanClass)默认传入的readMethod和writeMethod为:is+属性名/set+属性名PropertyDescriptor(String propertyName, Class<?> beanClass, String readMethodName, String writeMethodName)默认构造器PropertyDescriptor(String propertyName, Method readMethod, Method writeMethod)忽略了class的包装propertyName的构造器,目前用的比较多的也是这个PropertyDescriptor(Class<?> bean, String base, Method read, Method write)basepropertyName的区别是,当前构造器会调用Introspector.decapitalize(base)方法来设置属性名

Introspector.decapitalize方法:

public static String decapitalize(String name) {        if (name == null || name.length() == 0) {            return name;        }        if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&                        Character.isUpperCase(name.charAt(0))){            return name;        }        char chars[] = name.toCharArray();        chars[0] = Character.toLowerCase(chars[0]);        return new String(chars);    }

看不懂的话看下示例:
name -> name
Name ->name
nAme -> nAme
NAME -> NAME

getPropertyType/setPropertyType方法:

getPropertyType返回属性的Java类型信息。注意Class对象可能会描述原始Java类型,例如int。 这种类型是由read方法返回的,或者用作write方法的参数类型。 如果类型是不支持非索引访问的索引属性,则返回null。

    public synchronized Class<?> getPropertyType() {        Class<?> type = getPropertyType0();        if (type  == null) {            try {                type = findPropertyType(getReadMethod(), getWriteMethod());                setPropertyType(type);            } catch (IntrospectionException ex) {                // Fall            }        }        return type;    }

findPropertyType方法如下:

private Class<?> findPropertyType(Method readMethod, Method writeMethod)        throws IntrospectionException {        Class<?> propertyType = null;        try {            if (readMethod != null) {                Class<?>[] params = getParameterTypes(getClass0(), readMethod);                if (params.length != 0) {                    throw new IntrospectionException("bad read method arg count: "                                                     + readMethod);                }                propertyType = getReturnType(getClass0(), readMethod);                if (propertyType == Void.TYPE) {                    throw new IntrospectionException("read method " +                                        readMethod.getName() + " returns void");                }            }            if (writeMethod != null) {                Class<?>[] params = getParameterTypes(getClass0(), writeMethod);                if (params.length != 1) {                    throw new IntrospectionException("bad write method arg count: "                                                     + writeMethod);                }                if (propertyType != null && !params[0].isAssignableFrom(propertyType)) {                    throw new IntrospectionException("type mismatch between read and write methods");                }                propertyType = params[0];            }        } catch (IntrospectionException ex) {            throw ex;        }        return propertyType;    }

该类的使用案例:http://blog.csdn.net/z6913787/arti/8443777
关键的代码没问题,其余的方法没有太多可以说的地方,如有不懂的地方请及时留言

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝购物提示买家信息错误怎么办 苹果淘宝占用空间大怎么办 苹果手机淘宝占内存太大怎么办 苹果手机淘宝图标找不到了怎么办 苹果手机看淘宝很卡怎么办 苹果手机淘宝忘了密码怎么办 苹果手机更新后淘宝打不开怎么办 淘宝买到苹果翻新机怎么办 淘宝网密码忘了怎么办 淘宝改密码要拍摄脸部怎么办 苹果一体机键盘没反应怎么办 淘宝买东西退货卖家拒绝怎么办? 淘宝被限制下单怎么办 淘宝扫码登录后怎么办 花呗选项被隐藏怎么办 新换手机支付宝怎么办 ih5点击按钮跳转页面怎么办 sap点安装程序没反应怎么办 淘宝店账号忘了怎么办 点击电脑桌面图标没反应怎么办 为什么淘宝打不开已停止运行怎么办 移动宽带打不开淘宝网怎么办 淘宝发布宝贝没有品牌怎么办 烫了卷发显老怎么办 唯品会商品不支持退换货怎么办 京东没有自提怎么办 不支持七天无理由的商品怎么办 淘宝评价忘记点亮星星怎么办 淘宝宝贝权重降低了该怎么办 换卡支付宝账号怎么办 支付宝免密支付无法关闭怎么办 手机换了支付宝怎么办 支付宝版本过低怎么办 支付宝无故扣钱怎么办 支付宝里面的钱不见了怎么办 支付宝还不起钱怎么办 支付宝借不到钱怎么办 买家淘宝号被监控了怎么办 卖微信号被骗了怎么办 淘宝原密码忘了怎么办 淘宝号忘记密码了怎么办啊