Java tutorial
/** * The Clican-Pluto software suit is Copyright 2009, Clican Company * and individual contributors, and is licensed under the GNU LGPL. * * @author wezhang * */ package com.clican.pluto.common.util; import java.beans.PropertyDescriptor; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.apache.commons.beanutils.PropertyUtilsBean; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class BeanUtils { private final static Log log = LogFactory.getLog(BeanUtils.class); public static Object getProperty(Object obj, String propertyName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method method = null; try { method = obj.getClass().getMethod(StringUtils.getGetMethodName(propertyName), new Class[] {}); } catch (NoSuchMethodException e) { method = obj.getClass().getMethod(StringUtils.getIsMethodName(propertyName), new Class[] {}); } return method.invoke(obj, new Object[] {}); } @SuppressWarnings("unchecked") public static Collection<Serializable> getCollectionProperty(Object obj, String propertyName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { return (Collection<Serializable>) getProperty(obj, propertyName); } public static Calendar getCalendarProperty(Object obj, String propertyName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { return (Calendar) getProperty(obj, propertyName); } public static void setProperty(Object obj, String propertyName, Object propertyValue) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method method = obj.getClass().getMethod(StringUtils.getSetMethodName(propertyName), new Class[] { propertyValue.getClass() }); method.invoke(obj, new Object[] { propertyValue }); } public static void setCollectionProperty(Object obj, String propertyName, Collection<?> coll) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method method = obj.getClass().getMethod(StringUtils.getSetMethodName(propertyName), new Class[] { Collection.class }); method.invoke(obj, new Object[] { coll }); } /** * list ?? Map<String,Object>listmapkey??,value * <p> * Useridname, beanList10UserList<Map> 10,?Map?keyid,name, value. * </p> * * @param <v> * @param beanList ?? * @return List */ public static <V> List<Map<String, Object>> convertBeanToMapList(List<V> beanList) { if (beanList == null || beanList.size() < 1) { return null; } // ? List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); for (V bean : beanList) { result.add(convertBeanToMap(bean)); } return result; } /** * ??Mapmapkey??,value * <p> * bean id name 100 zhangsan ? id=100,name="zhangsan"Map * </p> * * @param <Bean> * @param bean * @return */ public static <Bean> Map<String, Object> convertBeanToMap(Bean bean) { if (bean == null) return null; PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); PropertyDescriptor[] psArray = propertyUtilsBean.getPropertyDescriptors(bean.getClass()); List<String> properties = new ArrayList<String>(); for (PropertyDescriptor ps : psArray) { properties.add(ps.getName()); } Map<String, Object> map = new HashMap<String, Object>(); for (String property : properties) { // ?class?map if ("class".equals(property)) { continue; } String methodName = StringUtils.getGetMethodName(property); try { Method method = bean.getClass().getMethod(methodName, new Class<?>[] {}); Object propertyValue = method.invoke(bean, new Object[] {}); if (propertyValue != null) { map.put(property, propertyValue); } } catch (Exception e) { log.error("", e); } } return map; } /** * List???List * <p> * Username?List convertToList(List<User> values, "name") List<String> * </p> * * @param <K> * @param <V> * @param values * @param propertyName * @return propertyName?List */ @SuppressWarnings("unchecked") public static <K, V> List<K> convertToList(List<V> values, String propertyName) { List<K> result = new ArrayList<K>(); if (values == null) { return result; } for (V v : values) { String methodName = StringUtils.getGetMethodName(propertyName); try { Method method = v.getClass().getMethod(methodName, new Class<?>[] {}); K k = (K) method.invoke(v, new Object[] {}); if (k != null) { result.add(k); } } catch (Exception e) { log.error("", e); } } return result; } /** * List???Set * <p> * Username?Set convertToSet(List<User> values, "name") Set<String> * </p> * * @param <K> * @param <V> * @param values * @param propertyName * @return propertyName?Set */ @SuppressWarnings("unchecked") public static <K, V> Set<K> convertToSet(List<V> values, String propertyName) { Set<K> result = new HashSet<K>(); if (values == null) { return result; } for (V v : values) { String methodName = StringUtils.getGetMethodName(propertyName); try { Method method = v.getClass().getMethod(methodName, new Class<?>[] {}); K k = (K) method.invoke(v, new Object[] {}); if (k != null) { result.add(k); } } catch (Exception e) { log.error("", e); } } return result; } /** * List???Map, keyvalue * <p> * convertToMap(List<User> values, "name")nameKey,UserMap * </p> * * @param <K> * @param <V> * @param values * @param propertyName * @return propertyNamekey?Map */ @SuppressWarnings("unchecked") public static <K, V> Map<K, V> convertToMap(List<V> values, String propertyName) { Map<K, V> result = new HashMap<K, V>(); if (values == null) { return result; } for (V v : values) { if (propertyName.indexOf(".") > 0) { String[] propertyNameList = propertyName.split("\\."); try { K k = (K) getPropertyByMthodName(v, propertyNameList); if (k != null) { if (!result.containsKey(k)) { result.put(k, v); } } } catch (Exception e) { log.error("", e); } } else { String methodName = StringUtils.getGetMethodName(propertyName); try { Method method = v.getClass().getMethod(methodName, new Class<?>[] {}); K k = (K) method.invoke(v, new Object[] {}); if (k != null) { result.put(k, v); } } catch (Exception e) { log.error("", e); } } } return result; } /** * List???Map, keyvalue?List * <p> * convertToMapAndListList<User> value, "name"UsernamekeyMap, name ??List. * </p> * * @param <K> * @param <V> * @param values * @param propertyName * @return propertyNamekeyMap,?key?List. */ @SuppressWarnings("unchecked") public static <K, V> Map<K, List<V>> convertToMapAndList2(List<V> values, String propertyName) { Map<K, List<V>> result = new HashMap<K, List<V>>(); if (values == null) { return result; } for (V v : values) { String methodName = null; if (propertyName.indexOf(".") > 0) { String[] propertyNameList = propertyName.split("\\."); try { K k = (K) getPropertyByMthodName(v, propertyNameList); if (k != null) { if (!result.containsKey(k)) { result.put(k, new ArrayList<V>()); } result.get(k).add(v); } } catch (Exception e) { log.error("", e); } } else { methodName = StringUtils.getGetMethodName(propertyName); try { Method method = v.getClass().getMethod(methodName, new Class<?>[] {}); K k = (K) method.invoke(v, new Object[] {}); if (k != null) { if (!result.containsKey(k)) { result.put(k, new ArrayList<V>()); } result.get(k).add(v); } } catch (Exception e) { log.error("", e); } } } return result; } /** * List???Map, keyvalue?List * <p> * convertToMapAndListList<User> value, "name"UsernamekeyMap, name ??List. * </p> * * @param <K> * @param <V> * @param values * @param propertyName * @return propertyNamekeyMap,?key?List. */ @SuppressWarnings("unchecked") public static <K, V> Map<K, List<V>> convertToMapAndList2(List<V> values, String propertyName, Boolean sorted) { Map<K, List<V>> result = sorted ? new TreeMap<K, List<V>>() : new HashMap<K, List<V>>(); if (values == null) { return result; } for (V v : values) { String methodName = null; if (propertyName.indexOf(".") > 0) { String[] propertyNameList = propertyName.split("\\."); try { K k = (K) getPropertyByMthodName(v, propertyNameList); if (k != null) { if (!result.containsKey(k)) { result.put(k, new ArrayList<V>()); } result.get(k).add(v); } } catch (Exception e) { log.error("", e); } } else { methodName = StringUtils.getGetMethodName(propertyName); try { Method method = v.getClass().getMethod(methodName, new Class<?>[] {}); K k = (K) method.invoke(v, new Object[] {}); if (k != null) { if (!result.containsKey(k)) { result.put(k, new ArrayList<V>()); } result.get(k).add(v); } } catch (Exception e) { log.error("", e); } } } return result; } @SuppressWarnings("unchecked") public static <K> Object getPropertyByMthodName(Object obj, String[] propertyNameList) { if (null == propertyNameList || propertyNameList.length < 1) { return (K) obj; } try { K result = null; String propertyName = propertyNameList[0]; Method method = obj.getClass().getMethod(StringUtils.getGetMethodName(propertyName), new Class<?>[] {}); if (propertyNameList.length > 1) { Object newObj = method.invoke(obj, new Object[] {}); String[] newPropertyNameList = new String[propertyNameList.length - 1]; System.arraycopy(propertyNameList, 1, newPropertyNameList, 0, propertyNameList.length - 1); return getPropertyByMthodName(newObj, newPropertyNameList); } else { result = (K) method.invoke(obj, new Object[] {}); } return result; } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * List???Map, keyvalue?List * <p> * convertToMapAndListList<User> value, "name"UsernamekeyMap, name ??List. * </p> * * @param <K> * @param <V> * @param values * @param propertyName * @return propertyNamekeyMap,?key?List. */ @SuppressWarnings("unchecked") public static <K, V> Map<K, List<V>> convertToMapAndList(List<V> values, String propertyName) { Map<K, List<V>> result = new HashMap<K, List<V>>(); if (values == null) { return result; } for (V v : values) { String methodName = StringUtils.getGetMethodName(propertyName); try { Method method = v.getClass().getMethod(methodName, new Class<?>[] {}); K k = (K) method.invoke(v, new Object[] {}); if (k != null) { if (!result.containsKey(k)) { result.put(k, new ArrayList<V>()); } result.get(k).add(v); } } catch (Exception e) { log.error("", e); } } return result; } /** * List???Map, keyvalue?List,?key? * <p> * convertToMapAndListList<User> value, "name"UsernamekeyMap, name ??List. * </p> * * @param <K> * @param <V> * @param values * @param propertyName * @param sorted ?? * @return */ @SuppressWarnings("unchecked") public static <K, V> Map<K, List<V>> convertToMapAndList(List<V> values, String propertyName, boolean sorted) { Map<K, List<V>> result = sorted ? new TreeMap<K, List<V>>() : new HashMap<K, List<V>>(); if (values == null) { return result; } for (V v : values) { String methodName = StringUtils.getGetMethodName(propertyName); try { Method method = v.getClass().getMethod(methodName, new Class<?>[] {}); K k = (K) method.invoke(v, new Object[] {}); if (k != null) { if (!result.containsKey(k)) { result.put(k, new ArrayList<V>()); } result.get(k).add(v); } } catch (Exception e) { log.error("", e); } } return result; } /** * List??Map,keypropertyName1, propertyName2keyMap * <p> * convertToMutilMapList<User> values, "id", "name"idkeyMap, namekeyMap. * </p> * * @param <K1> * @param <K2> * @param <V> * @param values * @param propertyName1 * @param propertyName2 * @return */ @SuppressWarnings("unchecked") public static <K1, K2, V> Map<K1, Map<K2, V>> convertToMutilMap(List<V> values, String propertyName1, String propertyName2) { Map<K1, Map<K2, V>> result = new HashMap<K1, Map<K2, V>>(); if (values == null) { return result; } for (V v : values) { String methodName1 = StringUtils.getGetMethodName(propertyName1); String methodName2 = StringUtils.getGetMethodName(propertyName2); try { Method method1 = v.getClass().getMethod(methodName1, new Class<?>[] {}); K1 k1 = (K1) method1.invoke(v, new Object[] {}); if (k1 != null) { if (!result.containsKey(k1)) { result.put(k1, new HashMap<K2, V>()); } Method method2 = v.getClass().getMethod(methodName2, new Class<?>[] {}); K2 k2 = (K2) method2.invoke(v, new Object[] {}); if (k2 != null) { if (!result.get(k1).containsKey(k2)) { result.get(k1).put(k2, v); } } } } catch (Exception e) { log.error("", e); } } return result; } /** * List??Map,keypropertyName1, propertyName2keyMap * <p> * convertToMutilMapList<User> values, "id", "name"idkeyMap, * namekeyMap. * </p> * * @param <K1> * @param <K2> * @param <V> * @param values * * @param propertyName1 * * @param propertyName2 * * @param sorted * @return */ @SuppressWarnings("unchecked") public static <K1, K2, V> Map<K1, Map<K2, V>> convertToMutilMap(List<V> values, String propertyName1, String propertyName2, boolean sorted) { Map<K1, Map<K2, V>> result = sorted ? new TreeMap<K1, Map<K2, V>>() : new HashMap<K1, Map<K2, V>>(); if (values == null) { return result; } for (V v : values) { String methodName1 = StringUtils.getGetMethodName(propertyName1); String methodName2 = StringUtils.getGetMethodName(propertyName2); try { Method method1 = v.getClass().getMethod(methodName1, new Class<?>[] {}); K1 k1 = (K1) method1.invoke(v, new Object[] {}); if (k1 != null) { if (!result.containsKey(k1)) { result.put(k1, new HashMap<K2, V>()); } Method method2 = v.getClass().getMethod(methodName2, new Class<?>[] {}); K2 k2 = (K2) method2.invoke(v, new Object[] {}); if (k2 != null) { if (!result.get(k1).containsKey(k2)) { result.get(k1).put(k2, v); } } } } catch (Exception e) { log.error("", e); } } return result; } } // $Id$