Source code generation for Java (Core)
- 0.4.3 (or later) = Java 11 before namespace change from 'javax' to 'jakarta'
- 0.4.2 (or previous) = Java 8
The project provides some parsers and generators based on the (srcgen4j-common) project.
- XtextParser Parses Xtext models.
- EMFGenerator Generates content based on an ECORE ResourceSet (for example created with Xtext).
- ParameterizedTemplateParser Parses a given directory for XML files of type ParameterizedTemplateModel or ParameterizedTemplateModels and combines all files into one model.
- ParameterizedTemplateGenerator Generates files for a model from the ParameterizedTemplateParser using the Velocity template engine.
The parser is configured with the path where the model files with a dedicated extension can be found. The setup class attribute is used to instantiate the Xtext) parser itself.
<parser name="ptp" class="org.fuin.srcgen4j.core.xtext.XtextParser">
<config>
<xtext:xtext-parser-config modelPath="${testRes}" modelExt="xsdsl"
setupClass="org.fuin.xsample.XSampleDslStandaloneSetup" />
</config>
</parser>
A full blown example for the Xtext based DDD DSL can be found here.
The EMF generator requires setting up the different artifact factories that generate code for different EMF model elements.
<generator name="gen1" class="org.fuin.srcgen4j.core.emf.EMFGenerator" parser="ptp" project="current">
<config>
<emf:emf-generator-config>
<emf:artifact-factory artifact="abstractHello" class="org.fuin.srcgen4j.core.emf.AbstractHelloTstGen">
<variable name="package" value="a.b.c" />
</emf:artifact-factory>
</emf:emf-generator-config>
</config>
<artifact name="abstractHello" folder="testGenMainJava" />
</generator>
You can also define local variables that will be provided to the artifact factory.
The parser is configured with the path where the model files can be found.
<parser name="ptp" class="org.fuin.srcgen4j.core.velocity.ParameterizedTemplateParser">
<config>
<velo:parameterized-template-parser modelPath="${testRes}"
modelFilter=".*\.ptg\.xml"
templatePath="${testRes}"
templateFilter=".*\.ptg\.java" />
</config>
</parser>
A model element always consists of two parts: An XML definition and a velocity template for code generation.
An example template definition (parameterized-template-1.ptg.xml):
<parameterized-template template="parameterized-template-1.ptg.java" xmlns="http://www.fuin.org/srcgen4j/core/velocity">
<!-- Variables that can be used in the velocity template -->
<arguments>
<argument key="name" value="-" />
<argument key="pkg" value="-" />
</arguments>
<!-- Files to be generated with constant values for the above defined variables -->
<target-file path="a" name="A.java">
<argument key="name" value="A" />
<argument key="pkg" value="a" />
</target-file>
<target-file path="b" name="B.java">
<argument key="name" value="B" />
<argument key="pkg" value="b" />
</target-file>
</parameterized-template>
An example velocity template (parameterized-template-1.ptg.java):
package ${pkg};
public class ${name} {
// Whatever
}
It's also possible to create the variable values programmatically (See TestTFLProducer):
<parameterized-template template="parameterized-template-2.ptg.java" xmlns="http://www.fuin.org/srcgen4j/core/velocity">
<arguments>
<argument key="name" value="-" />
<argument key="pkg" value="-" />
</arguments>
<target-file-list-producer class="org.fuin.srcgen4j.core.velocity.TestTFLProducer" />
</parameterized-template>
The generator is simply configured with the path to the to the velocity templates (See topic Resource Management / file.resource.loader.path). xxx
<generator name="gen1" class="org.fuin.srcgen4j.core.velocity.ParameterizedTemplateGenerator"
parser="ptp" project="current" folder="testJava">
<config>
<velo:parameterized-template-generator templatePath="${testRes}" />
</config>
<artifact name="file" />
</generator>
Snapshots can be found on the OSS Sonatype Snapshots Repository.
Add the following to your .m2/settings.xml to enable snapshots in your Maven build:
<repository>
<id>sonatype.oss.snapshots</id>
<name>Sonatype OSS Snapshot Repository</name>
<url>http://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>