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.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
public class BeanUtils {
/**
* bean 转化为 HashMap
*
* @param bean
* @return
*/
public static HashMap<String, Object> beanToMap(Object bean) {
HashMap<String, Object> map = new HashMap<String, Object>();
if (null == bean) {
return map;
}
Class<?> clazz = bean.getClass();
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(clazz);
} catch (IntrospectionException e) {
e.printStackTrace();
}
PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor descriptor : descriptors) {
String propertyName = descriptor.getName();
if (!"class".equals(propertyName)) {
Method method = descriptor.getReadMethod();
Object result;
try {
result = method.invoke(bean);
if (null != result) {
map.put(propertyName, result);
} else {
map.put(propertyName, "");
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
return map;
}
/**
* map 转化为 bean实体
*
* @Title: mapToBean
* @Description: Controller用
* @param type
* @param request
* @return
* @throws Exception
* @return: Object
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object mapToBean(Class type, HttpServletRequest request) throws Exception {
Map<String, Object> map = JSONObject.fromObject(request.getAttribute("condition"));
BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
Object obj = type.newInstance(); // 创建 JavaBean 对象
// 给 JavaBean 对象的属性赋值
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
// 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
Object value = map.get(propertyName);
Object[] args = new Object[1];
args[0] = value;
descriptor.getWriteMethod().invoke(obj, args);
}
}
return obj;
}
/**
* map 转化为 bean实体
*
* @Title: mapToBean
* @Description: 泛用
* @param type
* @param map
* @return
* @throws Exception
* @return: Object
*/
@SuppressWarnings("rawtypes")
public static Object mapToBean(Class type, Map<String, Object> map) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
Object obj = type.newInstance(); // 创建 JavaBean 对象
// 给 JavaBean 对象的属性赋值
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
// 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
Object value = map.get(propertyName);
Object[] args = new Object[1];
args[0] = value;
descriptor.getWriteMethod().invoke(obj, args);
}
}
return obj;
}
}