Java 显示生成的字节码字节码

Java 显示生成的字节码字节码,java,bytecode,byte-buddy,Java,Bytecode,Byte Buddy,我使用ByteBuddy在运行时创建一个具有动态生成字节码的类。生成的类执行其预期的操作,但我希望手动检查生成的字节码,以确保它是正确的 比如说 Class<?> dynamicType = new ByteBuddy() .subclass(MyAbstractClass.class) .method(named("mymethod")) .intercept(new MyImplementation(args)) .

我使用ByteBuddy在运行时创建一个具有动态生成字节码的类。生成的类执行其预期的操作,但我希望手动检查生成的字节码,以确保它是正确的

比如说

Class<?> dynamicType = new ByteBuddy()
        .subclass(MyAbstractClass.class)
        .method(named("mymethod"))
        .intercept(new MyImplementation(args))
        .make()
        .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
        .getLoaded();
classdynamictype=newbytebuddy()
.subclass(MyAbstractClass.class)
.方法(命名为“mymethod”))
.intercept(新MyImplementation(args))
.make()
.load(getClass().getClassLoader(),ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
其中MyImplementation将多个StackManipulation命令链接在一起,以创建动态生成的代码


我可以将生成的类写入一个文件(这样我就可以用IDE手动检查),或者打印出生成的类的字节码吗?

下面是一个将生成的类的字节存储在字节数组中的示例。以及如何将类保存到文件系统并从该数组实例化

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.instrumentation.FixedValue;
import static net.bytebuddy.instrumentation.method.matcher.MethodMatchers.named;

public class GenerateClass extends ClassLoader {

    void doStuff() throws Exception {
        byte[] classBytes = new ByteBuddy()
                .subclass(Object.class)
                .name("MyClass")
                .method(named("toString"))
                .intercept(FixedValue.value("Hello World!"))
                .make()
                .getBytes();

        // store the MyClass.class file on the file system
        Files.write(Paths.get("MyClass.class"), classBytes, StandardOpenOption.CREATE);

        // create an instance of the class from the byte array
        Class<?> defineClass = defineClass("MyClass", classBytes, 0, classBytes.length);
        System.out.println(defineClass.newInstance());
    }

    public static void main(String[] args) throws Exception {
        new GenerateClass().doStuff();
    }
}
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.nio.file.StandardOpenOption;
导入net.bytebuddy.bytebuddy;
导入net.bytebuddy.instrumentation.FixedValue;
导入静态net.bytebuddy.instrumentation.method.matcher.MethodMatchers.named;
公共类GenerateClass扩展类加载器{
void doStuff()引发异常{
byte[]classBytes=new ByteBuddy()
.subclass(Object.class)
.名称(“MyClass”)
.方法(命名为(“toString”))
.intercept(FixedValue.value(“helloworld!”)
.make()
.getBytes();
//在文件系统上存储MyClass.class文件
write(path.get(“MyClass.class”)、classBytes、StandardOpenOption.CREATE);
//从字节数组创建类的实例
Class defineClass=defineClass(“MyClass”,classBytes,0,classBytes.length);
System.out.println(defineClass.newInstance());
}
公共静态void main(字符串[]args)引发异常{
新生成类().doStuff();
}
}

您可以将类另存为.class文件:

new ByteBuddy()
    .subclass(Object.class)
    .name("Foo")
    .make()
    .saveIn(new File("c:/temp"));

这段代码创建了
c:/temp/Foo.class

,正如前面所指出的,您可以将文件存储为类文件,并使用javap检查它。谢谢,这是次优的。我接受了saka1029的答案,因为它比较短,并且达到了同样的效果。@bramp没关系。因为答案是解决不同的问题。来自saka1029的答案生成类并将其保存到文件中。我的答案是生成类,将其保存到文件中,然后可以实例化生成的类。如果您不需要实例化生成的类或不需要访问生成的字节,那么saka1029的答案就是正确的。