使用Introspector(Java内省机制)实现Map转换为JavaBean

转载自 https://blog.csdn.net/u013905744/article/details/73167861

需求:

有一个Map对象

[java]  view plain  copy
  1. Map<String,Object> map = new HashMap<>();  
  2. map.put("name""bellychang");  
  3. map.put("likes"new String[]{"football""basketball"});  

希望实现一个通用方法,将其转换为如下的JavaBean

[java]  view plain  copy
  1. public class SimpleBean {  
  2.   
  3.     private String name;  
  4.     private String[] likes;  
  5.   
  6.     public SimpleBean() {  
  7.     }  
  8.   
  9.     public SimpleBean(String name, String[] likes){  
  10.         this.name = name;  
  11.         this.likes = likes;  
  12.     }  
  13.   
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.   
  18.     public void setName(String name) {  
  19.         this.name = name;  
  20.     }  
  21.   
  22.     public String[] getLikes() {  
  23.         return likes;  
  24.     }  
  25.   
  26.     public void setLikes(String[] likes) {  
  27.         this.likes = likes;  
  28.     }  
  29.   
  30.     @Override  
  31.     public String toString() {  
  32.         return "SimpleBean{" +  
  33.                 "name='" + name + '\'' +  
  34.                 ", likes=" + Arrays.toString(likes) +  
  35.                 '}';  
  36.     }  
  37. }  

工具类

[java]  view plain  copy
  1. public static <T> T convertMap(Class<T> type, Map map) throws IntrospectionException, IllegalAccessException,  
  2.         InstantiationException, InvocationTargetException {  
  3.     BeanInfo beanInfo = null// 获取类属性  
  4.     T obj = null;  
  5.     beanInfo = Introspector.getBeanInfo(type);  
  6.     // 创建 JavaBean 对象  
  7.     obj = type.newInstance();  
  8.   
  9.     // 给 JavaBean 对象的属性赋值  
  10.   
  11.     // 获取属性的描述器(PropertyDescriptor)  
  12.     PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();  
  13.   
  14.     // 通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后就可以通过反射机制来调用这些方法  
  15.     for (int i = 0; i < propertyDescriptors.length; i++) {  
  16.         PropertyDescriptor descriptor = propertyDescriptors[i];  
  17.         String propertyName = descriptor.getName();  
  18.         if (map.containsKey(propertyName)) {  
  19.             // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。  
  20.             Object value = map.get(propertyName);  
  21.   
  22.             Object[] args = new Object[1];  
  23.             //getPropertyType得到属性类型  
  24.             if (descriptor.getPropertyType() == Long.class) {  
  25.                 args[0] = Long.parseLong(value.toString());  
  26.             } else if (descriptor.getPropertyType() == Integer.class) {  
  27.                 args[0] = Integer.valueOf(value.toString());  
  28.             } else {  
  29.                 args[0] = value;  
  30.             }  
  31.   
  32.             //getWriteMethod()得到此属性的set方法----Method对象,然后用invoke调用这个方法  
  33.             descriptor.getWriteMethod().invoke(obj, args);  
  34.         }  
  35.     }  
  36.     return obj;  
  37. }  
测试类

[java]  view plain  copy
  1. @Test  
  2. public void testConvertMap() throws Exception {  
  3.     //将一个Map对象转换为JavaBean  
  4.     Map<String,Object> map = new HashMap<>();  
  5.     map.put("name""changliang");  
  6.     map.put("likes"new String[]{"football""basketball"});  
  7.     SimpleBean simpleBean = BeanToMapUtil.convertMap(SimpleBean.class, map);  
  8.     System.out.println(simpleBean);  
  9. }  

注意事项:

1. Map的key与JavaBean的key一致

2. JavaBean中要有空的构造函数,以及get,set方法

参考:java 中的内省机制

猜你喜欢

转载自blog.csdn.net/u012240455/article/details/80515497