简体   繁体   English

来自 Class 实例的 jimple 表示

[英]jimple representation from ``Class`` instance

I need to hot-swap method implementations of various classes (the new implementations of which I do not know until somewhen during runtime and which may change again).我需要热交换各种类的方法实现(新的实现我直到运行时的某个时候才知道,并且可能会再次更改)。

ByteBuddy can do that easily, but it (apparently) cannot do much to a method except intercept it, which is why it comes with ASM. ByteBuddy可以很容易地做到这一点,但它(显然)除了拦截它之外不能对方法做太多事情,这就是它带有 ASM 的原因。

Basic usage is基本用法是

    ByteBuddyAgent.install();
    byte[] bytes = transformFoo();

    ClassFileLocator classFileLocator = ClassFileLocator.Simple.of(Foo.class.getName(), bytes);
    new ByteBuddy()
            .redefine(Foo.class, classFileLocator)
            .make()
            .load(Foo.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent());

where在哪里

private static byte[] transformFoo() throws IOException {
    ClassReader classReader = new ClassReader(Foo.class.getResourceAsStream("Foo.class"));
    ClassWriter classWriter = new ClassWriter(classReader, 0);
    MyClassVisitor myClassVisitor = new MyClassVisitor(classWriter);
    classReader.accept(myClassVisitor, 0);

    return classWriter.toByteArray();
}

is using ASM.正在使用 ASM。

But ASM is tedious and hard to both read and code for anything more sophisticated.但是 ASM 既乏味又难以阅读和编写更复杂的代码。 So I'd much rather use Jimple, instead, because it provides abstractions for if-stmts and the likes.所以我更愿意使用 Jimple,因为它为 if-stmts 等提供了抽象。

The idea is, therefore, to start with因此,这个想法是从

Class<?> fooClass = Foo.class;

, somehow convert it to a ,以某种方式将其转换为

SootClass fooSootClass = ...

, transform the method there, and somehow compile it back to byte[] ,在那里转换方法,并以某种方式将其编译回byte[]

byte[] ret = ...
return ret;

st ByteBuddy can reload the class. st ByteBuddy可以重新加载类。

In a nutshell:简而言之:

I want to create a transformable SootClass from a Class<?> and compile it to a byte[] I can pass around.我想从Class<?>创建一个可转换的SootClass并将其编译为一个byte[]我可以传递。

How do I do that?我怎么做?

Update更新

This seems to suggest hot to perform the transformation from SootClass to byte[] , but so far, I haven't been able to locate any kind of documentation or example that would help with conversion from Class to SootClass . 似乎表明执行从SootClassbyte[]的转换SootClass ,但到目前为止,我还没有找到任何有助于从Class转换为SootClass的文档或示例。 Most examples seem to instrument the class only once and before it is loaded.大多数示例似乎只在类加载之前检测一次类。

I have tried that myself years ago and failed.几年前我自己尝试过,但失败了。 I would say it's possible in theory but it's anything but easy.我会说这在理论上是可能的,但它绝非易事。

One problem is that when you try to instrument classes on the fly then typically you want to do that at load time, using java.lang.instrument.一个问题是,当您尝试动态检测类时,通常您希望在加载时使用 java.lang.instrument 进行检测。 This means that you will be seeing one class at a time only, because you are essentially hooking into the class-loading process.这意味着您一次只能看到一个类,因为您实际上是在连接类加载过程。 For many things that Soot does, however, it needs access to other classes.但是,对于 Soot 所做的许多事情,它需要访问其他类。 For instance it needs to resolve signatures and sometimes method bodies.例如,它需要解析签名,有时还需要解析方法体。 This then entirely changes the set of loaded classes and the order in which they are loaded.然后这会完全改变加载类的集合和它们加载的顺序。 I remember that this caused quite some problems.我记得这引起了很多问题。 ASM does no such resolution. ASM 没有这样的决议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM