BCEL==monkeypatching 用于 java 吗?

发布于 2024-07-04 21:32:46 字数 359 浏览 14 评论 0原文

有一天,一位同事向我指出了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

若能看破又如何 2024-07-11 21:32:46

BCEL 不支持猴子修补,它只是使用字节码进行操作,并可能将其加载到自定义类加载器中。 但是,您可以使用 BCEL 和 Java 代理等库在 JVM 上实现猴子补丁。 Java 代理(由 -javaagent 参数加载)可以访问 Instrumentation API 并修改加载的类。 通过一些桥梁来实现它并不难。

但请记住:

  • 我不确定是否必须使用 -javaagent 是您想要的。
  • 在任何语言中,猴子补丁都可能导致难以预测的行为。
  • 您可以修改一个方法。 理论上,您还可以添加一些方法,但是您需要针对修改(修补)的类来编译项目。 我认为这会造成很多痛苦,而且不值得。 有其他语言支持它(例如 Groovy)或支持类似的东西(例如 Scala 中的隐式转换)。
  • 精心设计你的 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:

  • I am not sure if having to use -javaagent is something you want.
  • In any language, monkey patching can lead to badly predictable behavior.
  • You can modify a method. In theory, you can also add some method, but you need to compile the project against modified (patched) classes. I think this would cause a lot of pain and it is not worth of it. There are alternative languages that support it (e.g. Groovy) or suppport something similar (e.g. implicit conversions in Scala).
  • It is better to design your API well than to use monkey patching. It may be rather useful for third party libraries.
紧拥背影 2024-07-11 21:32:46

它比经典的猴子修补要低一些,根据我的阅读,已经加载到虚拟机中的类不会更新。 它只支持再次保存到类文件中,不支持修改运行时类。

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.

自由如风 2024-07-11 21:32:46

您可能会将其视为猴子修补。 我宁愿不使用它(也许我从来没有遇到过它的良好用例?),但要熟悉它(了解 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).

香橙ぽ 2024-07-11 21:32:46

请参阅这个现实世界的示例:Jawk - 编译器模块。 BCEL 对于“编译”您的自定义语言很有用。

See this realworld example: Jawk - Compiler Module. BCEL is useful for "compilation" ur custom language.

指尖凝香 2024-07-11 21:32:46

来自 BCEL 的常见问题解答:

问:我可以创建或修改类吗
动态地使用 BCEL?

A: BCEL 包含有用的类
util 包,即 ClassLoader 和
JavaWrapper.看一下
ProxyCreator 示例。

但是monkeypatching是......呃......有争议的,如果你的语言不支持它,你可能不应该使用它。

如果您有一个好的用例,我可以建议嵌入 Jython 吗?

From BCEL's FAQ:

Q: Can I create or modify classes
dynamically with BCEL?

A: BCEL contains useful classes in the
util package, namely ClassLoader and
JavaWrapper.Take a look at the
ProxyCreator example.

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文