1 Star 0 Fork 0

wangjinwen/CodeGenerator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

CodeGenerator

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.

Installation

  1. Search CodeGenerator in Idea plugins
  2. Download zip from from Releases

To install a plugin from the disk in idea:

  1. Open the Settings/Preferences dialog box and select Plugins on the left pane.
  2. On the right pane of the dialog, click the Install plugin from disk button.
  3. Select the location of the zip file, click OK to continue.
  4. Click Apply button of the Settings/Preferences dialog.
  5. Restart IntelliJ IDEA to activate.

Usage

  1. Go to the Settings/Preferences > Other Settings > CodeGenerator to create a new generator/template.
  2. 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.

Pipeline for Generators

Say we want to create a template for generating getters/setters, how will user use your template? An example(the default intellij implementation) is:

  1. A dialog show up listing all the fields that hadn't implement getters/setters yet.
  2. User select the members.
  3. The code is generated using the getter/setter template.

Thus, as a template creator, we need to:

getters-setters-workflow

Here we call it a pipeline for generators. Currently two types of user action are supported:

  1. Member selection: generator user can select fields/methods.
  2. 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.

pipeline

Member Selection

member-selection

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, 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:

context-of-pipeline

Note in the begining, the class0 variable refers to the class entry where user starts code generation.

Class Selection

class-selection

Class selection is much simpler that template creator could specify the initial class to select.

Thanks to

  • CodeMaker: where the idea and part of code comes from.
  • generate-tostring: Offical toString generator. Part of the code comes from it.
Copyright 2017 Jinzhou Zhang Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

Intellij IDEA Plugin for creating customized code generators like the builtin toString, equals, etc. 展开 收起
README
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wangjinwen/CodeGenerator.git
git@gitee.com:wangjinwen/CodeGenerator.git
wangjinwen
CodeGenerator
CodeGenerator
master

搜索帮助