我正在尝试使用ASMifier来确切地了解.class文件中的内容。问题在于该工具忽略了某些mv.visitXXX。例如,对于行声明标签,它不显示mv.visitLabel(Label)。

这是有道理的,因为ASMifier仅显示创建类所需的mv.visit,但我想以其他方式使用它。

无论如何,有没有要求该工具显示其访问的所有内容?

最佳答案

ASMifier中没有针对与源代码行关联的标签的条件处理,换句话说,如果ClassReader报告它们,则ASMifier将显示它们。

为此,必须满足两个条件:


该方法必须具有与其LineNumberTable属性关联的Code属性(对于普通类,这由生成.class文件时使用的编译器选项驱动)。
您必须未为SKIP_DEBUGaccept方法指定ClassReader


使用javac时,调试信息的存在与否由-g选项控制。默认是生成LineNumberTable属性,因此不指定-g选项就足够了。所以下面的程序

package bytecodetests;

import java.io.IOException;
import java.io.PrintWriter;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.util.ASMifier;
import org.objectweb.asm.util.TraceClassVisitor;

public class BytecodeTests {

  public static void main(String[] args) throws IOException {
    new ClassReader(BytecodeTests.class.getResourceAsStream("BytecodeTests.class"))
      .accept(new TraceClassVisitor(null, new ASMifier(), new PrintWriter(System.out)), 0);
  }

}


将产生

package asm.bytecodetests;
import java.util.*;
import org.objectweb.asm.*;
public class BytecodeTestsDump implements Opcodes {

public static byte[] dump () throws Exception {

ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;

cw.visit(52, ACC_PUBLIC + ACC_SUPER, "bytecodetests/BytecodeTests", null, "java/lang/Object", null);

cw.visitSource("BytecodeTests.java", null);

{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitLineNumber(9, l0);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLocalVariable("this", "Lbytecodetests/BytecodeTests;", null, l0, l1, 0);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, new String[] { "java/io/IOException" });
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitLineNumber(12, l0);
mv.visitTypeInsn(NEW, "org/objectweb/asm/ClassReader");
mv.visitInsn(DUP);
mv.visitLdcInsn(Type.getType("Lbytecodetests/BytecodeTests;"));
mv.visitLdcInsn("BytecodeTests.class");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getResourceAsStream", "(Ljava/lang/String;)Ljava/io/InputStream;", false);
mv.visitMethodInsn(INVOKESPECIAL, "org/objectweb/asm/ClassReader", "<init>", "(Ljava/io/InputStream;)V", false);
mv.visitTypeInsn(NEW, "org/objectweb/asm/util/TraceClassVisitor");
mv.visitInsn(DUP);
mv.visitInsn(ACONST_NULL);
mv.visitTypeInsn(NEW, "org/objectweb/asm/util/ASMifier");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "org/objectweb/asm/util/ASMifier", "<init>", "()V", false);
mv.visitTypeInsn(NEW, "java/io/PrintWriter");
mv.visitInsn(DUP);
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitMethodInsn(INVOKESPECIAL, "java/io/PrintWriter", "<init>", "(Ljava/io/OutputStream;)V", false);
mv.visitMethodInsn(INVOKESPECIAL, "org/objectweb/asm/util/TraceClassVisitor", "<init>", "(Lorg/objectweb/asm/ClassVisitor;Lorg/objectweb/asm/util/Printer;Ljava/io/PrintWriter;)V", false);
mv.visitInsn(ICONST_0);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLineNumber(13, l1);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/objectweb/asm/ClassReader", "accept", "(Lorg/objectweb/asm/ClassVisitor;I)V", false);
Label l2 = new Label();
mv.visitLabel(l2);
mv.visitLineNumber(14, l2);
mv.visitInsn(RETURN);
Label l3 = new Label();
mv.visitLabel(l3);
mv.visitLocalVariable("args", "[Ljava/lang/String;", null, l0, l3, 0);
mv.visitMaxs(8, 1);
mv.visitEnd();
}
cw.visitEnd();

return cw.toByteArray();
}
}


请注意

Label l0 = new Label();
mv.visitLabel(l0);
mv.visitLineNumber(12, l0);
…
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLineNumber(13, l1);
…
Label l2 = new Label();
mv.visitLabel(l2);
mv.visitLineNumber(14, l2);
…


调用main方法。



从命令行运行ASMifier时,的确确实默认使用SKIP_DEBUG标志,除非已将-debug选项作为第一个参数。

08-19 11:11