堆叠几个 ASM 字节码访问者的简单方法?

发布于 2024-12-12 14:00:47 字数 1527 浏览 0 评论 0原文

我目前正在实现一些代码,对于类的每个方法,这些代码应该在 .class 文件上运行几个访问者,以检测其字节码。目前,我刚刚实现了一个 MethodRenamerVisitor,但这已经变得非常尴尬:

    ClassReader classReader = null;
    try {
        classReader = new ClassReader(monitoringFile.getCannonicalName());
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    ClassWriter classWriter = null;

    for (BytecodeMethod bytecodeMethod : bytecodeClass.bytecodeMethods) {
        System.out.println("\t" + bytecodeMethod.getName());

        classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

        MethodRenamerVisitor methodRenamerVisitor = new MethodRenamerVisitor(classWriter, bytecodeMethod);
        classReader.accept(methodRenamerVisitor, 0);

        String outputFile = monitoringFile.getCannonicalName();
        outputFile = outputFile.replace('.', File.separatorChar)+".class";

        classReader = new ClassReader(classWriter.toByteArray());
    }

    try {
        fileSystem.writeToDisk(
                classFilename,
                classWriter.toByteArray()
                );
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

我现在正在想要将其他访问者应用于每个 BytecodeMethods。我是否把这个问题过于复杂化了?如果我可以有以下形式的东西,那就太理想了:

MultiVisitors multiVisitors = new MultiVisitors();
visitors.add(new AVisitor(...));
visitors.add(new BVisitor(...));
...
visitors.run();

这样我就可以轻松地堆叠我想要运行的所有访问者,然后只有最后我才必须将它们保存在磁盘上。请记住,每个访问者都会添加/删除/修改其正在访问的方法!

I'm currently implementing some code that should, for each method of a class, run a couple of visitors on a .class file, as to instrument its bytecode. At the moment, I've just implemented a MethodRenamerVisitor, but this is already getting quite awkward:

    ClassReader classReader = null;
    try {
        classReader = new ClassReader(monitoringFile.getCannonicalName());
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    ClassWriter classWriter = null;

    for (BytecodeMethod bytecodeMethod : bytecodeClass.bytecodeMethods) {
        System.out.println("\t" + bytecodeMethod.getName());

        classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

        MethodRenamerVisitor methodRenamerVisitor = new MethodRenamerVisitor(classWriter, bytecodeMethod);
        classReader.accept(methodRenamerVisitor, 0);

        String outputFile = monitoringFile.getCannonicalName();
        outputFile = outputFile.replace('.', File.separatorChar)+".class";

        classReader = new ClassReader(classWriter.toByteArray());
    }

    try {
        fileSystem.writeToDisk(
                classFilename,
                classWriter.toByteArray()
                );
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

I'm now in the process of wanting to apply other visitors to each one of the BytecodeMethods. Am I over-complicating this? It'd be ideal if I could have something of the form of:

MultiVisitors multiVisitors = new MultiVisitors();
visitors.add(new AVisitor(...));
visitors.add(new BVisitor(...));
...
visitors.run();

so I could easily stack all the visitors I want to run and then only in the end I'd have to save them on disk. Keep in mind that each one of the visitors will add/remove/modify the methods it is visiting!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫七 2024-12-19 14:00:47

您已经堆积了两个访问者 - ClassWriter 和 MethodRenamerVisitor。基本上,链中的下一个访问者作为构造函数的第一个参数传递。所以第三个可以这样添加:

ThirdVisitor v3 = new ThirdVisitor(methodRenamerVisitor);
ForthVisitor v4 = new ForthVisitor(v3);
// etc

You already stacking up two visitors - ClassWriter and MethodRenamerVisitor. Basically the next visitor in the chain is passed as first parameter i constructor. So the third one can be added like this:

ThirdVisitor v3 = new ThirdVisitor(methodRenamerVisitor);
ForthVisitor v4 = new ForthVisitor(v3);
// etc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文