Reputation: 1137
In my Java application I have a class Foo
class Foo {
String field1;
String field2;
}
I would like to have some generated code which uses reflection on the fields in this class (Imaginary template language)
#for each Field $f in Foo.class.getDeclaredFields()
#writeFile $f.java
public @interface $f {
}
The end goal is to have Field1.java and Field2.java with just a simple @interface definition inside each.
Is there a templating language available which could do this generation as part of a Maven build process?
The closest I have been able to find is JET, but this project seems more geared towards generating Java source to be available at runtime, not at compile time. In theory I could probably make this work using AntRun along with several Javac and Java tasks, but it would be cumbersome.
The actual use case which I need this for is generating BindingAnnotations for Google Guice (which will be used in GWT source, so they must exist as .java files at compile time).
Upvotes: 2
Views: 744
Reputation: 1531
Take a look at Acceleo it is based on XSL Templates to generate source code . I used it with EMF to generate source code from a Data Model designed by the user.
Upvotes: 0
Reputation: 47617
I would suggest two options here:
Apache Velocity: it provides a template language looking close to what you describe. Look into it here. You could probably be interested by their engine.
GWTP seems to do something similar to what you are wanting to do. It looks likes they are using annotation processor to perform their code generation. Here is a processor example and their project home is here.
Upvotes: 0