一个很好用的反射工具类
2017-03-15 10:41
661 查看
在网上搜索很久,终于找到一个很好用的反射工具类,略微修改后,终于可以正常使用了。
注意:混淆后的代码不能用反射调用。
注意:混淆后的代码不能用反射调用。
package com.android.systemui.recents.misc; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; public class ReflectUtil { /** * 使用反射调用方法 * * @param o 被调用对象 * @param methodName 被调用对象的方法名称 * @param args 被调用方法所需传入的参数列表 */ public static Object callMethod(Object o,String methodName,Object... args) { Object result = null; try { int argslen = args.length; Class c = o.getClass(); Method m = null; Class<?> argsTypes[] = new Class<?>[argslen]; //基本类型在参数传递过程中会自动封住成对象类型,这里还原成基本类型的Class for (int i=0;i<argslen;i++) { argsTypes[i] = args[i].getClass(); if (argsTypes[i] == Integer.class) { argsTypes[i] = int.class; } if (argsTypes[i] == Float.class) { argsTypes[i] = float.class; } if (argsTypes[i] == Long.class) { argsTypes[i] = long.class; } if (argsTypes[i] == Byte.class) { argsTypes[i] = byte.class; } if (argsTypes[i] == Double.class) { argsTypes[i] = double.class; } if (argsTypes[i] == Boolean.class) { argsTypes[i] = boolean.class; } } if (argslen == 0) { m = c.getDeclaredMethod(methodName); } else { Method[] methodes = c.getDeclaredMethods(); for (Method method: methodes) { Class<?>[] parameterTypes = method.getParameterTypes(); if (method.getName().equals(methodName) && parameterTypes.length == argslen) { int i; for (i=0;i<argslen;i++) { if (argsTypes[i] != parameterTypes[i]) { break; } } if (i == argslen) { m = method; break; } } } } if (m == null) { String argsName = Arrays.toString(argsTypes).replace("[", "(").replace("]", ")"); throw new NoSuchMethodException(methodName + argsName); } m.setAccessible(true); result = m.invoke(o,args); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return result; } /** * 使用反射设置变量值 * * @param target 被调用对象 * @param fieldName 被调用对象的字段,一般是成员变量或静态变量,不可是常量! * @param value 值 * @param <T> value类型,泛型 */ public static <T> void setValue(Object target,String fieldName,T value) { try { Class c = target.getClass(); Field f = c.getDeclaredField(fieldName); f.setAccessible(true); f.set(target, value); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } /** * 使用反射获取变量值 * * @param target 被调用对象 * @param fieldName 被调用对象的字段,一般是成员变量或静态变量,不可以是常量 * @param <T> 返回类型,泛型 * @return 值 */ public static <T> T getValue(Object target,String fieldName) { T value = null; try { Class c = target.getClass(); Field f = c.getDeclaredField(fieldName); f.setAccessible(true); value = (T) f.get(target); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return value; } }
相关文章推荐
- 自己编写的一个Json工具类,实现了反射将整个Object转换为Json对象的功能,支持Hibernate的延迟加载对象
- 自己用反射写的一个javaBean工具类
- 自己编写的一个Json工具类,实现了反射将整个Object转换为Json对象的功能,支持Hibernate的延迟加载对象[修订081217]
- 一个很好用的C#反射操作类 [好东西不敢私有]
- android 一个很好使用的 LogUtils 日志工具类
- 利用反射进行操作的一个工具类
- 一个很好的通用 excel 导出工具类
- 一个很好用的C#反射操作类
- java中使用反射做一个工具类,来为指定类中的成员变量进行赋值操作,使用与多个类对象的成员变量的赋值。
- 自己编写的一个Json工具类,实现了反射将整个Object转换为Json对象的功能,支持Hibernate的延迟加载对象
- 今天根据别人的代码简单的封装了一个将java bean生成xml的一个工具类,运用了反射和泛型,重用性高。
- (反射方法)写一个将一个String类型的json经过base64加密后解密转化成javaBean的工具类
- 一个生成唯一序号的服务,虽然技术不先进,但是很好用
- 反射一个带有out参数的方法
- 一个很好的开源CRM项目
- >+ 用XMLHTTP很好的一个例子
- 一个处理日期的工具类
- 一个很好的blog
- [备忘]发现一个很好的.NET上手教程
- 找到一个很好的FLASH网站