JavaBeanとMap間の相互変換


参照用コード:
/**
	 *     JavaBean         Map
	 * 
	 * @param bean
	 *                JavaBean   
	 * @param qualifier
	 *            hbase  
	 * @return       Map   
	 * @throws IntrospectionException
	 *                      
	 * @throws IllegalAccessException
	 *                   JavaBean   
	 * @throws InvocationTargetException
	 *                     setter     
	 */
	public Map convertBean2Map(String qualifier, C bean)
			throws IntrospectionException, IllegalAccessException, InvocationTargetException {

		Class type = bean.getClass();
		Map returnMap = new HashMap();
		BeanInfo beanInfo = Introspector.getBeanInfo(type);

		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (int i = 0; i < propertyDescriptors.length; i++) {
			PropertyDescriptor descriptor = propertyDescriptors[i];
			String propertyName = descriptor.getName();
			if (!propertyName.equals("class")) {
				Method readMethod = descriptor.getReadMethod();
				Object result = readMethod.invoke(bean, new Object[0]);
				if (!StringUtils.isBlank(qualifier)) {
					returnMap.put(qualifier + ":" + propertyName, result);
				} else {
					returnMap.put(propertyName, result);
				}
				/*
				 * * if (result != null) {
				 * returnMap.put(qualifier+":"+propertyName, result); } else {
				 * returnMap.put(qualifier+":"+propertyName, ""); }
				 */
			}
		}
		return returnMap;
	}

 
 
 
//Map    javaBean


public static Object convertMap2Bean(Class type, Map map)
			throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {
		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();

			String propertyType = descriptor.getPropertyType().toString();

			if (map.containsKey(propertyName)) {

				//        try   ,                         。
				Object value = null;
				String key = propertyName;
				String valueStr = map.get(key);
				if (StringUtil.isNull(valueStr)) {
					continue;
				}
				//       
				if (propertyType.equals("int")) {
					value = Integer.parseInt(valueStr);
				} else if (propertyType.equals("boolean")) {
					value = Boolean.parseBoolean(valueStr);
				} else if (propertyType.equals("float")) {
					value = Float.parseFloat(valueStr);
				} else if (propertyType.equals("double")) {
					value = Double.parseDouble(valueStr);
				} else if (propertyType.equals("long")) {
					value = Long.parseLong(map.get(key));
				} else if (propertyType.equals("class java.lang.Integer")) {
					value = Integer.parseInt(valueStr);
				} else if (propertyType.equals("class java.lang.Long")) {
					value = new Long(valueStr);
				} else if (propertyType.equals("interface java.util.List")) {
					continue;
				} else if (propertyType.equals("class java.lang.String")) {
					value = map.get(key);
				} else if (propertyType.equals("interface java.util.Map")) {
					value = valueStr;
					// Map data = new HashMap();
					value = JSONObject.fromObject(value);
				} else {
					value = map.get(propertyName);
				}

				Object[] args = new Object[1];
				args[0] = value;

				try {
					descriptor.getWriteMethod().invoke(obj, args);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}

		}
		return obj;
	}