通过PropertyDescriptor和Introspector对JavaBean的简单内省操作
来源:互联网 发布:人工智能计算器破解 编辑:程序博客网 时间:2024/05/29 02:33
</pre><pre name="code" class="java">package com.franky.bean;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;/** * @描述 利用PropertyDescriptor类或Introspector类 * 进行javabean的内省操作,获取读写方法 * @作者 franky * @日期 2014-12-31 上午11:16:35 */public class JavaBeanTest {/** * @param args * @throws Exception */public static void main(String[] args) throws Exception {Point point = new Point(4, 5);String propertyName = "x";//按照javabean的方式来对对象进行内省操作//获取读的方法Object retVal = getReadMethod(point, propertyName);//另一种获取读的方法Object retVals = getReadMethod2(point, propertyName);System.out.println(retVal);//获取写的方法,并设置值Object value = 9;getWriteMethod(point, propertyName, value);System.out.println(point.getX());}/** * 获取bean对象的写方法,并修改值 * @param obj 内省javabean对象 * @param propertyName 属性名 * @param value 传入要修改的值对象 */private static void getWriteMethod(Object obj, String propertyName,Object value) throws IntrospectionException,IllegalAccessException, InvocationTargetException {PropertyDescriptor property2 = new PropertyDescriptor(propertyName , obj.getClass());Method writeMethod = property2.getWriteMethod();writeMethod.invoke(obj, value);}/** * 获取bean对象的读方法,并返回属性的值 * @param obj 内省javabean对象 * @param propertyName 属性名 * @return 返回属性值 */private static Object getReadMethod(Object obj, String propertyName)throws IntrospectionException, IllegalAccessException,InvocationTargetException {PropertyDescriptor property = new PropertyDescriptor(propertyName , obj.getClass());Method readMethod = property.getReadMethod();Object retVal = readMethod.invoke(obj, null);return retVal;}/** * 另一种获取bean对象的读方法,并返回属性的值 * @param obj 内省javabean对象 * @param propertyName 属性名 * @return 返回属性值 */private static Object getReadMethod2(Object obj, String propertyName)throws IntrospectionException, IllegalAccessException,InvocationTargetException {Object retVal = null; BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();for (int i = 0; i < descriptors.length; i++) {if(propertyName.equals(descriptors[i].getName())){Method readMethod = descriptors[i].getReadMethod();retVal = readMethod.invoke(obj, null);}}return retVal;}}
Point类:
package com.franky.bean;public class Point {private int x;private int y;/** * @return the x */public int getX() {return x;}/** * @param x the x to set */public void setX(int x) {this.x = x;}/** * @return the y */public int getY() {return y;}/** * @param y the y to set */public void setY(int y) {this.y = y;}public Point(int x, int y) {super();this.x = x;this.y = y;}}
0 0
- 通过PropertyDescriptor和Introspector对JavaBean的简单内省操作
- JAVA内省JavaBean(Introspector、BeanInfo和PropertyDescriptor)
- 内省Introspector操作 JavaBean的简单实现方式及稍复杂实现方式 主要用到PropertyDescriptor类
- 【Java进阶】内省IntroSpector操作JavaBean和Apache-commons-dbutils对内省的使用
- 【JavaEE】Java内省Introspector、PropertyDescriptor与JavaBean
- 对javabean的简单内省和复杂内省操作
- 【内省】introspector操作JavaBean
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- MATLAB图像处理_plot的使用详解
- 修改linux readonly
- 2014年生活记录
- 适配器模式
- iphone开发资源汇总
- 通过PropertyDescriptor和Introspector对JavaBean的简单内省操作
- RabbitMQ用户角色及权限控制
- LZ77 算法的基本流程
- Ps图片欣赏
- 字典序全排列算法研究
- 调用qq音乐接口备用...
- 几个常见的压缩算法
- oc语法的基础(二)
- Hello World!