How To Generate Java Class Based On Template
JavaForger
JavaForger can create source code from templates using existing java classes as input. It reads field, methods and other class data to fill in variables in a template. JavaForger includes default templates for generic java code like equals, hashCode, innerBuilders and unit tests for them. Custom templates tin can be created for repetative code that is project specific. JavaForger is able to directly insert source lawmaking into existing classes.
Example
Let'due south say we accept a class as given as below:
public class MyClass { int number; String name; } Then calling the post-obit code from a principal class:
JavaForger.execute(DefaultConfigurations.forEquals(), "AbsolutePathTo/MyClass.java"); Will outcome in the following grade:
public class MyClass { int number; String name; @Override public boolean equals(Object obj) { return new EqualsBuilder() .append(number, other.number) .append(name, other.proper noun) .isEquals(); } } JavaForger requires a JavaForgerConfiguration as input together with a path to the class to employ every bit input. The configuration contains the template and settings for parsing the input, processing the template and merging it to a destination form. The DefaultConfigurations form contains default configurations for all templates included in the project. A JavaForgerConfiguration tin can also incorporate child configurations, for case DefaultConfigurations.forBuilderAndTest() has a kid configuration for creating a unit test for the builder. That child configuration will be executed after the root configuration. Past default the generated code for the test will exist merged to the course: 'src/test/coffee//Examination.java'. If that file does not exist, a FileNotFoundException will be thrown.
Setup
Add the dependency beneath to the pom of your project.
<dependency> <groupId>com.github.daanvdh.javaforger</groupId> <artifactId>JavaForger</artifactId> <version>2.0.0</version> </dependency> To use all the features of JavaForger you as well accept to set the path to the project you are using as input for the templates. This is mostly needed for finding imports then that these can be used in templates, but can in the future also be needed for other things. The path has to be pointing to the source folder (for maven typically ending with src/master/coffee). It can be setup every bit follows:
StaticJavaForgerConfiguration.getConfig().setProjectPaths("absolutePathToProject/src/principal/java"); Settings and Dependencies
This projection depends on the open up source template engine FreeMarker for processing templates. Settings for executing templates with FreeMarker tin can be set up via JavaForgerConfiguration::setFreeMarkerConfiguration. For processing templates FreeMarker receives a hashmap of String to Object as input. The string tin can exist used inside a template to admission data inside the object. Custom parameters can be added via JavaForgerConfiguration::setInputParameters. Documentation on template settings and syntax can be establish at freemarker.apache.org
JavaForger depends on the open source project JavaParser for reading input java classes and later inserting code into classes. The hashmap with input parameters is filled with the keys 'fields', 'methods' and 'grade'. The fields and methods include data like type, name and annotations which can be accessed directly from a template. The class contains used interfaces and extended classes. It is possible to make changes to the parsed data via JavaForgerConfiguration::addParameterAdjusters. This method receives a Consumer that accepts the hashmap with parameters. Inside the consumer the parameters tin be changed, for instance to filter out static fields before creating a builder (e.g. DefaultAdjusters::removeStaticFields). The class to merge with can be changed via JavaForgerConfiguration::setMergeClass receiving the absolute path to the merge class. You can set the class to merge with via a provider using JavaForgerConfiguration::setMergeClassProvider. The provider receives either the input form path or the merged course of the parent configuration and uses that path to determine the next class path. An example of a merge class provider can exist plant in MergeClassProvider::forMavenUnitTest.
Custom Templates
For writing custom templates y'all first have to set a custom template location, which can exist done as follows:
StaticJavaForgerConfiguration.getConfig().addTemplateLocation("project/relative/path/to/templates"); We can ascertain a template called "equals.javat" as given below. We can see that we access the fields parsed from an input course in a loop, where we use the name of each field to construct the EqualsBuilder. The currently supported variables are described in TemplateVariables.doc.
@Override public boolean equals(Object obj) { return new EqualsBuilder() <#list fields every bit field> .append(${field.name}, other.${field.name}) </#list> .isEquals(); } Features
-
Generated course lawmaking will be merged recursively.
-
Past default existing code (fields, constructors, methods) will not exist overridden, setting via JavaForgerConfiguration::setOverride.
-
By default lawmaking will be inserted in the following guild; a) fields, constructors, methods, classes b) public, 'no admission modifier', protected, private. This can be overridden via StaticJavaForgerConfiguration::setMerger.
-
Required imports for whatsoever field f in a template can exist added by:
<#listing f.typeImports equally import> import ${import}; </#listing>
-
Initialization values for a field f are unique per type and tin used by:
${field.type} ${field.name} = ${field.init1};
Roadmap
- Create eclipse plugin to execute customly created templates without having to specify the input course manually.
- Automatically format coffee classes later inserting code.
- Create information flow graph for elementary methods to exist able to generate unit examination code.
License
JavaForger is available under the terms of the Apache License. http://world wide web.apache.org/licenses/LICENSE-2.0
How To Generate Java Class Based On Template,
Source: https://github.com/daanvdh/JavaForger
Posted by: collinshickinurpoll.blogspot.com

0 Response to "How To Generate Java Class Based On Template"
Post a Comment