一个用于编译或运行代码生成的实用程序,其目标是Android的Dalvik VM

网友投稿 822 2022-11-03

一个用于编译或运行代码生成的实用程序,其目标是Android的Dalvik VM

一个用于编译或运行代码生成的实用程序,其目标是Android的Dalvik VM

Dexmaker

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.

What does it do?

Mockito Mocks

Dexmaker lets you use the Mockito mocking library in your Android projects by generating Dalvik bytecode class proxies. Just add an androidTestCompile dependency on dexmaker-mockito and you can use Mockito in your Android Instrumentation tests.

The version of Mockito that Dexmaker targets can be found in dexmaker-mockito's build.gradle file. The general rule is that the major and minor version of Dexmaker will match the underlying major and minor version of Mockito.

Mocking Final Classes & Methods

Starting in Android "P", it is possible to mock final classes and methods using the dexmaker-mockito-inline library. If you execute your tests on a device or emulator running Android P or above, you can add an androidTestCompile dependency on dexmaker-mockito-inline (instead of dexmaker-mockito; don't add both) and you can use the normal Mockito APIs to mock final classes and methods in your Android Instrumentation tests.

NOTE: This functionality requires OS APIs which were introduced in Android P and cannot work on older versions of Android.

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.

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 systemType = TypeId.get(System.class); TypeId 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 a = code.newLocal(TypeId.INT); Local b = code.newLocal(TypeId.INT); Local c = code.newLocal(TypeId.INT); Local s = code.newLocal(TypeId.STRING); Local 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 toHexString = TypeId.get(Integer.class).getMethod(TypeId.STRING, "toHexString", TypeId.INT); code.invokeStatic(toHexString, s, c); // System.out.println(s); FieldId systemOutField = systemType.getField(printStreamType, "out"); code.sget(systemOutField, localSystemOut); MethodId printlnMethod = printStreamType.getMethod( TypeId.VOID, "println", TypeId.STRING); code.invokeVirtual(printlnMethod, null, localSystemOut, s); // return; code.returnVoid(); }}

Download

For Mockito support, download the latest .jar via Maven:

com.linkedin.dexmaker dexmaker-mockito 2.21.0 pom

or Gradle:

androidTestCompile 'com.linkedin.dexmaker:dexmaker-mockito:2.21.0'

Note: The dependency on Mockito will be transitively included, so there's no need to specify both Mockito AND dexmaker-mockito

You can also download the files manually from Bintray if you prefer.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:TF-seq2seq 使用TensorFlow实现序列到序列(seq2seq)学习
下一篇:本实验,是用BERT进行中文情感分类,记录了详细操作及完整程序
相关文章

 发表评论

暂时没有评论,来抢沙发吧~