Java examples for Reflection:Java Bean
convert Map to Java Bean
//package com.java2s; 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; import java.util.Map; public class Main { public static Object convertMap2Bean(Class type, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException { BeanInfo beanInfo = Introspector.getBeanInfo(type); Object obj = type.newInstance(); PropertyDescriptor[] propertyDescriptors = beanInfo .getPropertyDescriptors(); for (PropertyDescriptor pro : propertyDescriptors) { String propertyName = pro.getName(); if (pro.getPropertyType().getName().equals("java.lang.Class")) { continue; }/*from w ww . ja v a2 s . c o m*/ if (map.containsKey(propertyName)) { Object value = map.get(propertyName); Method setter = pro.getWriteMethod(); setter.invoke(obj, value); } } return obj; } }