简体   繁体   English

Javassist:Bytecode.get()无法正常工作

[英]Javassist : Bytecode.get() is not working

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() is not returning anything. bc.get()不返回任何内容。 My aim is to get the byte code of a class. 我的目的是获取类的字节码。

Well your System.out.println(b); 好你的System.out.println(b); is printing the whole array every time, you need System.out.println(b[i]); 每次都打印整个数组,您需要System.out.println(b[i]); but I don't think that will work anyway. 但我认为无论如何都行不通。 Try... 尝试...

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();
    }

}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM