ASM:输出Java字节码和操作码

我正在尝试编写一个程序,该程序使用.class文件并收集.class文件的所有方法以及每个方法的内容.这是我的代码

public class ClassReaderTest1 {

    public static void main(String[] args) throws Exception{
        InputStream in = new FileInputStream("*.class");
        ClassReader reader = new ClassReader(in);
        ClassNode classNode = new ClassNode();
        reader.accept(classNode,0);
        @SuppressWarnings("unchecked")
        final List<MethodNode> methods = classNode.methods;

        for(MethodNode m: methods){
             InsnList inList = m.instructions;
             System.out.println(m.name);
             for(int i = 0; i< inList.size(); i++){
                 System.out.println("     " +     Integer.toHexString(inList.get(i).getOpcode()));
             }
        }
    }
}

这是我的输出

init>
     ffffffff
     ffffffff
     19
     b7
     b1
     ffffffff
main
     ffffffff
     ffffffff
     b2
     12
     b6
     ffffffff
     ffffffff
     3
     36
     ffffffff
     ffffffff
     b1
     ffffffff

最终,我不想打印这些值,我只想能够在程序中引用它们(我正在尝试检查是否获得了正确的值).我得到的方法符合预期,但是方法的内容对我来说没有意义.在我看来,这些不是操作码;特别是“ fffffff”不是Java操作码.我想做的是打印出我上面做过的所有方法,然后在现在有操作码的地方打印出来,先打印出Java字节码,再打印一些空格,然后打印出操作码.例如

main
    bytecode **
    .
    .

我正在加载到该程序中的文件仅包含一个主要方法,一个println语句和一个int变量的初始化.

那么我的问题是我做错了什么,还是只是我没有正确解释我的结果?另外,如何获取字节码?我一直找不到找到它的方法.当我将Java字节码大纲插件用于Eclipse时,可以看到它,但是我需要能够在程序中引用它.

提前致谢

解决方法:

我能够自己弄清楚.如果有人遇到相同的问题,我会发布解决方案.请注意,在这里的实现中,我没有打印出操作码(只需添加一个println语句即可完成此操作).

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.util.List;
import org.objectweb.asm.*;
import org.objectweb.asm.tree.*;
import org.objectweb.asm.util.*;

public class ClassReaderTest1 {

    public static void main(String[] args) throws Exception{
        InputStream in = new FileInputStream("*afile*");
        ClassReader reader = new ClassReader(in);
        ClassNode classNode = new ClassNode();
        reader.accept(classNode,0);
        @SuppressWarnings("unchecked")
        final List<MethodNode> methods = classNode.methods;
        for(MethodNode m: methods){
             InsnList inList = m.instructions;
             System.out.println(m.name);
             for(int i = 0; i< inList.size(); i++){
                 System.out.print(insnToString(inList.get(i)));
             }
        }
    }

    public static String insnToString(AbstractInsnNode insn){
        insn.accept(mp);
        StringWriter sw = new StringWriter();
        printer.print(new PrintWriter(sw));
        printer.getText().clear();
        return sw.toString();
    }

    private static Printer printer = new Textifier();
    private static TraceMethodVisitor mp = new TraceMethodVisitor(printer); 

}

这是产生的输出

<init>
   L0
    LINENUMBER 1 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init> ()V
    RETURN
   L1
main
   L2
    LINENUMBER 3 L2
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    LDC "Hello World!!!"
    INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
   L3
    LINENUMBER 4 L3
    ICONST_0
    ISTORE 1
   L4
    LINENUMBER 5 L4
    RETURN
   L5
上一篇:为什么$x = 5; $x $x;在PHP中等于11?


下一篇:linux – 没有%gs寄存器的x86子集:使用%gs而不是捕获到仿真的二进制修补代码?