在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java 反射工具类

2016-12-20 13:16| 发布者: zhangjf| 查看: 582| 评论: 0

摘要: package com.su.dolphin.utils; import java.lang.reflect.Array;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.ref ...
  1. package com.su.dolphin.utils;
  2. import java.lang.reflect.Array;
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.Field;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.lang.reflect.Method;
  7. /**
  8. *
  9. * @className: ReflectionUtil
  10. * @description: 反射工具类
  11. * @author: gaoshuai
  12. * @date: 2015年8月5日 下午4:51:49
  13. */
  14. public class ReflectionUtil
  15. {
  16. /**
  17. *
  18. * @title: setField
  19. * @description: 设置某个成员遍历的值
  20. * @param owner
  21. * @param fieldName
  22. * @param value
  23. * @throws Exception
  24. * @return: void
  25. */
  26. public static void setField(Object owner, String fieldName, Object value) throws Exception {
  27. Class<?> ownerClass = owner.getClass();
  28. Field field = ownerClass.getDeclaredField(fieldName);
  29. field.setAccessible(true);
  30. field.set(owner, value);
  31. }
  32. /**
  33. *
  34. * @title: setFieldAll
  35. * @description: 可以设置父类的field的值
  36. * @param owner
  37. * @param fieldName
  38. * @param value
  39. * @throws Exception
  40. * @return: void
  41. */
  42. public static void setFieldAll(Object owner, String fieldName, Object value) throws Exception {
  43. Class<?> ownerClass = owner.getClass();
  44. Field field = null;
  45. for (Class<?> clazz = ownerClass; clazz != Object.class; clazz = clazz.getSuperclass()) {
  46. try {
  47. field = clazz.getDeclaredField(fieldName);
  48. LogUtil.d(field + " find : in " + clazz.getName());
  49. break;
  50. }
  51. catch (Exception e) {
  52. LogUtil.d(fieldName + " not find in " + clazz.getName());
  53. }
  54. }
  55. field.setAccessible(true);
  56. field.set(owner, value);
  57. }
  58. /**
  59. * 得到某个对象的公共属性
  60. *
  61. * @param owner
  62. * , fieldName
  63. * @return 该属性对象
  64. * @throws Exception
  65. *
  66. */
  67. public static Object getField(Object owner, String fieldName) throws Exception {
  68. Class<?> ownerClass = owner.getClass();
  69. Field field = ownerClass.getField(fieldName);
  70. Object property = field.get(owner);
  71. return property;
  72. }
  73. /**
  74. * 得到某类的静态公共属性
  75. *
  76. * @param className
  77. * 类名
  78. * @param fieldName
  79. * 属性名
  80. * @return 该属性对象
  81. * @throws Exception
  82. */
  83. public static Object getStaticField(String className, String fieldName) throws Exception {
  84. Class<?> ownerClass = Class.forName(className);
  85. Field field = ownerClass.getField(fieldName);
  86. Object property = field.get(ownerClass);
  87. return property;
  88. }
  89. /**
  90. * 执行某对象方法
  91. *
  92. * @param owner
  93. * 对象
  94. * @param methodName
  95. * 方法名
  96. * @param args
  97. * 参数
  98. * @return 方法返回值
  99. * @throws Exception
  100. */
  101. public static Object invokeMethod(Object owner, String methodName, Object... args) throws Exception {
  102. Class<?> ownerClass = owner.getClass();
  103. Class<?>[] argsClass = new Class[args.length];
  104. for (int i = 0, j = args.length; i < j; i++) {
  105. if (args[i].getClass() == Integer.class) { //一般的函数都是 int 而不是Integer
  106. argsClass[i] = int.class;
  107. }
  108. else if (args[i].getClass() == Float.class) { //一般的函数都是 int 而不是Integer
  109. argsClass[i] = float.class;
  110. }
  111. else if (args[i].getClass() == Double.class) { //一般的函数都是 int 而不是Integer
  112. argsClass[i] = double.class;
  113. }
  114. else {
  115. argsClass[i] = args[i].getClass();
  116. }
  117. }
  118. Method method = ownerClass.getDeclaredMethod(methodName, argsClass);
  119. method.setAccessible(true);
  120. return method.invoke(owner, args);
  121. }
  122. /**
  123. *
  124. * @title: invokeMethodAll
  125. * @description: 调用所有的函数, 包括父类的所有函数
  126. * @param owner
  127. * @param methodName
  128. * @param args
  129. * @return
  130. * @throws Exception
  131. * @return: Object
  132. */
  133. public static Object invokeMethodAll(Object owner, String methodName, Object... args) throws Exception {
  134. Class<?> ownerClass = owner.getClass();
  135. Class<?>[] argsClass = new Class[args.length];
  136. for (int i = 0, j = args.length; i < j; i++) {
  137. if (args[i].getClass() == Integer.class) { //一般的函数都是 int 而不是Integer
  138. argsClass[i] = int.class;
  139. }
  140. else if (args[i].getClass() == Float.class) { //一般的函数都是 int 而不是Integer
  141. argsClass[i] = float.class;
  142. }
  143. else if (args[i].getClass() == Double.class) { //一般的函数都是 int 而不是Integer
  144. argsClass[i] = double.class;
  145. }
  146. else {
  147. argsClass[i] = args[i].getClass();
  148. }
  149. }
  150. Method method = null;
  151. for (Class<?> clazz = ownerClass; clazz != Object.class; clazz = clazz.getSuperclass()) {
  152. try {
  153. method = clazz.getDeclaredMethod(methodName, argsClass);
  154. LogUtil.d(method + " find : in " + clazz.getName());
  155. return method;
  156. }
  157. catch (Exception e) {
  158. //e.printStackTrace();
  159. LogUtil.d(methodName + " not find in " + clazz.getName());
  160. }
  161. }
  162. method.setAccessible(true);
  163. return method.invoke(owner, args);
  164. }
  165. /**
  166. * 执行某类的静态方法
  167. *
  168. * @param className
  169. * 类名
  170. * @param methodName
  171. * 方法名
  172. * @param args
  173. * 参数数组
  174. * @return 执行方法返回的结果
  175. * @throws Exception
  176. */
  177. public static Object invokeStaticMethod(String className, String methodName, Object... args) throws Exception {
  178. Class<?> ownerClass = Class.forName(className);
  179. Class<?>[] argsClass = new Class[args.length];
  180. for (int i = 0, j = args.length; i < j; i++) {
  181. argsClass[i] = args[i].getClass();
  182. }
  183. Method method = ownerClass.getMethod(methodName, argsClass);
  184. method.setAccessible(true);
  185. return method.invoke(null, args);
  186. }
  187. /**
  188. * 新建实例
  189. *
  190. * @param className
  191. * 类名
  192. * @param args
  193. * 构造函数的参数 如果无构造参数,args 填写为 null
  194. * @return 新建的实例
  195. * @throws Exception
  196. */
  197. public static Object newInstance(String className, Object[] args) throws NoSuchMethodException, SecurityException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  198. return newInstance(className, args, null);
  199. }
  200. /**
  201. * 新建实例
  202. *
  203. * @param className
  204. * 类名
  205. * @param args
  206. * 构造函数的参数 如果无构造参数,args 填写为 null
  207. * @return 新建的实例
  208. * @throws Exception
  209. */
  210. public static Object newInstance(String className, Object[] args, Class<?>[] argsType) throws NoSuchMethodException, SecurityException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  211. Class<?> newoneClass = Class.forName(className);
  212. if (args == null) {
  213. return newoneClass.newInstance();
  214. }
  215. else {
  216. Constructor<?> cons;
  217. if (argsType == null) {
  218. Class<?>[] argsClass = new Class[args.length];
  219. for (int i = 0, j = args.length; i < j; i++) {
  220. argsClass[i] = args[i].getClass();
  221. }
  222. cons = newoneClass.getConstructor(argsClass);
  223. }
  224. else {
  225. cons = newoneClass.getConstructor(argsType);
  226. }
  227. return cons.newInstance(args);
  228. }
  229. }
  230. /**
  231. * 是不是某个类的实例
  232. *
  233. * @param obj
  234. * 实例
  235. * @param cls
  236. * 类
  237. * @return 如果 obj 是此类的实例,则返回 true
  238. */
  239. public static boolean isInstance(Object obj, Class<?> cls) {
  240. return cls.isInstance(obj);
  241. }
  242. /**
  243. * 得到数组中的某个元素
  244. *
  245. * @param array
  246. * 数组
  247. * @param index
  248. * 索引
  249. * @return 返回指定数组对象中索引组件的值
  250. */
  251. public static Object getItemInArray(Object array, int index) {
  252. return Array.get(array, index);
  253. }
  254. /**
  255. *
  256. * @title: GetClassListByPackage
  257. * @description: 获取包下的所有Class
  258. * @param pPackage
  259. * @return
  260. * @return: Class<?>
  261. */
  262. public static Class<?> getClassListByPackage(String pPackage) {
  263. Package _Package = Package.getPackage(pPackage);
  264. Class<?> _List = _Package.getClass();
  265. return _List;
  266. }
  267. }
复制代码

注意:

1.调用getMethods方法输出的是自身的public方法和父类Object的public方法。调用getDeclaredMethods方法输出的是自身的public、protected、private方法。

2.如果想获取父类的私有函数

  1. public static Method getDeclaredMethod(Object object, String methodName, Class<?> ... parameterTypes){
  2. Method method = null ;
  3. for(Class<?> clazz = object.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass()) {
  4. try {
  5. method = clazz.getDeclaredMethod(methodName, parameterTypes) ;
  6. return method ;
  7. } catch (Exception e) {
  8. }
  9. }
  10. return null;
  11. }
复制代码

来自:http://my.oschina.net/sfshine/blog/488280

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 15:16

Copyright 2015-2025 djqfx

返回顶部