堆叠几个 ASM 字节码访问者的简单方法?
我目前正在实现一些代码,对于类的每个方法,这些代码应该在 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经堆积了两个访问者 - ClassWriter 和 MethodRenamerVisitor。基本上,链中的下一个访问者作为构造函数的第一个参数传递。所以第三个可以这样添加:
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: