JaveBeanとMapの変換

3709 ワード

/*
 * Creation : 2014 11 25 
 */
package cn.newtouch.utils;

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.HashMap;
import java.util.Map;

/**
 * The Class BeanToMapUtils.
 * 
 * @author Li Hong Tai
 */
public class BeanAndMapTransfermationUtils {

	/**
	 * A Map object can be converted to a JavaBean.
	 * 
	 * @param beanClass
	 *            To convert the type
	 * @param map
	 *            The map contains the value of an attribute
	 * @return Transformation of the JavaBean object
	 * @throws IntrospectionException
	 *             If failure analysis class attribute
	 * @throws InstantiationException
	 *             If instantiation JavaBean to fail
	 * @throws IllegalAccessException
	 *             If instantiation JavaBean to fail
	 */
	@SuppressWarnings("rawtypes")
	public static Object mapToBean(Class beanClass, Map map) throws IntrospectionException, InstantiationException, IllegalAccessException {
		// get class properties.
		BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
		// create JavaBean object.
		Object object = beanClass.newInstance();
		// To the JavaBean object attribute assignment
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
			String propertyName = propertyDescriptor.getName();
			if (map.containsKey(propertyName)) {
				try {
					Object value = map.get(propertyName);
					Object[] args = new Object[1];
					args[0] = value;
					propertyDescriptor.getWriteMethod().invoke(object, args);
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
		return object;
	}

	/**
	 * A JavaBeancan be converted to Map object .
	 * 
	 * @param bean
	 *            the bean
	 * @return Transformation of the Map Object
	 * @throws IntrospectionException
	 *             If failure analysis class attribute
	 * @throws IllegalAccessException
	 *             If instantiation JavaBean to fail
	 * @throws InvocationTargetException
	 *             the invocation target exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map beanToMap(Object bean) throws IntrospectionException, IllegalAccessException, InvocationTargetException {
		Class beanClass = bean.getClass();
		Map resultMap = new HashMap();
		BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
			String propertyName = propertyDescriptor.getName();
			if (!propertyName.equals("class")) {
				Method readMethod = propertyDescriptor.getReadMethod();
				Object result = readMethod.invoke(bean, new Object[0]);
				if (result != null) {
					resultMap.put(propertyName, result);
				} else {
					resultMap.put(propertyName, "");
				}
			}
		}
		return resultMap;
	}
}