0
import javassist.bytecode.Bytecode;
import javassist.bytecode.ConstPool;
public class Coverage {

    public static void main(String[] args) {

        ConstPool cp = new ConstPool("Hello");
        byte[] b = new byte[100];
        Bytecode bc = new Bytecode(cp);
        b = bc.get();
        System.out.println("Bytecode start");
            for(int i = 0 ; i < b.length ; i++)
             {
                System.out.println(b);
             }
        System.out.println("Bytecode end");
    }

}            

bc.get()没有返回任何东西。我的目标是获取一个类的字节码。

4

2 回答 2

0

好吧,您System.out.println(b);每次都在打印整个阵列,您需要,System.out.println(b[i]);但我认为这无论如何都行不通。尝试...

public static void main(String[] args)  {

    ClassPool pool = ClassPool.getDefault();

    try {
        CtClass cc = pool.get("java.lang.String");
        byte[] bytes = cc.toBytecode();

        System.out.println("Bytecode start");
        for (Byte b : bytes) {
            System.out.println(b);
        }
        System.out.println("Bytecode end");

    } catch (NotFoundException e) {
        e.printStackTrace();
    } catch (CannotCompileException e) {
        e.printStackTrace();
    }

}
于 2012-09-11T12:03:50.530 回答
0

请参阅此BCEL 教程以编写代码覆盖工具。

于 2012-09-17T11:36:48.160 回答