Is there a way to obtain the bytecode for a class at runtime?

自作多情 提交于 2019-12-17 16:17:04

问题


In Java, is there a way (at runtime) to obtain the bytecode which defined a particular class?

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.

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.


回答1:


Here is a description how to implement an agent




回答2:


"Java agents" would be the obvious solution.



来源:https://stackoverflow.com/questions/2737285/is-there-a-way-to-obtain-the-bytecode-for-a-class-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!