BCEL==monkeypatching 用于 java 吗?
有一天,一位同事向我指出了BCEL,我可以从他的解释和快速读取,一种在运行时修改字节码的方法。 我的第一个想法是这听起来很危险,第二个想法是这听起来很酷。 然后我又想了想,我想起了关于猴子修补的codinghorror帖子并意识到这基本上是同一件事。 有人用过 BCEL 做过任何实际的事情吗? 我是否正确,这基本上是运行时猴子修补,还是我错过了一些东西?
a colleague pointed me the other day to BCEL which , as best I can tell from his explanation and a quick read, a way to modify at run time the byte code. My first thought was that it sounded dangerous, and my second thought was that it sounded cool. Then I gave it some more thought and I recalled the codinghorror post on monkey-patching and realized that this was basically the same thing. Has anyone ever used BCEL for anything practical? Am I right that this is basically run time monkey patching, or am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
BCEL 不支持猴子修补,它只是使用字节码进行操作,并可能将其加载到自定义类加载器中。 但是,您可以使用 BCEL 和 Java 代理等库在 JVM 上实现猴子补丁。 Java 代理(由 -javaagent 参数加载)可以访问 Instrumentation API 并修改加载的类。 通过一些桥梁来实现它并不难。
但请记住:
BCEL does not support monkey patching, it just manipulates with bytecode and possibly loads it in a custom classloader. However you can implement monkeypatching on JVM using library like BCEL and Java agent. The Java agent (loaded by -javaagent argument) can access the Instrumentation API and modify loaded classes. It is not hard to implement it via some bridges.
But remember:
它比经典的猴子修补要低一些,根据我的阅读,已经加载到虚拟机中的类不会更新。 它只支持再次保存到类文件中,不支持修改运行时类。
It's a bit more low-level than classic monkey patching, and from what I read, the classes already loaded into the VM are not updated. It only supports saving it to class files again, not modifying run time classes.
您可能会将其视为猴子修补。 我宁愿不使用它(也许我从来没有遇到过它的良好用例?),但要熟悉它(了解 Spring 和 Hibenrate 如何使用它以及为什么)。
You might look at it as monkey patching. I prefer not to use it (maybe I never faced a good use case for it?), but be familiar with it (to have an idea how Spring and Hibenrate use it and why).
请参阅这个现实世界的示例:Jawk - 编译器模块。 BCEL 对于“编译”您的自定义语言很有用。
See this realworld example: Jawk - Compiler Module. BCEL is useful for "compilation" ur custom language.
来自 BCEL 的常见问题解答:
但是monkeypatching是......呃......有争议的,如果你的语言不支持它,你可能不应该使用它。
如果您有一个好的用例,我可以建议嵌入 Jython 吗?
From BCEL's FAQ:
But monkeypatching is... uhm... controversial, and you probably shouldn't use it if your language doesn't support it.
If you have a good use case for it, may I suggest embbededing Jython?