An idea-plugin for code generation, support template customization.
// TODO: add demo
As we know, Intellij had provided useful code generators such as constructors, getter/setters, equals, hashCode, overrides and delegates, etc. And Intellij allows us to apply customized velocity templates for each generator. But we cannot add our own generators.
Code Generator is here to help. Two types of generation are supported here
- Members(fields/method) based templates, such as serializers, equals, etc.
- Class based template, such as transformers, converters, etc. Normally new classes are created.
- Search
CodeGenerator
in Idea plugins - Download zip from from Releases
To install a plugin from the disk in idea:
- Open the
Settings/Preferences
dialog box and selectPlugins
on the left pane. - On the right pane of the dialog, click the
Install plugin from disk
button. - Select the location of the zip file, click OK to continue.
- Click
Apply
button of the Settings/Preferences dialog. - Restart IntelliJ IDEA to activate.
- Go to the
Settings/Preferences > Other Settings > CodeGenerator
to create a new generator/template. - Right click on your java file, Select
Generate > CodeGenerator > [name of your generator]
to run the generator.
According to the settings of your generator, there might be dialogs show up asking to select members or classes that's required by your generator.
Say we want to create a template for generating getters/setters, how will user use your template? An example(the default intellij implementation) is:
- A dialog show up listing all the fields that hadn't implement getters/setters yet.
- User select the members.
- The code is generated using the getter/setter template.
Thus, as a template creator, we need to:
Here we call it a pipeline
for generators. Currently two types of user
action are supported:
- Member selection: generator user can select fields/methods.
- Class selection: generator user can select a class.
Another example is: you might want to create templates that generate convertors between two classes, so that you want the user to select the target class to convert to.
In CodeGenerator, you can create a pipeline with several steps, CodeGenerator will execute the steps sequencially to collect the context variables. Finally generate the code use the template.
Templates varies on what members it allows for selection, for example:
- Getters/Setters generator might want user to select only the fields that have no getters/setters implemented.
- Delegate generators might want user to select the methods that belongs to the field or its super classes.
Thus CodeGenerator allows generator creators to provide the members to select:
- set
availableMembers
to provide the members to select. - set
selectedMembers
to select the members initially, not setting it means select all available members.
Also after the selection, the template context will add some more variables:
fields1
: the selected fields, where1
is the step postfix;methods1
: the selected methods, if any;members1
: the selected fields/methods.
Here is an example of the context variables:
Note in the begining, the class0
variable refers to the class entry where
user starts code generation.
Class selection is much simpler that template creator could specify the initial class to select.
- CodeMaker: where the idea and part of code comes from.
- generate-tostring: Offical toString generator. Part of the code comes from it.