views:

77

answers:

2

I want to build a "toJavaCode()" on my model that would generated the required Java source code to generate that model (never mind the reasons or if it should or shouldn't be done, nor the compatibility issues that may occur).

I'm at a loss at how to test this. I'm using maven, but generate-sources won't really work for me since my server needs to be up for proper, bulk testing. I do get the server up during the "test" goal, but generate-sources is just too early.

On the other hand, while I can use the built in compiler (from tools.jar in the JDK) to do this, I don't know how I can pack it into the jar for testing (or load that jar).

Any ideas?

A: 

You can add ant tasks to your maven. That's a way to something 'out-of-classical-order' during a maven build. Like adding a javac ant task to mavens test goal or so.

(sorry, I'm as confused as your commentor matt b - but the embedded ant tasks are your swiss army knife here.)

Andreas_D
+1  A: 

You can use the JavaCompiler API to compile your source files and setup a classloader to load the compiled classes in your test (sample code). tools.jar has to be on the classpath. This is the case if the JDK is used.

If your generated code is stable for any given class you could use annotation processor to generate the source code and compile it in the same javac run as the annotated class.

Thomas Jung
compile directly to memory? didn't think about that.
Ran Biron
It does compile to temp directory that can be part of your classpath.
Thomas Jung
I've added a link to sample code.
Thomas Jung