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; /** *
* ** 例如有如下定义: * class A* * @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{} * * interface IA {} * * class B extends A implements IA {} * * class C extends A implements IA {} * * class C2 extends C {} * * 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 *
* Description:[删除对象中的基类属性的值] *
* @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); } }