JGEN - examples
With the GeneratorPreset property customPaths we tell JGEN where to look for resources (class files), to gather information from. It takes a
Map<File, Boolean>
where the the Boolean indicates if JGEN shall lookup that path recursively.
With the property classPathSelectors we we select the classpath where JGEN shall lookup for resources (class files). JGEN gets the split Java classpath and will ask each ClassPathSelector if it accepts the individual path given as argument.
With the properties classFileSelectors and classFilenameSelectors we we select the class files which JGEN has found in the same manner as with the classpath. ClassFileSelectors and ClassFilenameSelectors extend the JSE interfaces java.io.FileFilter respectively java.io.FilenameFilter.
After all these selections of paths and files we might still need to filter out files in a cross-cutting manner by other criteria, to assert the aggregation of files by many different selectors does not contain files we must omit. We can do that by adding ClassFileValidators to the property classFileValidators.
The core business of JGEN as a template engine is of course to replace the symbols with values.
Values can be fixed values, that is, the value does not depend on a resource and does not change (but is normally not a hard coded literal). The fixed value can be dynamically constructed at the time we assemble the GeneratorPreset, but keeps the same during generation.
Values can be assigned to a key, that is, for each given key a value is returned, e.g. from a Map or resources with key value pairs, at generation time.
Values can be gathered from a Java class, that is, for each given Java class a value is returned at generation time.
To tell JGEN which symbol in the template is to be replaced by which value, we use the Map symbolReferences where assign the symbol in the template as Map key with a SymbolReference as Map value. The type SymbolReference has 3 direct extensions:
- FixedSymbol (for fixed values)
- KeySymbolAssignment (for key value assignments)
- ClassElementType (for values gathered from classes)
At generation time, JGEN will pick the SymbolReference for the assigned template symbol and pass an argument to that SymbolReference. The type and content of the argument will depend on the type of the SymbolReference. For a FixedSymbol it is obsolete. For a KeySymbolAssignment it is a key, which is the template symbol. For a ClassElementType it is a found, selected and validated Java class.
Last but not least we should define an OutputFileStrategy. This will determine if files can be overwritten, existing files will be skipped and which is the output file. For the latter JGEN passes the found class to the strategy, so the strategy can use this to create the name and path / package for the output file.
Note: there is a DefaultOutputFileStrategy that can be extended for very easy changes of the path / package and file name.
Discover what you can do with JGEN to save implementation efforts!
Pages |