Java的反射机制, 将Android系统的安全性几乎破坏得一无是处了。。。。。 这篇文章基本上该说的都说了~~, 回家慢慢学习Android的源码吧,统统破了~~
今天和一位朋友谈到父类私有方法的调用问题,本来以为利用反射很轻松就可以实现,因为在反射看来根本不区分是否是private的,没有想到调用本身的
私有方法是可以的,但是调用父类的私有方法则不行,后来纠其原因很有可能是因为getDeclaredMethod方法和getMethod方法并不会查
找父类的私有方法,于是只好自己写递归了,经过尝试果然如此。把代码放出来方便更多人。这段代码可以解决很多实际问题,不过利用反射来做的话性能不会太好
- public class PrivateUtil {
-
- public static Method getMethod(Class clazz, String methodName,
- final Class[] classes) throws Exception {
- Method method = null;
- try {
- method = clazz.getDeclaredMethod(methodName, classes);
- } catch (NoSuchMethodException e) {
- try {
- method = clazz.getMethod(methodName, classes);
- } catch (NoSuchMethodException ex) {
- if (clazz.getSuperclass() == null) {
- return method;
- } else {
- method = getMethod(clazz.getSuperclass(), methodName,
- classes);
- }
- }
- }
- return method;
- }
-
- public static Object invoke(final Object obj, final String methodName,
- final Class[] classes, final Object[] objects) {
- try {
- Method method = getMethod(obj.getClass(), methodName, classes);
- method.setAccessible(true);
- return method.invoke(obj, objects);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- public static Object invoke(final Object obj, final String methodName,
- final Class[] classes) {
- return invoke(obj, methodName, classes, new Object[] {});
- }
- public static Object invoke(final Object obj, final String methodName) {
- return invoke(obj, methodName, new Class[] {}, new Object[] {});
- }
-
- public static void main(String[] args) {
- PrivateUtil.invoke(new B(), "printlnA", new Class[] { String.class },
- new Object[] { "test" });
- PrivateUtil.invoke(new B(), "printlnB");
- }
- }
- class A {
- private void printlnA(String s) {
- System.out.println(s);
- }
- }
- class B extends A {
- private void printlnB() {
- System.out.println("b");
- }
- }
- 程序的输出结果为
- test
- b
- 说明private方法调用成功了不管是自己的私有方法还是父类的私有方法。
|