格式化TraceClassVisitor的输出

格式化TraceClassVisitor的输出

问题描述:

假设我要使用asm库漂亮地打印方法的字节码。

Let's say I want to pretty print the bytecode of a method with the asm library.

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

与我无关的其他所有内容,因此我想将其清除。

being everything else largely irrelevant to me, so I want to wipe them out.

我曾经考虑过通过继承 TraceMethodVisitor 来过滤不需要的内容,但实际上却是最后一堂课(笨蛋!)。

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!).

有没有办法格式化 TraceClassVisitor 的输出?如果不是,那么您认为什么是过滤掉我不关心的东西的最佳方法?

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?

通过将ClassReader.SKIP_DEBUG标志传递给ClassReader.accept()方法来消除行号和局部变量信息。

You can get rid of line numbers and local variables information by passing ClassReader.SKIP_DEBUG flag to ClassReader.accept() method.

另一种方法是在TraceClassVisitor和TraceMethodVisitor之前添加一个访问者会吞下您不想在输出中看到的事件。

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.