谷哥的小弟学后台(32)——内省Introspector

来源:互联网 发布:mac如何修改照片像素 编辑:程序博客网 时间:2024/05/22 15:07

探索Android软键盘的疑难杂症
深入探讨Android异步精髓Handler
详解Android主流框架不可或缺的基石
站在源码的肩膀上全解Scroller工作机制


Android多分辨率适配框架(1)— 核心基础
Android多分辨率适配框架(2)— 原理剖析
Android多分辨率适配框架(3)— 使用指南


自定义View系列教程00–推翻自己和过往,重学自定义View
自定义View系列教程01–常用工具介绍
自定义View系列教程02–onMeasure源码详尽分析
自定义View系列教程03–onLayout源码详尽分析
自定义View系列教程04–Draw源码分析及其实践
自定义View系列教程05–示例分析
自定义View系列教程06–详解View的Touch事件处理
自定义View系列教程07–详解ViewGroup分发Touch事件
自定义View系列教程08–滑动冲突的产生及其处理


版权声明

  • 本文原创作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

内省(Introspector)简介

内省Introspector是一种特殊的反射技术,常用于操作JavaBean。


字段和属性以及JavaBean

先来看我们经常写的一种类:

/** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */package cn.com;public class Girl {    private String name;    private int age;    private int bust;    private int waist;    private int hip;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public int getBust() {        return bust;    }    public void setBust(int bust) {        this.bust = bust;    }    public int getWaist() {        return waist;    }    public void setWaist(int waist) {        this.waist = waist;    }    public int getHip() {        return hip;    }    public void setHip(int hip) {        this.hip = hip;    }}

在该类中,我们定义了几个字段并且为它们生成了对应的getXXX()和setXXX()。在很多时候我们喜欢把name,age,waist这些东西叫属性。其实,更加规范的的说法应该是:把有getXXX()或者setXXX()的变量叫做属性,把没有getXXX()或者setXXX()的变量称作字段。

很多时候,我们会看到这样的类:里面只有属性及其对应的get和set方法,这样的类也被称为JavaBean,比如此处的Girl类。


内省Introspector的使用方式

在此介绍与内省Introspector相关的操作

    /**     * 本文作者:谷哥的小弟     * 博客地址:http://blog.csdn.net/lfdfhl     */    private static void testPropertyDescriptor1() throws IntrospectionException{        BeanInfo beanInfo = Introspector.getBeanInfo(Girl.class);        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {            System.out.println(propertyDescriptor.getName());        }    }

在此,我们利用属性描述器PropertyDescriptor获取Bean的属性,结果如下:

agebustclasshipnamewaist

嗯哼,发现了么?我们不但获取到了Gril类的五个属性还获取到了它的父类Object的属性class。

继续来看下面的例子:

    /**     * 本文作者:谷哥的小弟     * 博客地址:http://blog.csdn.net/lfdfhl     */    private static void testPropertyDescriptor2() throws Exception{        Girl girl = new Girl();        PropertyDescriptor propertyDescriptor = new PropertyDescriptor("age", Girl.class);        Method setter = propertyDescriptor.getWriteMethod();        setter.invoke(girl, 18);        Method getter = propertyDescriptor.getReadMethod();        Object age = getter.invoke(girl, null);        System.out.println("age="+age);    }

输出结果:

age=18

代码解析如下:

  • 得到与属性对应的属性描述器PropertyDescriptor,请参见代码第7行
  • 得到该属性的set方法后为对象赋值,请参见代码第8—9行
  • 得到该属性的get方法后取出属性值,请参见代码第10—11行

Beanutils

BeanUtils是Apache提供的操作JavaBean的工具,它使用简单,操作方便。如有需要,可至其官方网站详细了解;在此不再赘述。

0 0