免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4567 | 回复: 1
打印 上一主题 下一主题

JAVA反射工具类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-20 11:09 |只看该作者 |倒序浏览
鉴于android的源码隐藏了部分关键方法不给使用,所以去查了反射机制写了这个工具类。
使用方法:直接调用invoke方法即可。其他的方法应该用不上。

[Java]代码
  1. package com.linin.utils;

  2. import java.lang.reflect.Method;

  3. /**
  4. * 反射工具
  5. *
  6. * @author linin
  7. */
  8. public class ReflectUtil {

  9.     /**
  10.      * 根据对象,返回一个class对象,用于获取方法
  11.      */
  12.     public static Class<?> getClass(Object obj) {
  13.         try {
  14.             return Class.forName(obj.getClass().getName());
  15.         } catch (Exception e) {
  16.             e.printStackTrace();
  17.             return null;
  18.         }
  19.     }

  20.     /**
  21.      * 根据对象,获取某个方法
  22.      *
  23.      * @param obj
  24.      *            对象
  25.      * @param methodName
  26.      *            方法名
  27.      * @param parameterTypes
  28.      *            该方法需传的参数类型,如果不需传参,则不传
  29.      */
  30.     public static Method getMethod(Object obj, String methodName,
  31.             Class<?>... parameterTypes) {
  32.         try {
  33.             Method method = getClass(obj).getDeclaredMethod(methodName,
  34.                     parameterTypes);
  35.             method.setAccessible(true);
  36.             return method;
  37.         } catch (Exception e) {
  38.             e.printStackTrace();
  39.             return null;
  40.         }
  41.     }

  42.     public static Method getMethod(Class<?> cls, String methodName,
  43.             Class<?>... parameterTypes) {
  44.         try {
  45.             Method method = cls.getDeclaredMethod(methodName, parameterTypes);
  46.             method.setAccessible(true);
  47.             return method;
  48.         } catch (Exception e) {
  49.             e.printStackTrace();
  50.             return null;
  51.         }
  52.     }

  53.     /**
  54.      * 直接传入对象、方法名、参数,即可使用该对象的隐藏方法
  55.      *
  56.      * @param obj
  57.      * @param methodName
  58.      * @param parameter
  59.      */
  60.     public static Object invoke(Object obj, String methodName,
  61.             Object... parameter) {
  62.         Class<?>[] parameterTypes = new Class<?>[parameter.length];
  63.         for (int i = 0; i < parameterTypes.length; i++) {
  64.             parameterTypes[i] = parameter[i].getClass();
  65.         }
  66.         try {
  67.             return getMethod(obj, methodName, parameterTypes).invoke(obj,
  68.                     parameter);
  69.         } catch (Exception e) {
  70.             e.printStackTrace();
  71.             return null;
  72.         }
  73.     }

  74.     /**
  75.      * 直接传入类名、方法名、参数,即可使用该对象的隐藏静态方法
  76.      *
  77.      * @param cls
  78.      * @param methodName
  79.      * @param parameter
  80.      */
  81.     public static Object invoke(Class<?> cls, String methodName,
  82.             Object... parameter) {
  83.         Class<?>[] parameterTypes = new Class<?>[parameter.length];
  84.         for (int i = 0; i < parameterTypes.length; i++) {
  85.             parameterTypes[i] = parameter[i].getClass();
  86.         }
  87.         try {
  88.             return getMethod(cls, methodName, parameterTypes).invoke(null,
  89.                     parameter);
  90.         } catch (Exception e) {
  91.             e.printStackTrace();
  92.             return null;
  93.         }
  94.     }

  95. }
复制代码

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
2 [报告]
发表于 2015-08-12 11:45 |只看该作者
不错,把Java反射常用的方式集中起来了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP