package a;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class BeanMapUtils {
private static Log logger = LogFactory.getLog(BeanMapUtils.class);
public BeanMapUtils() {
}
public static Object getMap2Bean(Map map, Class<?> beanClass) {
Object inst = null;
try {
Field[] fields = beanClass.getDeclaredFields();
Constructor<?> constructor1 = beanClass.getConstructor(new Class[0]);
inst = constructor1.newInstance(new Object[0]);
for (Field field : fields) {
if (!field.getName().equals("serialVersionUID")) {
try {
if (map.get(field.getName()) != null) {
if (("Long".equals(field.getType().getSimpleName()))
|| ("long".equals(field.getType().getSimpleName()))) {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString()) ? null
: Long.valueOf(Long.parseLong(map.get(field.getName()).toString())) });
} else if ("Date".equals(field.getType().getSimpleName())) {
if ("java.util.Date".equals(field.getType())) {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString())
? null
: DateUtil.formatDate(map.get(field.getName()).toString()) });
} else {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString())
? null
: DateUtil.formatDate(map.get(field.getName()).toString()) });
}
} else if (("Integer".equals(field.getType().getSimpleName()))
|| ("int".equals(field.getType().getSimpleName()))) {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString()) ? null
: Integer.valueOf(
Integer.parseInt(map.get(field.getName()).toString())) });
} else if ("Double".equals(field.getType().getSimpleName())) {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString()) ? null
: Double.valueOf(
Double.parseDouble(map.get(field.getName()).toString())) });
} else if ("List".equals(field.getType().getSimpleName())) {
method.invoke(inst, new Object[] {
map.get(field.getName()) == null ? null : (List) map.get(field.getName()) });
} else if ("Map".equals(field.getType().getSimpleName())) {
method.invoke(inst, new Object[] {
map.get(field.getName()) == null ? null : (Map) map.get(field.getName()) });
} else if ("String".equals(field.getType().getSimpleName())) {
if (map.get(field.getName()).equals("")) {
method.invoke(inst, new Object[] { "" });
} else {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString())
? null : map.get(field.getName()) });
}
} else {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString()) ? null
: map.get(field.getName()) });
}
}
} catch (Exception e) {
logger.error(e);
}
}
}
} catch (Exception e) {
logger.error(e);
}
return inst;
}
public static Map getBean2Map(Object beanObj) {
Map map = new HashMap();
if (beanObj == null)/* 80 */ return map;
try {
Class<?> classType = beanObj.getClass();
Field[] fields = classType.getDeclaredFields();
for (Field field : fields) {
if (!field.getName().equals("serialVersionUID")) {
try {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), beanObj.getClass());
Method method = pd.getReadMethod();
Object value = method.invoke(beanObj, new Object[0]);
if (value != null) {
map.put(field.getName(), value);
} else if (("Long".equals(field.getType().getSimpleName()))
|| ("Integer".equals(field.getType().getSimpleName()))) {
map.put(field.getName(), Integer.valueOf(-1));
} else {
map.put(field.getName(), "");
}
} catch (Exception e) {
logger.error(e);
}
}
}
} catch (Exception e) {
logger.error(e);
}
return map;
}
}
package a;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
public DateUtil() {
}
public static Date formatDate(String date) {
SimpleDateFormat simple = null;
switch (date.trim().length()) {
case 19:
simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
break;
case 10:
simple = new SimpleDateFormat("yyyy-MM-dd");
break;
case 8:
simple = new SimpleDateFormat("HH:mm:ss");
break;
}
try {
return simple.parse(date.trim());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
package a;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class HHSYBeanMapUtils extends BeanMapUtils {
private static Log logger = LogFactory.getLog(HHSYBeanMapUtils.class);
/**
*
* 有多次继承属性的javaBean转Map;
*
* @param beanObj
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map HHSYgetBean2Map(Object beanObj)
{
Map map = new HashMap();
if (beanObj == null)
return map;
try {
Class<?> classType = beanObj.getClass();
Field[] fields = classType.getDeclaredFields();
do{
for (Field field : fields) {
if (!field.getName().equals("serialVersionUID")){
try{
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), beanObj.getClass());
Method method = pd.getReadMethod();
Object value = method.invoke(beanObj, new Object[0]);
if (value != null) {
map.put(field.getName(), value);
}else if (("Long".equals(field.getType().getSimpleName())) || ("Integer".equals(field.getType().getSimpleName()))) {
map.put(field.getName(), Integer.valueOf(-1));
} else {
map.put(field.getName(), "");
}
}catch (Exception e) {
logger.error(e);
}
}
}
classType = classType.getSuperclass();
fields = classType.getDeclaredFields();
}while(!classType.getName().equals("java.lang.Object"));//最高父类Object
} catch (Exception e) {
logger.error(e);
}
return map;
}
/**
*
* List<?>转List<Map<Object,Object>>
* @param beanList
* @return
* @throws Exception
*/
public static List<Map<Object,Object>> convertListBean2ListMap(List<?> beanList) throws Exception {
List<Map<Object,Object>> mapList = new ArrayList<Map<Object,Object>>();
if(beanList!=null && beanList.size()>0){
for(int i=0, n=beanList.size(); i<n; i++){
Object bean = beanList.get(i);
Map map = HHSYgetBean2Map(bean);
mapList.add(map);
}
}
return mapList;
}
}