zoukankan      html  css  js  c++  java
  • 反射调用 java bean的set和get方法

    一、使用java.beans.PropertyDescriptor

    import java.beans.IntrospectionException;
    import java.beans.PropertyDescriptor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    public class PropertyUtil {
        
        private static final String SET_PREFIX = "set";
        private static final String IS_PREFIX = "is";
        private static final String GET_PREFIX = "get";
        
        public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName) {//根据需求,定制 自己的get和set方法
            Method setMethod = null;
            Method getMethod = null;
            PropertyDescriptor pd = null;
            try {
                Field field = clazz.getDeclaredField(propertyName);// 根据字段名来获取字段
                if (field != null) {
                    // 构建方法的后缀
                    String methodEnd = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
                    setMethod = clazz.getDeclaredMethod(SET_PREFIX + methodEnd, new Class[] { field.getType() });
                    // 构建get 方法
                    getMethod = clazz.getDeclaredMethod(GET_PREFIX + methodEnd, new Class[] {});
                    // 构建一个属性描述器 把对应属性 propertyName 的 get 和 set 方法保存到属性描述器中
                    pd = new PropertyDescriptor(propertyName, getMethod, setMethod);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    
            return pd;
        }
        
        public static PropertyDescriptor getPropertyDescriptor2(Class<?> clazz, String propertyName) {//使用 PropertyDescriptor 提供的 get和set方法
            try {
                return new PropertyDescriptor(propertyName, clazz);
            } catch (IntrospectionException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        public static void setProperty(Object obj, String propertyName, Object value) {
            Class<?> clazz = obj.getClass();// 获取对象的类型
            PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);// 获取
                                                                                // clazz
                                                                                // 类型中的
                                                                                // propertyName
                                                                                // 的属性描述器
            Method setMethod = pd.getWriteMethod();// 从属性描述器中获取 set 方法
            try {
                setMethod.invoke(obj, new Object[] { value });// 调用 set
                                                                // 方法将传入的value值保存属性中去
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static Object getProperty(Object obj, String propertyName) {
            Class<?> clazz = obj.getClass();// 获取对象的类型
            PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);// 获取
                                                                                // clazz
                                                                                // 类型中的
                                                                                // propertyName
                                                                                // 的属性描述器
            Method getMethod = pd.getReadMethod();// 从属性描述器中获取 get 方法
            Object value = null;
            try {
                value = getMethod.invoke(clazz, new Object[] {});// 调用方法获取方法的返回值
            } catch (Exception e) {
                e.printStackTrace();
            }
            return value;// 返回值
        }
        
        
    }

    二、使用收藏的一个工具类 BeanHelper

      查看代码

    三、获取一个类每个属性的 PropertyDescriptor

     org.springframework.beans.BeanUtils

      PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);

      使用请参考 这里

  • 相关阅读:
    【转载】SAP_ECC6.0_EHP4或SAP_ECC6.0_EHP5_基于Windows_Server_2008R2_和SQL_server_2008下的安装
    使用delphi 开发多层应用(二十四)KbmMW 的消息方式和创建WIB节点
    使用delphi 开发多层应用(二十三)KbmMW 的WIB
    实现KbmMw web server 支持https
    KbmMW 服务器架构简介
    Devexpress VCL Build v2014 vol 14.1.1 beta发布
    使用delphi 开发多层应用(二十二)使用kbmMW 的认证管理器
    KbmMW 4.50.00 测试版发布
    Basic4android v3.80 beta 发布
    KbmMW 认证管理器说明(转载)
  • 原文地址:https://www.cnblogs.com/hujunzheng/p/6778223.html
Copyright © 2011-2022 走看看