利用Java反射解决Android系统方法不能调用的情况

一是从linux中编译后源码中提取该隐藏类所在的jar包,然后导入项目中调用;

二是从编译前的源码中提取相应的java类,将其包含在项目中使用;

从性能的角度将,我不提倡使用反射,据网上测试结果说,简单方法使用反射调用其性能损耗太大,但复杂方法或项目中使用性能损耗比例小一些,但还是会有损耗,所以希望大家在用之前多多斟酌得失吧~

转载:

之前在写2.2的退出程序的时候,发现一些API是被隐藏的,App不能直接使用,在讨论的时候知道利用反射可以实现。今天就做了个反射的测试用例。做为引子!

为了实用,概念性的东西,直接上代码了:

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

//: ShowMethod.java
//Using Java 1.1 reflection to show all the
//methods of a class, even if the methods are
//defined in the base class

public class ShowMethod {
    static final String usage =
        "usage: \n" +
        "ShowMethods qualified.class.name\n" +
        "To show all methods in class or: \n" +
        "ShowMethods qualified.class.name word\n" +
        "To search for methods involving 'word'";

public static void main(String[] args) {
        System.out.println(Arrays.deepToString(args));
    if(args.length < 1) {
            System.out.println(usage);
            System.exit(0);
        }
    try {
            Class c = Class.forName(args[0]);
          
            Class[] parameterTypes = new Class[1];
            parameterTypes[0] = char[].class;
            try {
                Method tt = c.getMethod("valueOf", parameterTypes);
                char[] pp = {'a','b','c','d'};
                try {
                    String back = (String)tt.invoke(c.newInstance(), pp);
                    System.out.println("反射调用方法valueOf(char[])返回String: "+back);
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
              
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          
            Method[] m = c.getMethods();
            Constructor[] ctor = c.getConstructors();
            if(args.length == 1) {
                for (int i = 0; i < m.length; i++)
                    System.out.println(m.toString());
                for (int i = 0; i < ctor.length; i++)
                    System.out.println(ctor.toString());
            }
            else {
                for (int i = 0; i < m.length; i++)
                    if(m.toString()
                            .indexOf(args[1])!= -1)
                System.out.println(m.toString());
                for (int i = 0; i < ctor.length; i++)
                    if(ctor.toString()
                            .indexOf(args[1])!= -1)
                System.out.println(ctor.toString());
        }
        } catch (ClassNotFoundException e) {
                System.out.println("No such class: " + e);
        }
  
    }
      
}


例子的执行需要注意下:

1.如果不是在eclipse环境下,而是在命令行执行,则需要注意编译出来的ShowMethod.class与执行路径的关系。

需要在工程的bin目录下执行,我的即是:/work/workspace/th-java/bin

我的class文件的位置是:/work/workspace/th-java/bin/thinking/java/example_c11


2.如果是在eclipse中执行。 则在run Configuration ->Arguments 中配置参数 java.lang.String。


部分运行结果:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwxppw.html