格式化 TraceClassVisitor 的输出
假设我想用 asm 库漂亮地打印一个方法的字节码。
public int get777() { return 777; }
通过 TraceClassVisitor
将看起来像
// access flags 0x1
public get777()I
L0
LINENUMBER 21 L0
SIPUSH 777
IRETURN
L1
LOCALVARIABLE this Lsomething/Point; L0 L1 0
MAXSTACK = 1
MAXLOCALS = 1
}
现在,问题是我只关心与
SIPUSH 777
IRETURN
我无关的其他所有内容,所以我想将它们清除。
我想过通过继承 TraceMethodVisitor
来过滤我不想要的东西,但它实际上是一个最终类(真糟糕!)。
有没有办法格式化 TraceClassVisitor
的输出?如果不是,您认为过滤掉我不关心的内容的最佳方法是什么?
Let's say I want to pretty print the bytecode of a method with the asm library.
public int get777() { return 777; }
through TraceClassVisitor
will look as
// access flags 0x1
public get777()I
L0
LINENUMBER 21 L0
SIPUSH 777
IRETURN
L1
LOCALVARIABLE this Lsomething/Point; L0 L1 0
MAXSTACK = 1
MAXLOCALS = 1
}
Now, the thing is that I only care for
SIPUSH 777
IRETURN
being everything else largely irrelevant to me, so I want to wipe them out.
I've thought of filtering the stuff I don't want by inheriting TraceMethodVisitor
, but it actually turned out to be a final class (bummer!).
Is there any way of formatting the output of a TraceClassVisitor
, at all? If not, what would you consider the best approach to filter out the stuff I don't care about?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过将 ClassReader.SKIP_DEBUG 标志传递给 ClassReader.accept() 方法来摆脱行号和局部变量信息。
另一种方法是在 TraceClassVisitor 和 TraceMethodVisitor 之前添加一个访问者,该访问者会吞掉您不想在输出中看到的事件。
You can get rid of line numbers and local variables information by passing ClassReader.SKIP_DEBUG flag to ClassReader.accept() method.
An alternative approach wiuld be to add a visitor before TraceClassVisitor and TraceMethodVisitor that would swallow events you dont want to see in the output.
我会考虑提供自己的 打印机 (可能扩展或委托给 文本化器)通过 TraceClassVisitor(ClassVisitor,Printer,PrintWriter) 构造函数。我还没有测试过这种方法。
I would look at providing my own Printer (perhaps extending or delegating to Textifier) via the TraceClassVisitor(ClassVisitor,Printer,PrintWriter) constructor. I haven't tested this approach.
我的标准方法:获取源代码,搜索并替换
final class
->class
,重新编译。My standard approach: Get the source code, search'n'replace
final class
->class
, recompile.