1. import java.lang.reflect.Field;
    2. import java.lang.reflect.InvocationTargetException;
    3. import java.lang.reflect.Method;
    4. import java.lang.reflect.Modifier;
    5. import java.lang.reflect.ParameterizedType;
    6. import java.lang.reflect.Type;
    7. /**
    8. * 反射的 Utils 函数集合
    9. * 提供访问私有变量, 获取泛型类型 Class, 提取集合中元素属性等 Utils 函数
    10. *
    11. */
    12. public class ReflectionUtils {
    13. /**
    14. * 将反射时的 "检查异常" 转换为 "运行时异常"
    15. *
    16. * @return
    17. */
    18. public static IllegalArgumentException convertToUncheckedException(Exception e) {
    19. if (e instanceof IllegalAccessException
    20. || e instanceof IllegalArgumentException
    21. || e instanceof NoSuchMethodException) {
    22. throw new IllegalArgumentException("反射异常", e);
    23. } else {
    24. throw new IllegalArgumentException(e);
    25. }
    26. }
    27. /**
    28. * 通过反射, 获得定义 Class 时声明的父类的泛型参数的类型
    29. * 如: public EmployeeDao extends BaseDao<Employee, String>
    30. *
    31. * @param clazz
    32. * @param index
    33. * @return
    34. */
    35. @SuppressWarnings("unchecked")
    36. public static Class getSuperClassGenricType(Class clazz, int index) {
    37. Type genType = clazz.getGenericSuperclass();
    38. if (!(genType instanceof ParameterizedType)) {
    39. return Object.class;
    40. }
    41. Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
    42. if (index >= params.length || index < 0) {
    43. return Object.class;
    44. }
    45. if (!(params[index] instanceof Class)) {
    46. return Object.class;
    47. }
    48. return (Class) params[index];
    49. }
    50. /**
    51. * 通过反射, 获得 Class 定义中声明的父类的泛型参数类型
    52. * 如: public EmployeeDao extends BaseDao<Employee, String>
    53. *
    54. * @param <T>
    55. * @param clazz
    56. * @return
    57. */
    58. @SuppressWarnings("unchecked")
    59. public static <T> Class<T> getSuperGenericType(Class clazz) {
    60. return getSuperClassGenricType(clazz, 0);
    61. }
    62. /**
    63. * 循环向上转型, 获取对象的 DeclaredMethod
    64. *
    65. * @param object
    66. * @param methodName
    67. * @param parameterTypes
    68. * @return
    69. */
    70. public static Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes) {
    71. for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
    72. try {
    73. //superClass.getMethod(methodName, parameterTypes);
    74. return superClass.getDeclaredMethod(methodName, parameterTypes);
    75. } catch (NoSuchMethodException e) {
    76. //Method 不在当前类定义, 继续向上转型
    77. e.printStackTrace();
    78. }
    79. //..
    80. }
    81. return null;
    82. }
    83. /**
    84. * 使 filed 变为可访问
    85. *
    86. * @param field
    87. */
    88. public static void makeAccessible(Field field) {
    89. if (!Modifier.isPublic(field.getModifiers())) {
    90. field.setAccessible(true);
    91. }
    92. }
    93. /**
    94. * 循环向上转型, 获取对象的 DeclaredField
    95. *
    96. * @param object
    97. * @param filedName
    98. * @return
    99. */
    100. public static Field getDeclaredField(Object object, String filedName) {
    101. for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
    102. try {
    103. return superClass.getDeclaredField(filedName);
    104. } catch (NoSuchFieldException e) {
    105. //Field 不在当前类定义, 继续向上转型
    106. e.printStackTrace();
    107. }
    108. }
    109. return null;
    110. }
    111. /**
    112. * 直接调用对象方法, 而忽略修饰符(private, protected)
    113. *
    114. * @param object
    115. * @param methodName
    116. * @param parameterTypes
    117. * @param parameters
    118. * @return
    119. * @throws InvocationTargetException
    120. * @throws IllegalArgumentException
    121. */
    122. public static Object invokeMethod(Object object, String methodName, Class<?>[] parameterTypes,
    123. Object[] parameters) throws InvocationTargetException {
    124. Method method = getDeclaredMethod(object, methodName, parameterTypes);
    125. if (method == null) {
    126. throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]");
    127. }
    128. method.setAccessible(true);
    129. try {
    130. return method.invoke(object, parameters);
    131. } catch (IllegalAccessException e) {
    132. e.printStackTrace();
    133. }
    134. return null;
    135. }
    136. /**
    137. * 直接设置对象属性值, 忽略 private/protected 修饰符, 也不经过 setter
    138. *
    139. * @param object
    140. * @param fieldName
    141. * @param value
    142. */
    143. public static void setFieldValue(Object object, String fieldName, Object value) {
    144. Field field = getDeclaredField(object, fieldName);
    145. if (field == null) {
    146. throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
    147. }
    148. makeAccessible(field);
    149. try {
    150. field.set(object, value);
    151. } catch (IllegalAccessException e) {
    152. e.printStackTrace();
    153. }
    154. }
    155. /**
    156. * 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter
    157. *
    158. * @param object
    159. * @param fieldName
    160. * @return
    161. */
    162. public static Object getFieldValue(Object object, String fieldName) {
    163. Field field = getDeclaredField(object, fieldName);
    164. if (field == null)
    165. throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
    166. makeAccessible(field);
    167. Object result = null;
    168. try {
    169. result = field.get(object);
    170. } catch (IllegalAccessException e) {
    171. e.printStackTrace();
    172. }
    173. return result;
    174. }
    175. }