importjava.lang.reflect.Field;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;importjava.lang.reflect.Modifier;importjava.lang.reflect.ParameterizedType;importjava.lang.reflect.Type;/*** 反射的 Utils 函数集合
* 提供访问私有变量, 获取泛型类型 Class, 提取集合中元素属性等 Utils 函数
*@authorAdministrator
**/
public classReflectionUtils {/*** 通过反射, 获得定义 Class 时声明的父类的泛型参数的类型
* 如: public EmployeeDao extends BaseDao
*@paramclazz
*@paramindex
*@return
*/@SuppressWarnings("unchecked")public static Class getSuperClassGenricType(Class clazz, intindex){
Type genType=clazz.getGenericSuperclass();if(!(genType instanceofParameterizedType)){return Object.class;
}
Type [] params=((ParameterizedType)genType).getActualTypeArguments();if(index >= params.length || index < 0){return Object.class;
}if(!(params[index] instanceofClass)){return Object.class;
}return(Class) params[index];
}/*** 通过反射, 获得 Class 定义中声明的父类的泛型参数类型
* 如: public EmployeeDao extends BaseDao
*@param
*@paramclazz
*@return
*/@SuppressWarnings("unchecked")public static ClassgetSuperGenericType(Class clazz){return getSuperClassGenricType(clazz, 0);
}/*** 循环向上转型, 获取对象的 DeclaredMethod
*@paramobject
*@parammethodName
*@paramparameterTypes
*@return
*/
public static Method getDeclaredMethod(Object object, String methodName, Class>[] parameterTypes){for(Class> superClass = object.getClass(); superClass != Object.class; superClass =superClass.getSuperclass()){try{//superClass.getMethod(methodName, parameterTypes);
returnsuperClass.getDeclaredMethod(methodName, parameterTypes);
}catch(NoSuchMethodException e) {//Method 不在当前类定义, 继续向上转型
}//..
}return null;
}/*** 使 filed 变为可访问
*@paramfield*/
public static voidmakeAccessible(Field field){if(!Modifier.isPublic(field.getModifiers())){
field.setAccessible(true);
}
}/*** 循环向上转型, 获取对象的 DeclaredField
*@paramobject
*@paramfiledName
*@return
*/
public staticField getDeclaredField(Object object, String filedName){for(Class> superClass = object.getClass(); superClass != Object.class; superClass =superClass.getSuperclass()){try{returnsuperClass.getDeclaredField(filedName);
}catch(NoSuchFieldException e) {//Field 不在当前类定义, 继续向上转型
}
}return null;
}/*** 直接调用对象方法, 而忽略修饰符(private, protected)
*@paramobject
*@parammethodName
*@paramparameterTypes
*@paramparameters
*@return*@throwsInvocationTargetException
*@throwsIllegalArgumentException*/
public static Object invokeMethod(Object object, String methodName, Class>[] parameterTypes,
Object [] parameters)throwsInvocationTargetException{
Method method=getDeclaredMethod(object, methodName, parameterTypes);if(method == null){throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]");
}
method.setAccessible(true);try{returnmethod.invoke(object, parameters);
}catch(IllegalAccessException e) {
System.out.println("不可能抛出的异常");
}return null;
}/*** 直接设置对象属性值, 忽略 private/protected 修饰符, 也不经过 setter
*@paramobject
*@paramfieldName
*@paramvalue*/
public static voidsetFieldValue(Object object, String fieldName, Object value){
Field field=getDeclaredField(object, fieldName);if (field == null)throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
makeAccessible(field);try{
field.set(object, value);
}catch(IllegalAccessException e) {
System.out.println("不可能抛出的异常");
}
}/*** 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter
*@paramobject
*@paramfieldName
*@return
*/
public staticObject getFieldValue(Object object, String fieldName){
Field field=getDeclaredField(object, fieldName);if (field == null)throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
makeAccessible(field);
Object result= null;try{
result=field.get(object);
}catch(IllegalAccessException e) {
System.out.println("不可能抛出的异常");
}returnresult;
}
}