有没有办法在运行时获取类的字节码? [英] Is there a way to obtain the bytecode for a class at runtime?
问题描述
在Java中,有没有办法(在运行时)获取定义特定类的字节码?
In Java, is there a way (at runtime) to obtain the bytecode which defined a particular class?
换句话说,有没有办法获得 byte []
传递给的数组ClassLoader.defineClass(String name,byte [] b,int off,int len)
当一个特定的类被加载?我看到这个方法被声明为 final
,因此创建一个自定义 ClassLoader
来拦截类定义似乎是不可能的。
Put another way, is there a way to obtain the byte[]
array passed to ClassLoader.defineClass(String name, byte[] b, int off, int len)
when a particular class was loaded? I see that this method is declared final
, so creating a custom ClassLoader
to intercept class definitions seems out of the question.
过去,我使用了类的 ClassLoader
来通过获取字节码getResourceAsStream(String)
方法,但我更喜欢更规范的解决方案。
In the past, I have used the class's ClassLoader
to obtain the bytecode via the getResourceAsStream(String)
method, but I would prefer a more canonical solution.
推荐答案
这里描述了如何实现代理
这篇关于有没有办法在运行时获取类的字节码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!