ReflectionUtil.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package jnpf.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.util.Assert;
  4. import java.lang.reflect.*;
  5. /**
  6. * 反射工具类
  7. * @author JNPF开发平台组
  8. * @version V3.1.0
  9. * @copyright 引迈信息技术有限公司
  10. * @date 2021/3/16 8:56
  11. */
  12. @Slf4j
  13. public class ReflectionUtil {
  14. /** 是否Debug模式 */
  15. private static boolean isDebug = false;
  16. /**
  17. * 调用Getter方法.
  18. */
  19. public static Object invokeGetterMethod(Object obj, String propertyName) {
  20. String getterMethodName = "get" + StringUtil.capitalize(propertyName);
  21. return invokeMethod(obj, getterMethodName, new Class[] {}, new Object[] {});
  22. }
  23. /**
  24. * 调用Setter方法.使用value的Class来查找Setter方法.
  25. */
  26. public static void invokeSetterMethod(Object obj, String propertyName, Object value) {
  27. invokeSetterMethod(obj, propertyName, value, null);
  28. }
  29. /**
  30. * 调用Setter方法.
  31. *
  32. * @param propertyType 用于查找Setter方法,为空时使用value的Class替代.
  33. */
  34. public static void invokeSetterMethod(Object obj, String propertyName, Object value, Class<?> propertyType) {
  35. Class<?> type = propertyType != null ? propertyType : value.getClass();
  36. String setterMethodName = "set" + StringUtil.capitalize(propertyName);
  37. invokeMethod(obj, setterMethodName, new Class[] { type }, new Object[] { value });
  38. }
  39. /**
  40. * 直接读取对象属性值, 无视private/protected修饰符, 不经过getter函数.
  41. */
  42. public static Object getFieldValue(final Object obj, final String fieldName) {
  43. Field field = getAccessibleField(obj, fieldName);
  44. if (field == null) {
  45. throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
  46. }
  47. Object result = null;
  48. try {
  49. result = field.get(obj);
  50. } catch (IllegalAccessException e) {
  51. log.error("不可能抛出的异常{}", e);
  52. }
  53. return result;
  54. }
  55. /**
  56. * 直接设置对象属性值, 无视private/protected修饰符, 不经过setter函数.
  57. */
  58. public static void setFieldValue(final Object obj, final String fieldName, final Object value) {
  59. Field field = getAccessibleField(obj, fieldName);
  60. if (field == null) {
  61. throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
  62. }
  63. try {
  64. field.set(obj, value);
  65. } catch (IllegalAccessException e) {
  66. log.error("不可能抛出的异常:{}", e);
  67. }
  68. }
  69. /**
  70. * 循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问.
  71. *
  72. * 如向上转型到Object仍无法找到, 返回null.
  73. */
  74. public static Field getAccessibleField(final Object obj, final String fieldName) {
  75. Assert.notNull(obj, "object不能为空");
  76. Assert.hasText(fieldName, "fieldName");
  77. for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
  78. try {
  79. Field field = superClass.getDeclaredField(fieldName);
  80. field.setAccessible(true);
  81. return field;
  82. } catch (NoSuchFieldException e) {// NOSONAR
  83. // Field不在当前类定义,继续向上转型
  84. }
  85. }
  86. return null;
  87. }
  88. /**
  89. * 直接调用对象方法, 无视private/protected修饰符. 用于一次性调用的情况.
  90. */
  91. public static Object invokeMethod(final Object obj, final String methodName, final Class<?>[] parameterTypes, final Object[] args) {
  92. Method method = getAccessibleMethod(obj, methodName, parameterTypes);
  93. if (method == null) {
  94. throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + obj + "]");
  95. }
  96. try {
  97. return method.invoke(obj, args);
  98. } catch (Exception e) {
  99. throw convertReflectionExceptionToUnchecked(e);
  100. }
  101. }
  102. /**
  103. * 系统调度使用!!!
  104. * 直接调用对象方法, 无视private/protected修饰符. 用于一次性调用的情况.
  105. */
  106. public static Boolean invokeMethodByTask(final Object obj, final String methodName, final Class<?>[] parameterTypes, final Object[] args) {
  107. Method method = getAccessibleMethod(obj, methodName, parameterTypes);
  108. if (method == null) {
  109. return false;
  110. }
  111. try {
  112. method.invoke(obj, args);
  113. return true;
  114. } catch (Exception e) {
  115. throw convertReflectionExceptionToUnchecked(e);
  116. }
  117. }
  118. /**
  119. * 循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问. 如向上转型到Object仍无法找到, 返回null.
  120. *
  121. * 用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... args)
  122. */
  123. public static Method getAccessibleMethod(final Object obj, final String methodName, final Class<?>... parameterTypes) {
  124. Assert.notNull(obj, "object不能为空");
  125. for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
  126. .getSuperclass()) {
  127. try {
  128. Method method = superClass.getDeclaredMethod(methodName, parameterTypes);
  129. method.setAccessible(true);
  130. return method;
  131. } catch (NoSuchMethodException e) {// NOSONAR
  132. // Method不在当前类定义,继续向上转型
  133. }
  134. }
  135. return null;
  136. }
  137. /**
  138. * 通过反射, 获得Class定义中声明的父类的泛型参数的类型. 如无法找到, 返回Object.class. eg. public UserDao extends HibernateDao<User>
  139. *
  140. * @param clazz
  141. * The class to introspect
  142. * @return the first generic declaration, or Object.class if cannot be determined
  143. */
  144. @SuppressWarnings("unchecked")
  145. public static <T> Class<T> getSuperClassGenricType(final Class<?> clazz) {
  146. return (Class<T>) getSuperClassGenricType(clazz, 0);
  147. }
  148. /**
  149. * 通过反射, 获得Class定义中声明的父类的泛型参数的类型. 如无法找到, 返回Object.class.
  150. *
  151. * 如public UserDao extends HibernateDao<User,Long>
  152. *
  153. * @param clazz
  154. * clazz The class to introspect
  155. * @param index
  156. * the Index of the generic ddeclaration,start from 0.
  157. * @return the index generic declaration, or Object.class if cannot be determined
  158. */
  159. public static Class<?> getSuperClassGenricType(final Class<?> clazz, final int index) {
  160. Type genType = clazz.getGenericSuperclass();
  161. if (!(genType instanceof ParameterizedType)) {
  162. log.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
  163. return Object.class;
  164. }
  165. Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
  166. if (index >= params.length || index < 0) {
  167. log.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
  168. + params.length);
  169. return Object.class;
  170. }
  171. if (!(params[index] instanceof Class)) {
  172. log.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
  173. return Object.class;
  174. }
  175. return (Class<?>) params[index];
  176. }
  177. /**
  178. * 将反射时的checked exception转换为unchecked exceptions.
  179. */
  180. public static RuntimeException convertReflectionExceptionToUnchecked(Exception e) {
  181. if (e instanceof IllegalAccessException || e instanceof IllegalArgumentException
  182. || e instanceof NoSuchMethodException) {
  183. return new IllegalArgumentException("Reflection jnpf.exception.", e);
  184. } else if (e instanceof InvocationTargetException) {
  185. return new RuntimeException("Reflection jnpf.exception.", ((InvocationTargetException) e).getTargetException());
  186. } else if (e instanceof RuntimeException) {
  187. return (RuntimeException) e;
  188. }
  189. return new RuntimeException("Unexpected Checked jnpf.exception.", e);
  190. }
  191. /**
  192. * 调用传入对象的toString方法或反射返回对象成员变量值字符串。
  193. *
  194. * @param obj
  195. * 传入对象
  196. * @return
  197. * @author Lin Chenglin 2013-4-9
  198. */
  199. public static String toString(final Object obj) {
  200. if (obj == null) {
  201. return null;
  202. }
  203. if (obj.getClass() == Object.class || obj.getClass().isPrimitive()) {
  204. return obj.toString();
  205. }
  206. try {
  207. Method method = obj.getClass().getDeclaredMethod("toString", new Class[] {});
  208. if (isDebug) {
  209. log.debug("传入的对象实现了自己的toString方法,直接调用!");
  210. }
  211. return (String) method.invoke(obj, new Object[] {});
  212. } catch (NoSuchMethodException e) {
  213. if (isDebug) {
  214. log.debug("传入的对象没有实现自己的toString方法,反射获取!");
  215. }
  216. StringBuffer buf = new StringBuffer(obj.getClass().getName());
  217. buf.append(" [");
  218. // 获取所有成员变量
  219. Field[] fileds = obj.getClass().getDeclaredFields();
  220. int size = fileds.length;
  221. for (int i = 0; i < size; i++) {
  222. Field field = fileds[i];
  223. Object value = ReflectionUtil.getFieldValue(obj, field.getName());
  224. buf.append(field.getName() + "=" + ReflectionUtil.toString(value));
  225. if (i != size - 1) {
  226. buf.append(", ");
  227. }
  228. }
  229. buf.append("]");
  230. return buf.toString();
  231. } catch (Exception e) {
  232. throw convertReflectionExceptionToUnchecked(e);
  233. }
  234. }
  235. }