123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620 |
- package com.behosoft.util;
- import java.beans.PropertyDescriptor;
- import java.io.PrintStream;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.lang.reflect.ParameterizedType;
- import java.lang.reflect.Type;
- import java.lang.reflect.TypeVariable;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
- import org.apache.commons.beanutils.BeanUtilsBean;
- import org.apache.commons.beanutils.DynaBean;
- import org.apache.commons.beanutils.MethodUtils;
- import org.apache.commons.beanutils.NestedNullException;
- import org.apache.commons.beanutils.PropertyUtils;
- import com.behosoft.framework.web.model.RecordBO;
- /**
- * <pre></pre>
- *
- * <br>
- * JDK版本:1.6
- *
- * @author 刘俊杰
- * @version 1.0
- * @see The author for more details
- * @since 1.0
- */
- public class BeanUtils {
- public static Object getStaticProperty(Class clazz, String name) {
- try {
- return clazz.getField(name).get(null);
- } catch (IllegalArgumentException e) {
- throw new BeanAccessException("在读取类[" + clazz + "]的静态属性[" + name
- + "]时遇到错误", e);
- } catch (SecurityException e) {
- throw new BeanAccessException("在读取类[" + clazz + "]的静态属性[" + name
- + "]时遇到错误", e);
- } catch (IllegalAccessException e) {
- throw new BeanAccessException("在读取类[" + clazz + "]的静态属性[" + name
- + "]时遇到错误", e);
- } catch (NoSuchFieldException e) {
- throw new BeanAccessException("在读取类[" + clazz + "]的静态属性[" + name
- + "]时遇到错误", e);
- }
- }
- public static Object getProperty(Object bean, String name) {
- try {
- return PropertyUtils.getProperty(bean, name);
- } catch (NestedNullException e) {
- return null;
- } catch (IllegalAccessException e) {
- throw new BeanAccessException("在读取bean[" + bean + "]的属性[" + name
- + "]时遇到错误", e);
- } catch (InvocationTargetException e) {
- throw new BeanAccessException("在读取bean[" + bean + "]的属性[" + name
- + "]时遇到错误", e);
- } catch (NoSuchMethodException e) {
- throw new BeanAccessException("在读取bean[" + bean + "]的属性[" + name
- + "]时遇到错误", e);
- }
- }
- public static void setProperty(Object bean, String name, Object value) {
- try {
- PropertyUtils.setProperty(bean, name, value);
- } catch (IllegalAccessException e) {
- throw new BeanAccessException("在将属性[" + name + "]写入bean[" + bean
- + "]时遇到错误", e);
- } catch (InvocationTargetException e) {
- throw new BeanAccessException("在将属性[" + name + "]写入bean[" + bean
- + "]时遇到错误", e);
- } catch (NoSuchMethodException e) {
- throw new BeanAccessException("在将属性[" + name + "]写入bean[" + bean
- + "]时遇到错误", e);
- }
- }
- public static void setSimpleProperty(Object bean, String name, Object value) {
- try {
- PropertyUtils.setSimpleProperty(bean, name, value);
- } catch (IllegalAccessException e) {
- throw new BeanAccessException("在将属性[" + name + "]写入bean[" + bean
- + "]时遇到错误", e);
- } catch (InvocationTargetException e) {
- throw new BeanAccessException("在将属性[" + name + "]写入bean[" + bean
- + "]时遇到错误", e);
- } catch (NoSuchMethodException e) {
- throw new BeanAccessException("在将属性[" + name + "]写入bean[" + bean
- + "]时遇到错误", e);
- }
- }
- /**
- * 获取bean的指定名称的属性,如果不存在指定的属性,返回null
- *
- * @param bean
- * @param name
- * @return
- */
- public static Object getPropertyIf(Object bean, String name) {
- if (hasProperty(bean, name)) {
- return getProperty(bean, name);
- } else
- return null;
- }
- public static Object getSimpleProperty(Object bean, String name) {
- try {
- return PropertyUtils.getSimpleProperty(bean, name);
- } catch (IllegalAccessException e) {
- throw new BeanAccessException("在读取bean[" + bean + "]的属性[" + name
- + "]时遇到错误", e);
- } catch (InvocationTargetException e) {
- throw new BeanAccessException("在读取bean[" + bean + "]的属性[" + name
- + "]时遇到错误", e);
- } catch (NoSuchMethodException e) {
- throw new BeanAccessException("在读取bean[" + bean + "]的属性[" + name
- + "]时遇到错误", e);
- }
- }
- /**
- * 返回bean是否有指定的属性
- *
- * @param bean
- * @param name
- * @return
- */
- public static boolean hasProperty(Object bean, String name) {
- if (bean == null || StringUtils.isBlank(name))
- return false;
- return getPropertyDescriptor(bean.getClass(), name) != null;
- }
- /**
- * 调用指定的方法
- *
- * @param object
- * @param methodName
- * @param args
- * 方法参数,不能用null值
- * @return
- */
- public static Object invokeMethod(Object object, String methodName,
- Object[] args, Class[] parameterTypes) {
- try {
- // 试着将参数转换成合法的参数类型
- if (args != null && args.length > 0 && parameterTypes != null
- && parameterTypes.length > 0
- && args.length == parameterTypes.length) {
- Object[] args2 = new Object[args.length];
- for (int i = 0; i < args.length; i++) {
- args2[i] = DataUtils.as(args[i], parameterTypes[i]);
- }
- args = args2;
- }
- return MethodUtils.invokeMethod(object, methodName, args,
- parameterTypes);
- } catch (NoSuchMethodException e) {
- throw new BeanAccessException("调用bean[" + object + "的方法["
- + methodName + "]时遇到一个错误", e);
- } catch (IllegalAccessException e) {
- throw new BeanAccessException("调用bean[" + object + "的方法["
- + methodName + "]时遇到一个错误", e);
- } catch (InvocationTargetException e) {
- throw new BeanAccessException("调用bean[" + object + "的方法["
- + methodName + "]时遇到一个错误", e);
- }
- }
- public static PropertyDescriptor getPropertyDescriptor(Class beanClass,
- String proName) {
- return org.springframework.beans.BeanUtils.getPropertyDescriptor(
- beanClass, proName);
- }
- public static PropertyDescriptor[] getPropertyDescriptors(Class beanClass) {
- return org.springframework.beans.BeanUtils
- .getPropertyDescriptors(beanClass);
- }
- /**
- * 返回一个bean的Map形式
- *
- * @param bean
- * @param ignoreProperties
- * 要忽略的属性名
- * @return
- */
- public static Map<String, Object> describe(Object bean,
- String[] ignoreProperties) {
- if (bean == null)
- return null;
- if (bean instanceof DynaBean) {
- try {
- return BeanUtilsBean.getInstance().describe(bean);
- } catch (IllegalAccessException e) {
- throw new BeanAccessException("在将Bean[" + bean
- + "]转换成Map形式时遇到一个错误:", e);
- } catch (InvocationTargetException e) {
- throw new BeanAccessException("在将Bean[" + bean
- + "]转换成Map形式时遇到一个错误:", e);
- } catch (NoSuchMethodException e) {
- throw new BeanAccessException("在将Bean[" + bean
- + "]转换成Map形式时遇到一个错误:", e);
- }
- }
- Map<String, Object> description = new HashMap<String, Object>();
- PropertyDescriptor[] descriptors = BeanUtilsBean.getInstance()
- .getPropertyUtils().getPropertyDescriptors(bean);
- Class clazz = bean.getClass();
- for (int i = 0; i < descriptors.length; i++) {
- String name = descriptors[i].getName();
- boolean ignore = false;
- if (ignoreProperties != null) {
- for (String ignorePro : ignoreProperties) {
- if (name.equals(ignorePro)) {
- ignore = true;
- break;
- }
- }
- }
- if (ignore)
- continue;
- if (MethodUtils.getAccessibleMethod(clazz,
- descriptors[i].getReadMethod()) != null) {
- description.put(name, getSimpleProperty(bean, name));
- }
- }
- return description;
- }
- /**
- * 返回一个bean的Map形式
- *
- * @param bean
- * @return
- */
- public static Map<String, Object> describe(Object bean) {
- return describe(bean, null);
- }
- public static void printBean(PrintStream ps, Object bean,
- String... ignoreProperties) {
- ps.println("start printBean:" + bean.getClass());
- if (bean == null) {
- ps.println("null");
- return;
- }
- Map<String, Object> map = describe(bean, ignoreProperties);
- for (Map.Entry<String, Object> ent : map.entrySet()) {
- ps.println(ent.getKey() + ": " + ent.getValue());
- }
- ps.println("end printBean:" + bean.getClass());
- }
- public static void printBean(Object bean, String... ignoreProperties) {
- printBean(System.out, bean, ignoreProperties);
- }
- /**
- * 打印两个bean不同的属性
- *
- * @param ps
- * @param bean1
- * @param bean2
- * @param ignoreProperties
- */
- public static void printCompareBeans(PrintStream ps, Object bean1,
- Object bean2, String... ignoreProperties) {
- ps.println("start compare " + bean1.getClass() + "," + bean2.getClass());
- Map<String, Object> map1 = describe(bean1, ignoreProperties);
- Map<String, Object> map2 = describe(bean2, ignoreProperties);
- Set<String> keySet1 = map1.keySet();
- Set<String> keySet2 = map2.keySet();
- for (String key : keySet1) {
- Object value1 = map1.get(key);
- Object value2 = map2.get(key);
- if (!equals(value1, value2)) {
- ps.println("key=" + key);
- ps.println("value1=" + value1);
- ps.println("value2=" + value2);
- }
- keySet2.remove(key);
- }
- for (String key : keySet2) {
- Object value1 = map1.get(key);
- Object value2 = map2.get(key);
- if (!equals(value1, value2)) {
- ps.println("key=" + key);
- ps.println("value1=" + value1);
- ps.println("value2=" + value2);
- }
- }
- ps.println("end compare " + bean1.getClass() + "," + bean2.getClass());
- }
- /**
- * 打印两个bean不同的属性
- *
- * @param bean1
- * @param bean2
- * @param ignoreProperties
- */
- public static void printCompareBeans(Object bean1, Object bean2,
- String... ignoreProperties) {
- printCompareBeans(System.out, bean1, bean2, ignoreProperties);
- }
- public static boolean equals(Object obj1, Object obj2) {
- if (obj1 == null) {
- return obj2 == null;
- }
- return obj1.equals(obj2);
- }
- /**
- * 返回类对于指定超类或接口实现的类型参数数组
- *
- * <pre>
- * 例如有如下定义:
- * class A<T>{}
- *
- * interface IA<T1,T2>{}
- *
- * class B extends A<String> implements IA<Double,Integer>{}
- *
- * class C<T1,T2> extends A<T1> implements IA<T2,Integer>{}
- *
- * class C2 extends C<Double,String>{}
- *
- * class D extends C{}
- *
- * getTypeArguments(B.class,A.class) 返回[String.class]
- * getTypeArguments(B.class,IA.class) 返回[Double.class,Integer.class]
- * getTypeArguments(C2.class,A.class) 返回[Double.class]
- * getTypeArguments(C2.class,IA.class) 返回[String.class,Integer.class]
- * getTypeArguments(C.class,IA.class) 返回[null,Integer.class]
- * getTypeArguments(D.class,C.class) 返回null
- * </pre>
- *
- * @param cls
- * @param superType
- * @return
- */
- @SuppressWarnings("unchecked")
- public static Type[] getTypeArguments(Class cls, Class superType) {
- if (superType.isAssignableFrom(cls))
- return searchTypeArguments(cls, cls, superType);
- else
- return null;
- }
- @SuppressWarnings("unchecked")
- private static Type[] searchTypeArguments(Class rootCls, Class cls,
- Class superType) {
- Type[] sts = getAllGenericSuperTypes(cls);
- for (Type st : sts) {
- if (superType == st) {
- } else if (st instanceof ParameterizedType
- && ((ParameterizedType) st).getRawType() == superType) {
- Type[] rs = ((ParameterizedType) st).getActualTypeArguments();
- for (int i = 0; i < rs.length; i++) {
- if (rs[i] instanceof TypeVariable) {
- rs[i] = getTypeArgumentsForTypeVariable(rootCls, cls,
- (TypeVariable) rs[i]);
- }
- }
- return rs;
- }
- }
- for (Type st : sts) {
- Type[] rs = null;
- if (st instanceof Class) {
- rs = searchTypeArguments(rootCls, (Class) st, superType);
- } else if (st instanceof ParameterizedType) {
- rs = searchTypeArguments(rootCls,
- (Class) (((ParameterizedType) st).getRawType()),
- superType);
- }
- if (rs != null)
- return rs;
- }
- return null;
- }
- @SuppressWarnings("unchecked")
- private static Type getTypeArgumentsForTypeVariable(Class cls,
- Class superType, TypeVariable typeVariable) {
- int k = -1;
- for (int i = 0; i < superType.getTypeParameters().length; i++) {
- if (typeVariable == superType.getTypeParameters()[i]) {
- k = i;
- break;
- }
- }
- if (k >= superType.getTypeParameters().length || k < 0) {
- throw new RuntimeException(typeVariable + "不是" + superType
- + "的类型参数变量");
- }
- return searchTypeArgumentsForTypeVariable(cls, cls, superType, k);
- }
- @SuppressWarnings("unchecked")
- private static Type searchTypeArgumentsForTypeVariable(Class rootCls,
- Class cls, Class superType, int i) {
- Type[] sts = getAllGenericSuperTypes(cls);
- for (Type st : sts) {
- if (superType == st) {
- } else if (st instanceof ParameterizedType
- && ((ParameterizedType) st).getRawType() == superType) {
- Type t = ((ParameterizedType) st).getActualTypeArguments()[i];
- if (t instanceof TypeVariable) {
- Type t2 = getTypeArgumentsForTypeVariable(rootCls, cls,
- (TypeVariable) t);
- if (t2 != null)
- t = t2;
- }
- return t;
- }
- }
- for (Type st : sts) {
- Type r = null;
- if (st instanceof Class) {
- r = searchTypeArgumentsForTypeVariable(rootCls, (Class) st,
- superType, i);
- } else if (st instanceof ParameterizedType) {
- r = searchTypeArgumentsForTypeVariable(rootCls,
- (Class) (((ParameterizedType) st).getRawType()),
- superType, i);
- }
- if (r != null)
- return r;
- }
- return null;
- }
- private static Type[] getAllGenericSuperTypes(Class cls) {
- Type[] superTypes;
- Type[] ts = cls.getGenericInterfaces();
- if (ts != null && ts.length > 0) {
- superTypes = new Type[ts.length + 1];
- System.arraycopy(ts, 0, superTypes, 1, ts.length);
- } else {
- superTypes = new Type[1];
- }
- superTypes[0] = cls.getGenericSuperclass();
- return superTypes;
- }
- public static boolean isSimpleValueType(Class clazz) {
- return org.springframework.beans.BeanUtils.isSimpleValueType(clazz);
- }
- // 测试getTypeArguments
- // class A<T> {
- // }
- //
- // interface IA<T1, T2> {
- // }
- //
- // class B extends A<String> implements IA<Double, Integer> {
- // }
- //
- // class C<T1, T2> extends A<T1> implements IA<T2, Integer> {
- // }
- //
- // class C2 extends C<Double, String> {
- // }
- //
- // class D extends C {
- //
- // }
- //
- // private static void test(Class cls, Class superCls) {
- // System.out.println("test:" + cls + "," + superCls);
- // Type[] ts = getTypeArguments(cls, superCls);
- // if (ts != null) {
- // String str = "[";
- // for (int i = 0; i < ts.length; i++) {
- // if (i > 0)
- // str += ",";
- // str += ts[i];
- // }
- // str += "]";
- // System.out.println(str);
- // }
- // else
- // System.out.println(ts);
- //
- // }
- //
- // public static void main(String[] args) {
- // test(B.class, A.class);
- // test(B.class, IA.class);
- // test(C2.class, A.class);
- // test(C2.class, IA.class);
- // test(C.class, IA.class);
- // test(IA.class, IA.class);
- // test(D.class, C.class);
- // }
- /**
- * 将一个JavaBean风格对象的属性值拷贝到另一个对象的同名属性中
- * (如果不存在同名属性的就不拷贝)
- * 本方法目前只为系统Bo转换为报文对象内部对象时的属性复制服务,其它功能请慎用。
- */
- @SuppressWarnings("unchecked")
- public static void copyProperties(Object target, Object source)
- throws Exception {
- /*
- * 分别获得源对象和目标对象的Class类型对象,Class对象是整个反射机制的源头和灵魂!
- *
- * Class对象是在类加载的时候产生,保存着类的相关属性,构造器,方法等信息
- */
- Class sourceClz = source.getClass();
- Class targetClz = target.getClass();
- // 得到Class对象所表征的类的所有属性(包括私有属性)
- Field[] fields = sourceClz.getDeclaredFields();
- for (int i = 0; i < fields.length; i++) {
- String fieldName = fields[i].getName();
- String targetFieldName = fieldName.toLowerCase();
- Field targetField = null;
- try {
- // 得到targetClz对象所表征的类的名为fieldName的属性,不存在就进入下次循环
- targetField = targetClz.getDeclaredField(targetFieldName);
- } catch (SecurityException e) {
- e.printStackTrace();
- break;
- } catch (NoSuchFieldException e) {
- continue;
- }
- // 判断sourceClz字段类型和targetClz同名字段类型是否相同
- if (fields[i].getType() == targetField.getType()
- || (fields[i].getType() == Long.class && targetField.getType() == String.class)
- || (fields[i].getType() == Integer.class && targetField.getType() == String.class)
- || (fields[i].getType() == Double.class && targetField.getType() == String.class)
- || (fields[i].getType() == Float.class && targetField.getType() == String.class)
- || (fields[i].getType() == Date.class && targetField.getType() == String.class)){
- // 由属性名字得到对应get和set方法的名字
- String getMethodName = "get"
- + fieldName.substring(0, 1).toUpperCase()
- + fieldName.substring(1);
- String setMethodName = "set"
- + targetFieldName.substring(0, 1).toUpperCase()
- + targetFieldName.substring(1);
- // 由方法的名字得到get和set方法的Method对象
- Method getMethod;
- try {
- getMethod = sourceClz.getDeclaredMethod(getMethodName,
- new Class[] {});
- Method setMethod = targetClz.getDeclaredMethod(
- setMethodName, String.class);
- // 调用source对象的getMethod方法
- Object result = getMethod.invoke(source, new Object[] {});
- if(fields[i].getType() == Date.class){
- if(result != null){
- Date date = (Date) result;
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- result = dateFormat.format(date);
- }
- }
- if(result == null){
- result = "";
- }
- // 调用target对象的setMethod方法
- setMethod.invoke(target, result==null?null:new String(result.toString()));
- } 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();
- }
- } else {
- new Exception("同名属性类型不匹配!").printStackTrace();
- }
- }
- }
- /**
- * <p>
- * Description:[删除对象中的基类属性的值]
- * </p>
- * @param recordBO
- *
- * @author wangjinping
- */
- public static void removeBaseAttributeValue(RecordBO recordBO){
- recordBO.setCreatedBy(null);
- recordBO.setCreationMethod(null);
- recordBO.setCreationTime(null);
- recordBO.setUpdatedBy(null);
- recordBO.setUpdateMethod(null);
- recordBO.setUpdateTime(null);
- recordBO.setDeleteFlag(0);
- }
- }
|