preface

In Java development, it is common to write duplicate code. For example, when writing domain classes and persistent classes, most of the time their variable names and types are the same. It is often necessary to write similar code repeatedly when writing domain classes. There were too many similar problems, but I couldn’t find a plug-in that could support custom code templates. So I had to develop a code generation plug-in for IDEA and generate codes by supporting custom code templates with Velocity.

Project address: CodeMaker

The main function

  1. Support for adding custom code templates (Velocity)
  2. Support for selecting multiple classes as contexts for code templates

The installation

Download the plug-in: codemaker.zip

  1. Open the Settings and select “Plugin”
  2. Click “Install Plugin from Disk” in the box on the right
  3. Select the “codemaker.zip” download above
  4. Click “Apply” and restart IDEA.

use

On the Java class editing screen, right-click “Generate” and select the corresponding template to automatically Generate code into the package of the current class. In most cases, the generated code has solved 80% of the problem. Just make a few changes and move to the appropriate package to quickly write code.

If your code template requires a class other than the current class as a context, you can select it through the class selection box.

There are currently two built-in templates:

  1. Model: Generate a class with similar properties from the current class to automatically generate the corresponding domain class for the persistent class (this can save a lot of time if the persistent class has more than 10 properties).
  2. Converter: This template requires two classes as input contexts for automatically generating the domain class and the transformation class for the persistent class.

The above two templates are commonly used in my own work, just for your reference, the built-in template may not meet your needs, so the plug-in supports custom new code templates.

The template configuration

  1. Add the template: Click Add Template and fill in related Settings (none of them can be empty). Click Save to make the Settings take effect without restart. Thank you,khotynRemind)
  2. Delete Template: Click “Delete Template” to Delete the Template

  1. Template Name: The Name displayed in the build menu
  2. Class Number: The number of input context classes required by this template, for example: if 1, take the current class as input:$class0; If it is 2, the user is required to select another class as input:$class0, $class1.
  3. Class Name: The Name of the generated Class, which can be configured with Velocity in the same context as the code template.

Template context

The template context contains the following variables:

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
## Common variables:
## $YEAR - yyyy
## $TIME - yyyy-MM-dd HH:mm:ss
## $USER - user.name
# #
## Available variables:
## $class0 - the context class
## $class1 - the selected class, like $class2, $class2
## $ClassName - generate by the config of "Class Name", the generated class name
# #
## Class Entry Structure:
## $class0.className - the class Name
## $class0.packageName - the packageName
## $class0.importList - the list of imported classes name
## $class0.fields - the list of the class fields
## - type: the field type
## - name: the field name
## - modifier: the field modifier, like "private"
## $class0.methods - the list of class methods
## - name: the method name
## - modifier: the method modifier, like "private static"
## - returnType: the method returnType
## - params: the method params, like "(String name)"
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Copy the code

For details, see the built-in code templates. The customization capability provided by the template context allows each user to customize their own code templates.