JDK 7 允许程序更改自己的代码吗?
我注意到 JDK 7 中有一个编译器 API。这是否意味着 Java 程序可以重新编译并更改正在运行的程序的定义?
目前我知道像 JavaRebel 这样的产品可以做到这一点,但提供产品的免费下载不包括 Javarebel,它是付费产品。
I noticed a compiler API in JDK 7. Does this mean that a Java program can recompile and alter the definition of a running program?
At the moment I know that products like JavaRebel can do this, but to offer a free download of a product excludes Javarebel which is a paid for product.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Java 6 中已经提供了 Compiler API。
我们无法更改已加载类的代码 - 热代码替换仍然仅限于调试,但我们可以从 Java 应用程序编译源代码并在运行时加载新创建的类。
The Compiler API is already available in Java 6.
We can't change the code of already loaded classes - hot code replacement will still be limited to debugging, but we can compile source code from Java applications and load newly created classes at runtime.
是的,这是可能的。一种方法是编写专门的类加载器并使用 ASM 等字节码增强工具,并在运行时更改应用程序的部分内容。也可以使用
javaagent
和ASM
或类似的东西来实现相同的目的。Yes, it is possible. One way is to write a specialized class-loader and use a bytecode enhancement tool like ASM and alter parts of your application at runtime. It's also possible to achieve the same using a
javaagent
and againASM
or something similar.据我所知,Java 7 没有引入额外的自我修改方法。
一些影响已经可以通过使用编译器 API 和 ClassLoaders 来完成,而其他一些影响可以使用 JVM TI。
但据我所知,Java 7 并未对这些 API 进行任何重大更改。
As far as I know Java 7 introduces no additional methods of self-modification.
Some of the affects can already be done by using the compiler API and
ClassLoaders
, while some others can be done using the JVM TI.But as far as I know Java 7 does not introduce any major changes to those APIs.
有一个项目将类似 JRebel 的类重新加载功能添加到 OpenJDK 7,尽管它目前不是 Open JDK 7 的一部分:动态代码演进 VM (DCEVM)
它通过正常的 运行JVMTI类重定义机制。
There is a project to add JRebel-like class reloading functionality to OpenJDK 7, though it is not currently part of Open JDK 7: Dynamic Code Evolution VM (DCEVM)
It works through the normal JVMTI class redefinition mechanism.
Javeleon 提供了类似的功能;它是免费提供的,但不能免费重新分发。
编译器 API (JSR 199) 与代码重新加载或自我修改关系不大,除非希望以某种方式加载新字节码的程序可能使用 199 从 Java 源代码构建该字节码,而不是使用汇编器 API就像 ASM 一样。
更新:
Javelon 似乎不再可用,因为它已被 Zeroturnaround(JRebel 的创建者)收购
从 Javelon 主页:
Javeleon offers functionality along these lines; it is available at no cost but not freely redistributable.
The Compiler API (JSR 199) has little to do with code reloading or self-modification, except insofar as a program which wishes to load new bytecode in some way might use 199 to build that bytecode from Java source, rather than using an assembler API like ASM.
Update:
It looks like Javelon is no longer available as it has been acquired by zeroturnaround (creators of JRebel)
From the Javelon homepage: