Chinaunix首页 | 论坛 | 博客
  • 博客访问: 137183
  • 博文数量: 37
  • 博客积分: 277
  • 博客等级: 二等列兵
  • 技术积分: 326
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-17 21:06
文章分类
文章存档

2016年(2)

2015年(11)

2014年(5)

2013年(7)

2012年(4)

2011年(8)

分类: Java

2011-04-30 23:33:38

  项目实施过程中,需要调用外系统的接口,外系统的参数总是形如 Map method(Map req)的结构,这种方式,调用是很简单,但是降低了对输入参数的约束力和代码的直观性,经过和项目组长的讨论,决定在本系统中,一律使用Bean的形式传递参数。于是就产生了一个问题,本系统的bean 到 Map 之间的转换。如果每个bean写一个转换的方法,未免太过繁琐,于是想到了java 的反射机制,将bean 和 map 来后的转换。废话少说了,直接上代码,具体问题代码注释解释.
/**
*
* 将bean转换为map结构
* bean : bean对象
* outputNull: 为true表示不管某个属性是否设置了值,都添加到Map中
*/
public static Map newMapFromBean(Object bean,boolean outputNull) throws IllegalArgumentException, IllegalAcces***ception, InvocationTargetException{
       
        Map newMap = new HashMap();
        Class beanClass = bean.getClass();
        Method[] methods = beanClass.getMethods();
       
        for(Method method : methods){
            String methodName = method.getName();
            // 属性名的格式为 get + 属性名将首字母大写
            if(matchPropertyGetMethod(methodName)){
                // 如果方法的访问修饰符为 public 并且输入参数为空 则认为是获取属性方法
                if(Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0){
                    Object res = method.invoke(bean, null);
                    if(outputNull){ // 如果不输出null字段
                        newMap.put(catProperty(methodName),res);
                    }else if(res != null){
                        newMap.put(catProperty(methodName),res);
                    }
                }
            }
           
        }
        return newMap;
    }
 /**
*
* 从map转换到Bean
*
*/  
    public static Object newBeanFromMap(Class beanClass,Map dataSrc) throws Exception{
        Object bean = null;
        try {
            bean = beanClass.newInstance();
            Method[] methods = beanClass.getMethods();
           
            for(Method method : methods){
                String methodName = method.getName();
                if(matchPropertySetMethod(methodName)){
                    // 如果方法的访问修饰符为 public 并且输入参数个数为1 则认为是获取属性方法
                    if(Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 1){
                        method.invoke(bean, dataSrc.get(catProperty(methodName)));
                    }
                }
            }
           
        } catch (Exception e){
            e.printStackTrace();
            throw e;
        }
       
        return bean;
    }
    /**
     * 检察一个方法名是否为标准的获取属性的方法名
     * @param methodName 方法名称
     * @return true : 该方法名称是一个获取属性的方法名 false: 该方法不是一个获取属性的方法名
     */
    public static boolean matchPropertyGetMethod(String methodName){
       
        return Pattern.compile("get\\p{Upper}.*").matcher(methodName).matches();
    }
   
    /**
     * 判断一个方法是否为属性设置方法
     * @param methodName
     * @return
     */
    public static boolean matchPropertySetMethod(String methodName){
        return Pattern.compile("set\\p{Upper}.*").matcher(methodName).matches();
    }
    /**
     * 从标准的bean属性方法中,提取出属性名
     * @param propertyMethod 属性方法名
     * @return 属性值
     */
    public static String catProperty(String propertyMethod){
       
        String property = propertyMethod.substring(3,4).toLowerCase();// set 和 get 方法刚好占3个字符
       
        if(propertyMethod.length() > 4){ // 如果长度大于四,则将后面的字符串也加进来
            property = property + propertyMethod.substring(4);
        }
        return property;
    }


使用时有几点需要注意: 1: bean 的class 必须要有默认构造方法
                      2: 来回转换的依据为属性名


第一次发博,不足之处,请指教,虚心求教谢谢!
阅读(1165) | 评论(0) | 转发(0) |
0

上一篇:spring annotation

下一篇:DB2 字符集

给主人留下些什么吧!~~