ASMifier显示不够

时间:2016-10-20 13:57:00

标签: java java-bytecode-asm

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

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

无论如何要求工具显示它访问的所有内容吗?

1 个答案:

答案 0 :(得分:1)

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

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

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

  2. 您不得在SKIP_DEBUG

  3. accept方法中指定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标志。