Bean处理
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletRequest;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.log4j.Logger;
public class BeanUtil {
private static Logger logger = Logger.getLogger(BeanUtil.class);
private BeanUtil() {
}
public static <T> T createBean(Class<T> c, ServletRequest request)
throws Exception {
return createBean(c, request.getParameterMap());
}
public static <T> T createBean(Class<T> c, Map<?, ?> properties)
throws Exception {
try {
T t = c.newInstance();
BeanUtil.getBeanUtilsBean().populate(t, properties);
return t;
} catch (Exception e) {
logger.debug("错误:", e);
throw new Exception("数据处理失败!");
}
}
public static void copyProperties(Object dest, Object orig)
throws Exception {
try {
BeanUtil.getBeanUtilsBean().copyProperties(dest, orig);
} catch (Exception e) {
logger.debug("错误:", e);
throw new Exception("数据处理失败!");
}
}
public static BeanUtilsBean getBeanUtilsBean() {
DateConverter dc = new DateConverter();
ConvertUtilsBean cub = new ConvertUtilsBean();
cub.register(dc, java.util.Date.class);
cub.register(dc, java.sql.Date.class);
cub.register(dc, java.sql.Time.class);
cub.register(dc, java.sql.Timestamp.class);
PropertyUtilsBean pub = new PropertyUtilsBean();
BeanUtilsBean utils = new BeanUtilsBean(cub, pub);
return utils;
}
/**
* 获取指定类的所有属性
*
* @param c
* 要获取属性的JavaBean类
* @return 类的所有属性集合
*/
public static List<String> getBeanProperty(Class<?> c) {
ArrayList<String> list = new ArrayList<String>();
try {
PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(c);
for (PropertyDescriptor pd : pds) {
String name = pd.getName();
if (!name.equals("class")) {
list.add(pd.getName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* 获取指定类的所有可读属性方法集合,键值为属性名称。
*
* @param c
* 要获取属性的JavaBean类
* @return 类的所有可读属性方法集合
*/
public static Map<String, Method> getBeanGetter(Class<?> c) {
LinkedHashMap<String, Method> map = new LinkedHashMap<String, Method>();
try {
PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(c);
for (PropertyDescriptor pd : pds) {
String name = pd.getName();
Method method = pd.getReadMethod();
if (!name.equals("class") && method != null) {
map.put(name, method);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* 获取指定类的所有可写属性方法集合,键值为属性名称。
*
* @param c
* 要获取属性的JavaBean类
* @return 类的所有可写属性方法集合
*/
public static Map<String, Method> getBeanSetter(Class<?> c) {
LinkedHashMap<String, Method> map = new LinkedHashMap<String, Method>();
try {
PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(c);
for (PropertyDescriptor pd : pds) {
String name = pd.getName();
Method method = pd.getWriteMethod();
if (method != null) {
map.put(name, method);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* 获取指定对象的字符串表达式(输出所有字段的字符表达式)
*
* @param o
* 要输出信息的对象
*/
public static String getBeanInfoString(Object o) {
StringBuilder sb = new StringBuilder();
Map<String, Method> map = getBeanGetter(o.getClass());
for (String name : map.keySet()) {
try {
sb.append(name + ":" + map.get(name).invoke(o) + ",");
} catch (Exception e) {
e.printStackTrace();
}
}
if (sb.indexOf(",") != -1) {
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}
public static boolean classIsType(String className, Class<?> c)
throws Exception {
Class<?> clazz = Class.forName(className);
if (clazz.equals(c)) {
return true;
}
Class<?>[] classes = clazz.getInterfaces();
for (Class<?> ic : classes) {
if (ic.equals(c)) {
return true;
}
}
Class<?> sc = clazz.getSuperclass();
if (sc == null) {
return false;
}
if (sc.equals(c)) {
return true;
} else if (classIsType(sc.getName(), c)) {
return true;
}
return false;
}
}