开发者

JavaCC: I wish to generate java classes from dynamically generated jj files and to compile them at runtime

开发者 https://www.devze.com 2022-12-09 16:37 出处:网络
I am refactoring a project which uses javaCC to create a proprietary language parser during compile time. Due to the fact that different variations of languages can exist at the same time, it has been

I am refactoring a project which uses javaCC to create a proprietary language parser during compile time. Due to the fact that different variations of languages can exist at the same time, it has been decided to dynamically generate the java source code from the jj files during runtime, to then compile the java files into class files and load them.

In order to do so more efficiently I would like to generate the java files in memory to some stream I suppose. JavaC开发者_C百科C's API Javadoc is not too accessible :).

How best to progress?


You're probably better off amending your build process as kdgregory suggests. But, this article describes how to create a new class loader that compiles bits of Java code on the fly using the compiler API. It should be fairly easy to modify it to read from files rather than strings.


You could use the Compiler API aka JSR-199 (see javax.tools) to compile in memory generated classes. You'll need to extend javax.tools.SimpleJavaFileObject class to create a class that will represent the source text of a Java class in RAM.

Check out the following example, it has an implementation of a RAMResidentJavaFileObject that is actually doing that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号