The best practice of the Genormous framework: Java Class Library Development Guide
The best practice of the Genormous framework: Java Class Library Development Guide
In order to improve the development efficiency and maintenance of the Java library, many developers have begun to use the Genormous framework.Genormous is a powerful and easy -to -use tool to generate template code for generating standard Java libraries.This article will introduce the best practice and some example code of the Genormous framework to help you get started quickly.
1. Install Genormous
Before starting, you need to install the Genormous framework.Please follow the steps below for installation:
1. Download Genormous's jar file and add it to the construction path of your project.
2. Open the command prompt or terminal and navigate to the root directory of the project.
3. Run the following commands to create Genormous configuration files: java -jar geenormous.jar init
4. Edit the Genormous.yaml configuration file, and configure the template and output path.
Second, configure Genormous
Configure the template and output path of Genormous is the key step to start using the framework.The following is the Genormous.yaml configuration file of an example:
yaml
templates:
- name: Genormous Template
path: templates/GenormousTemplate.ftl
output:
path: src/main/java/com/example/generated
In the above example, we specify a template called "Genormous Template" and the path of the template file.In addition, we also configure the output path of the generated Java file.
3. Create Genormous template
Genormous uses the FreeMarker template engine to generate code.We need to create a template file to define the structure and content of the Java class to generate.
The following is the Genormous template file of the example (GenormousReMPlate.ftl):
package com.example.generated;
/**
* ${class.description}
*/
public class ${class.name} {
${class.fields?map(field -> "private " + field.type + " " + field.name + ";").join("
")}
${class.fields?map(field ->
"public " + field.type + " get" + field.name[0]?upper_case + field.name[1:] + "() {
" +
" return " + field.name + ";
" +
"}
" +
"public void set" + field.name[0]?upper_case + field.name[1:] + "(" + field.type + " " + field.name + ") {
" +
" this." + field.name + " = " + field.name + ";
" +
"}
").join("
")}
}
The above templates define a simple Java class structure, including comments, private fields, and corresponding Getter and Setter methods.
In the template, we use FreeMarker's template syntax, such as $ {} and #if to generate the dynamic part of the code.The details of the template grammar exceed the scope of this article, but you can find more information in the official documentation of FreeMarker.
Fourth, run Genormous
Once we create configuration files and template files, we can run Genormous to generate the template code of the Java library.
In the command prompt or terminal, navigate to the root directory of the project, and run the following command: java -jar geenormous.jar generate
Genormous will generate java files according to the template and output path specified in the configuration file.You can find the generated files generated in the specified output path.
5. Use Genormous
Once the template code of the class library is generated, you can continue to develop according to your needs.According to the specific requirements of each project, you can add new fields, methods, or modified code.
The following is an example of a class generated by Genormous:
package com.example.myapp;
import com.example.generated.GenormousTemplate;
public class MyApp {
public static void main(String[] args) {
GenormousTemplate template = new GenormousTemplate();
template.setId(1);
template.setName("Example");
System.out.println(template.getId());
System.out.println(template.getName());
}
}
The above example demonstrates how to use the class generated by Genormous and set the value of its field.You can expand and modify according to actual needs when using the class generated by Genormous.
in conclusion
The Genormous framework provides a convenient way to generate the template code of the Java class library.By correcting Genormous and creating templates that are suitable for your project needs, you can greatly improve the development efficiency and maintenance of the Java library.
I hope the example code and best practice in this article will help you be in using the Genormous framework.I wish you success in the development of the Java library!