动态类生成预加载-dexmaker使用

本文介绍了Dexmaker库的使用,它是一个针对Dalvik VM的代码生成库,用于运行时或编译时的代码生成。内容包括Dexmaker的特性,如动态类生成、Mockito支持,并给出了一个生成并执行方法的示例。同时提供了Maven和Gradle的导入方式以及单元测试的运行方法。
摘要由CSDN通过智能技术生成

下面这个例子非常典型,可以说入门非常好了。过程很简单,生成一个包含一个函数的类,在主程序里面动态加载(使用ClassLoader),然后执行类里面的函数。这里虽然是写在main函数里的,但是你完全可以将这mian里边的代码放到acticty中实现。注意:进过楼主的实验,放到Android的junit下跑会报错的。注意添加文件权限。

public final class HelloWorldMaker {
    public static void main(String[] args) throws Exception {
        DexMaker dexMaker = new DexMaker();

        // Generate a HelloWorld class.
        TypeId<?> helloWorld = TypeId.get("LHelloWorld;");
        dexMaker.declare(helloWorld, "HelloWorld.generated", Modifier.PUBLIC, TypeId.OBJECT);
        generateHelloMethod(dexMaker, helloWorld);

        // Create the dex file and load it.
                //File outputDir = getApplicationContext().getDir("dx", Context.MODE_PRIVATE);
                //建议在android上测试时使用这个文件路径
        File outputDir = new File(".");
        ClassLoader loader = dexMaker.generateAndLoad(HelloWorldMaker.class.getClassLoader(),
                outputDir, outputDir);
        Class<?> helloWorldClass = loader.loadClass("HelloWorld");

        // Execute our newly-generated code in-process.
        helloWorldClass.getMethod("hello").invoke(null);
    }

    private static void generateHelloMethod(DexMaker dexMaker, TypeId<?> declaringType) {
        // Lookup some types we‘ll need along the way.
        TypeId<System> systemType = TypeId.get(System.class);
        TypeId<PrintStream> printStreamType = TypeId.get(PrintStream.class);

        // Identify the ‘hello()‘ method on declaringType.
        MethodId hello = declaringType.getMethod(TypeId.VOID, "hello");

        // Declare that method on the dexMaker. Use the returned Code instance
        // as a builder that we can append instructions to.
        Code code = dexMaker.declare(hello, Modifier.STATIC | Modifier.PUBLIC);

        // Declare all the locals we‘ll need up front. The API requires this.
        Local<Integer> a = code.newLocal(TypeId.INT);
        Local<Integer> b = code.newLocal(TypeId.INT);
        Local<Integer> c = code.newLocal(TypeId.INT);
        Local<String> s = code.newLocal(TypeId.STRING);
        Local<PrintStream> localSystemOut = code.newLocal(printStreamType);

        // int a = 0xabcd;
        code.loadConstant(a, 0xabcd);

        // int b = 0xaaaa;
        code.loadConstant(b, 0xaaaa);

        // int c = a - b;
        code.op(BinaryOp.SUBTRACT, c, a, b);

        // String s = Integer.toHexString(c);
        MethodId<Integer, String> toHexString
                = TypeId.get(Integer.class).getMethod(TypeId.STRING, "toHexString", TypeId.INT);
        code.invokeStatic(toHexString, s, c);

        // System.out.println(s);
        FieldId<System, PrintStream> systemOutField = systemType.getField(printStreamType, "out");
        code.sget(systemOutField, localSystemOut);
        MethodId<PrintStream, Void> printlnMethod = printStreamType.getMethod(
                TypeId.VOID, "println", TypeId.STRING);
        code.invokeVirtual(printlnMethod, null, localSystemOut, s);

        // return;
        code.returnVoid();
    }
}

generateHelloMethod函数生成的函数是:

       public static void hello() {
             int a = 0xabcd;
             int b = 0xaaaa;
             int c = a - b;
            String s = Integer.toHexString(c);
            System.out.println(s);
            return;
        }

这里很关键的是变量的声明与赋值和函数的声明与调用。

例如int变量声明:
Local a = code.newLocal(TypeId.INT);
变量赋值:
code.loadConstant(a, 0xabcd);
函数声明:
MethodId

国外原文 ,提供jar下载地址

A Java-language API for doing compile time or runtime code generation targeting the Dalvik VM. Unlike
cglib or ASM, this library creates Dalvik .dex
files instead of Java .class files.

It has a small, close-to-the-metal API. This API mirrors the
Dalvik bytecode specification giving you tight
control over the bytecode emitted. Code is generated instruction-by-instruction; you bring your own abstract
syntax tree if you need one. And since it uses Dalvik’s dx tool as a backend, you get efficient register
allocation and regular/wide instruction selection for free.

Class Proxies

Dexmaker includes a stock code generator for class proxies.
If you just want to do AOP or class mocking, you don’t need to mess around with bytecodes.

Mockito Mocks

Dexmaker includes class proxy support for Mockito. Add the mockito
and the dexmaker .jar files to your Android test project’s libs/ directory and you can use Mockito
in your Android unit tests.

This requires Mockito 1.10.5 or newer.

Runtime Code Generation

This example generates a class and a method. It then loads that class into the current process and invokes its method.

public final class HelloWorldMaker {
    public static void main(String[] args) throws Exception {
        DexMaker dexMaker = new DexMaker();

        // Generate a HelloWorld class.
        TypeId<?> helloWorld = TypeId.get("LHelloWorld;");
        dexMaker.declare(helloWorld, "HelloWorld.generated", Modifier.PUBLIC, TypeId.OBJECT);
        generateHelloMethod(dexMaker, helloWorld);

        // Create the dex file and load it.
        File outputDir = new File(".");
        ClassLoader loader = dexMaker.generateAndLoad(HelloWorldMaker.class.getClassLoader(),
                outputDir, outputDir);
        Class<?> helloWorldClass = loader.loadClass("HelloWorld");

        // Execute our newly-generated code in-process.
        helloWorldClass.getMethod("hello").invoke(null);
    }

    /**
     * Generates Dalvik bytecode equivalent to the following method.
     *    public static void hello() {
     *        int a = 0xabcd;
     *        int b = 0xaaaa;
     *        int c = a - b;
     *        String s = Integer.toHexString(c);
     *        System.out.println(s);
     *        return;
     *    }
     */
    private static void generateHelloMethod(DexMaker dexMaker, TypeId<?> declaringType) {
        // Lookup some types we'll need along the way.
        TypeId<System> systemType = TypeId.get(System.class);
        TypeId<PrintStream> printStreamType = TypeId.get(PrintStream.class);

        // Identify the 'hello()' method on declaringType.
        MethodId hello = declaringType.getMethod(TypeId.VOID, "hello");

        // Declare that method on the dexMaker. Use the returned Code instance
        // as a builder that we can append instructions to.
        Code code = dexMaker.declare(hello, Modifier.STATIC | Modifier.PUBLIC);

        // Declare all the locals we'll need up front. The API requires this.
        Local<Integer> a = code.newLocal(TypeId.INT);
        Local<Integer> b = code.newLocal(TypeId.INT);
        Local<Integer> c = code.newLocal(TypeId.INT);
        Local<String> s = code.newLocal(TypeId.STRING);
        Local<PrintStream> localSystemOut = code.newLocal(printStreamType);

        // int a = 0xabcd;
        code.loadConstant(a, 0xabcd);

        // int b = 0xaaaa;
        code.loadConstant(b, 0xaaaa);

        // int c = a - b;
        code.op(BinaryOp.SUBTRACT, c, a, b);

        // String s = Integer.toHexString(c);
        MethodId<Integer, String> toHexString
                = TypeId.get(Integer.class).getMethod(TypeId.STRING, "toHexString", TypeId.INT);
        code.invokeStatic(toHexString, s, c);

        // System.out.println(s);
        FieldId<System, PrintStream> systemOutField = systemType.getField(printStreamType, "out");
        code.sget(systemOutField, localSystemOut);
        MethodId<PrintStream, Void> printlnMethod = printStreamType.getMethod(
                TypeId.VOID, "println", TypeId.STRING);
        code.invokeVirtual(printlnMethod, null, localSystemOut, s);

        // return;
        code.returnVoid();
    }
}

Use it in your app

Maven users can get dexmaker from Sonatype’s central repository. The Mockito dependency is optional.

<dependency>
    <groupId>com.crittercism.dexmaker</groupId>
    <artifactId>dexmaker</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>com.crittercism.dexmaker</groupId>
    <artifactId>dexmaker-dx</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>com.crittercism.dexmaker</groupId>
    <artifactId>dexmaker-mockito</artifactId>
    <version>1.4</version>
</dependency>

Gradle users can import the project this way:

testCompile "com.crittercism.dexmaker:dexmaker:1.4"
testCompile "com.crittercism.dexmaker:dexmaker-dx:1.4"
testCompile "com.crittercism.dexmaker:dexmaker-mockito:1.4"

Download dexmaker-1.4.jar, dexmaker-dx-1.4.jar
and dexmaker-mockito-1.4.jar.

Run the Unit Tests

The unit tests for dexmaker must be run on a dalvikvm. In order to do this, you can use Vogar in the following fashion:

$ java -jar vogar.jar \
  --mode device \
  --sourcepath /path/to/dexmaker/dexmaker/src/test/java \
  --sourcepath /path/to/dexmaker/dexmaker/src/main/java \
  --sourcepath /path/to/dexmaker/dx/src/main/java \
  --device-dir /data/dexmaker /path/to/dexmaker/dexmaker/src/test/

Download vogar.jar.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值