如何在运行时编译和部署 java 类?
我正在编写一个规则引擎,该引擎执行由条件构造确定的简单分配。 规则采用XML格式是该项目的先决条件。 我已将 XML 模式建模为类似于简单的代码块。 我希望解析 XML,然后将其转换为 Java 代码。 然后我希望在运行时编译(并运行)此代码。 这样做意味着我的规则引擎不再充当解释器,而是执行本机 Java 字节代码。
我已经弄清楚了解析阶段和或多或少的 Java 代码生成阶段。 我现在想弄清楚最后一个阶段 - 运行时编译阶段。
按照此线程: 编译为 java 字节码(不使用 Java) 我已经意识到以下可能的解决方案:
我会喜欢这些的比较以及解决 Java 在运行时阶段编译的其他建议。
I am in the process of writing a rule engine that performs simple assignments as determined by conditional constructs. It is a prerequisite of the project that the rules be in XML format. I have modeled my XML schema to resemble simple code blocks. I wish to parse the XML and to then transform it into Java code. I then wish to compile (and run) this code at runtime. Doing so would mean that my rule engine no longer acts as an interpreter but executes native Java Byte Code.
I have figured out the parsing stage and more or less the Java code generation phase. I would now like to figure out the last phase - the compile at runtime phase.
Following this thread: Compile to java bytecode (without using Java) I have become aware of the following possible solutions:
I would love a comparison of these as well as other suggestions for solving the Java compile at runtime phase.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将其转换为 Clojure 代码,然后 Clojure 编译器会将其转换为字节码。
You could transform it into Clojure code, and the Clojure compiler will turn it into bytecode for you.
省去麻烦,使用这里提到的 BeanShell 执行文本文件中给出的 java 代码。
Save yourself the hassle and use BeanShell as alluded to here Executing java code given in a text file.
Groovy、BeanShell 或任何其他基于 JVM 的脚本语言都具有在运行时注入、修改、添加和运行代码的功能。 实际上所有脚本语言都是解释性的,所以实际上它们并不是在运行时编译的。
Groovy, BeanShell or any other scripting language which is based on JVM have such a facility to inject, modify, add and run code at runtime. Actually all the scripting language are interpreted, so actually those are not compiling at runtime.
Javassist 几乎是一个用 Java 编写的完整 Java 编译器,并且它完全由 Java 制成。 您不能一次给它一个完整的 .java 文件,但您可以给它各个函数的代码字符串,并将它们添加到同一个 CtClass 对象中,该对象变成字节码,然后变成 java.lang.Class。
我刚刚发布了 GigaLineCompile 0.1 版本,它结合使用 Javassist(编译器)和 Beanshell(解释器),让您可以控制优化哪些代码以及何时优化。 在以后的版本中,它将以更小的粒度在 Javassist 和 Beanshell 之间进行更改,因此如果您有许多共享某些子字符串的代码字符串,则将编译子字符串,而其他部分将在 Beanshell 中运行。 它主要用于生成 Java 代码的人工智能,但它也是 Clojure 或 Javassist/Beanshell 极端的替代方案。
Javassist、Beanshell 和 GigaLineCompile 可以在此处下载(包含源代码):
http://sourceforge.net/projects/gigalinecompile
Javassist is almost a full Java compiler written in Java, and it's completely made of Java. You can't give it a whole .java file at once, but you can give it the code string for individual functions and add them to the same CtClass object, which becomes bytecode and then a java.lang.Class.
I just released version 0.1 of GigaLineCompile, which uses Javassist (compiler) and Beanshell (interpreter) together and gives you control over which code to optimize and when. In later versions, it will change between Javassist and Beanshell at a smaller granularity so if you have many strings of code that share some substrings, the substrings will be compiled and the other parts run in beanshell. Its mostly useful for artificial intelligence that generates Java code, but its also an alternative to Clojure or the extremes of Javassist/Beanshell alone.
Javassist, Beanshell, and GigaLineCompile can be downloaded (with source) here:
http://sourceforge.net/projects/gigalinecompile
您可以分叉这样的进程
并可以编译和运行它。
You can fork a process like this
and can compile and run it.