java - 如何在ASM java中将ClassVisitor转换为ClassNode

标签 java bytecode java-bytecode-asm bytecode-manipulation

我想同时使用 ASM Core 和 Tree API。目前,系统正在使用一定数量的ClassVisitor链接在一起来分析一个类文件。现在,在此链的末尾,我想添加另一个在 ClassNode 上工作的 Transformer。是否可以将ClassVisitor转换为ClassNode。

...
_cv = new FirstCV(_cv);
_cv = new SecondCV(_cv);
...
_cv = new ClassNodeTransformer((ClassNode) _cv); // Not sure how to achieve this step.

最佳答案

ClassVisitor 表示处理步骤,而 ClassNode 表示特定的类状态。要集成在 ClassNode 上运行的操作,您需要一个访问者,它在访问类时构建树,并在最后开始访问处理后的结果。

public abstract class IntegrateNodeTransformer extends ClassVisitor {
    ClassVisitor target;

    public IntegrateNodeTransformer(int api, ClassVisitor target) {
        super(api, null);
        this.target = target;
    }

    /**
     * Initiate the building of a tree when the visit of a class starts.
     */
    @Override
    public void visit(int version, int access, String name, String signature,
                      String superName, String[] interfaces) {
        super.cv = new ClassNode();
        super.visit(version, access, name, signature, superName, interfaces);
    }

    /**
     * On completion of visiting the source class, process the tree and initiate
     * visiting of the result by the target visitor, if there is one.
     */
    @Override
    public void visitEnd() {
        super.visitEnd();
        ClassNode source = (ClassNode)super.cv;
        ClassNode result = process(source);
        if(target != null) result.accept(target);
    }

    /**
     * Do the actual work with the Tree API.
     * May manipulate {@code source} directly and return it or return an entirely new tree.
     */
    protected abstract ClassNode process(ClassNode source);
}

如果您只有一个使用 Tree API 的处理步骤,您可以考虑直接执行它,而不将其改造为 ClassVisitor:

ClassReader cr = new ClassReader(…);
// steps before using the tree API

// mind that composed visitor process in the opposite order of their construction
ClassNode cn = new ClassNode();
ClassVisitor cv = new SomeClassVisitor(cn); // 3rd step
cv = new AnotherClassVisitor(cv);           // 2nd step
cv = new YetAnotherClassVisitor(cv);        // 1st step

cr.accept(cv, 0); // perform steps 1 to 3 and build result tree

cn = process(cn); // the Tree API based operation (4th step)

// steps after using the tree API
ClassWriter cw = new ClassWriter(cr, 0);    // last generate class
cv = new YourLastClassVisitor(cw);          // 7th step
cv = new InCaseYouHaveMoreClassVisitor(cv); // 6th step
cv = new YouKnowWhatIMean(cv);              // 5th step

cn.accept(cv); // perform steps 5 to 7 on tree input and generate class
final byte[] code = cw.toByteArray();

关于java - 如何在ASM java中将ClassVisitor转换为ClassNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58509067/

相关文章:

lua - 将所有内容保存在单个 lua 字节码 block 中?

java - 在运行时向方法添加 if stmt 时出现 VerifyException

java - ASM jar - 为什么我的 java 项目依赖于此?

java - iText pdf具有相同内容的多个页面

java - 访问 .class 文件中的符号表

java - 任何 Java 字节码生成指南?

java - 如何避免验证错误: “Expecting to find unitialized object on stack” for objects already initialized

java - 升级到 spring 3.2 但邮件似乎消失了?

java - JSONRequest 成功后 Android 更新 View

java - 为什么要在classpath后面写 "*"