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
CodeGenerator
in Idea pluginsTo install a plugin from the disk in idea:
Settings/Preferences
dialog box and select Plugins
on the left pane.Install plugin from disk
button.Apply
button of the Settings/Preferences dialog.Settings/Preferences > Other Settings > CodeGenerator
to
create a new generator/template.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:
Thus, as a template creator, we need to:
Here we call it a pipeline
for generators. Currently two types of user
action are supported:
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:
Thus CodeGenerator allows generator creators to provide the members to select:
availableMembers
to provide the members to select.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, where 1
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.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。