VALJOGen annotations are source-level and needed by compiler only. There are no-runtime dependencies and no libraries that you need to add to your classpath at runtime (unless you explicitly add references yourself to 3rd party code). You do need to add the VALJOGen annotationprocessor jar to your compile path though. Files are available at maven central or can be downloaded manually as noted here
VALJOGen uses standard Java annotation processors (JSR 269
) and should work any Java tool running JDK 1.8+
with a target of JDK 1.7
or later. Below are listed some ways of using VALJOGen with popular tools.
Simple example:
Here is a simple example of how to use a VALJOGen annotation to generate source code for a Java Value Class called SimpleInterfaceImpl:
import com.fortyoneconcepts.valjogen.annotations.VALJOGenerate; /** * Example that shows how to control the name of the generated implementation class. */ @VALJOGenerate(name="SimpleInterfaceImpl") public interface SimpleInterfaceWithNamedOutput { public Object getObject(); public String getString(); }
Refer to the collection of examples along with their generated output for output and the full list of examples.
Advice:
In addtion, make sure output directory is empty when running the annotation processor. Some javac versions are fragile and might give an exception java.lang.IllegalStateException: endPosTable already set
if you forgot to clean output directories before running the processor.
Use Maven 3.2.0
or later and add the dependency which will add the annotation processor and it’s included annotations to your classpath:
<dependency> <groupId>com.41concepts</groupId> <artifactId>valjogen-annotationprocessor</artifactId> <version>2.0.0</version> <optional>true</optional> </dependency>
If you are compiling without the annotation processor or using the -processorpath
option you may want to add just the annotations:
<dependency> <groupId>com.41concepts</groupId> <artifactId>valjogen-annotations</artifactId> <version>2.0.0</version> <optional>true</optional> </dependency>
In both cases the dependency is compile-time only. Unfortunately, there is no good way to specify this with maven but you might get succes marking the dependency as optional as stated above.
In addition you should configure the following for the maven-compiler-plugin:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> ... <configuration> <compilerArgs> <arg>-parameters</arg> <!-- Ensure parameter meta data is available for best code generation --> <arg>-Acom.fortyoneconcepts.valjogen.SOURCEPATH=${basedir}/src/main/java</arg> <!-- Specify where to locate sources --> </compilerArgs> </configuration> </plugin>
See also here for a complete example of a maven pom file.
If you are debugging custom templates you may also want to add extra compilerArgs argument like this:
<arg>-Acom.fortyoneconcepts.valjogen.logLevel=INFO</arg> <arg>-Acom.fortyoneconcepts.valjogen.LOGFILE=${basedir}/target/valjogen.log</arg>
Inside the generated logfile you will then find useful (but a bit complex) dumps of the configuration and models etc.
javac -parameters -cp valjogen-annotationprocessor-2.0.0.jar -Acom.fortyoneconcepts.valjogen.SOURCEPATH=SourceDirForYourCode -s DestinationDirForGeneratedSources -d DestinationDirForOutputClasses SourceDirForYourCodeUsingTheAnnotationProcessor.java
The example above makes the annotation processor available on the normal class path, uses the JDK1.8+ -parameter
option to enable parameter names processing by the annotation processor and the -Akey[=value]
option to let the processor know the source path.
Alternatively, it is possible to compile using the -processorpath option. In this case the classes in the annotation processor is not seen on the classoath so do add the jar file with the VALJOGen annotations on the classpath seperately as shown below.
javac -parameters -cp valjogen-annotations/target/valjogen-annotations-2.0.0.jar -processorpath ../valjogen-processor/target/valjogen-annotationprocessor-2.0.0.jar -Acom.fortyoneconcepts.valjogen.SOURCEPATH=SourceDirForYourCode -s DestinationDirForGeneratedSources -d DestinationDirForOutputClasses SourceDirForYourCodeUsingTheAnnotationProcessor.java
Due to Eclipse bug 382590 VALJOGen can not generated correct code when subclassing a generic interface. Apart from this use case the annotation processor works inside Eclipse if you do the following:
Bug 382590 in Eclipse was reported in 2012 and has not been fixed yet. If you want VALJOGen and other annotation processors to work perfectly in eclipse then please cast your vote for the bug at Eclipse’s bugzilla.
<javac srcdir="${basedir}/src/main/java" destdir="build/classes" classpath="valjogen-annotationprocessor-2.0.0.jar"> <compilerarg value="-parameters" /> <compilerarg value="-Acom.fortyoneconcepts.valjogen.SOURCEPATH=${basedir}/src/main/java"/> </javac>
Where basedir is a defined property pointing to the absolute file path of the project.
VALJOGen output can be customized in a number of ways as listed below (in a order of preference):
See VALJOConfigure JavaDocs and the examples for details about custom templates. See also the StringTemplate 4 cheat sheet for syntax and general tips.
See readme in annotaton processor project for some implementation details or look at the source.
The VALJOGen website is generated using nanoc using github flavored markdown files. See site subproject folder for details.