views:

171

answers:

2

Hi all,

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. JavaCC's API Javadoc is not too accessible :).

Can anyone advise me on how best to progress?

+1  A: 

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.

Dave Ray
+1  A: 

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.

Pascal Thivent