IDEA代码生成插件 CodeMaker

前言

Java 开发过程当中常常会遇到编写重复代码的事情,例如说:编写领域类和持久类的时候,大部分时候它们的变量名称,类型是同样的,在编写领域类的时候经常要重复写相似的代码。相似的问题太多,却没找到能够支持自定义代码模板的插件,只能本身动手,丰衣足食,开发了一个 IDEA 的代码生成插件,经过 Velocity 支持自定义代码模板来生成代码。git

项目地址CodeMakergithub

主要功能

  1. 支持增长自定义代码模板(Velocity)
  2. 支持选择多个类做为代码模板的上下文

安装

下载插件:CodeMaker.zipbash

  1. 打开设置,选择“Plugin”
  2. 在右边的框中点击“Install plugin from disk”
  3. 选择上面下载的“CodeMaker.zip”
  4. 点击“Apply”,而后重启 IDEA。

使用

在 Java 类编辑界面右键“Generate”,选择对应模板便可自动生成代码到当前类的包,大部分状况下生成的代码已经解决了百分之八十的问题,只需稍做修改,移动到合适的包中,就能快速完成代码编写。 spa

若是代码模板须要除了当前类以外的类做为上下文,能够经过类选择框进行选择。 插件

目前自带的两个模板:code

  1. Model:根据当前类生成一个与其拥有相似属性的类,用于自动生成持久类对应的领域类(在持久类拥有超过10个属性的状况下,可以节省大量时间)。
  2. Converter:该模板须要两个类做为输入的上下文,用于自动生成领域类与持久类的转化类。

上面两个模板是我本身工做中经常使用的模板,仅供你们参考,自带的模板可能知足不了你们的需求,因此插件支持自定义新的代码模板cdn

模板配置

  1. 增长模板:点击“Add Template”后,填写相关配置(都不能为空),点击保存后便可生效,无需重启。(感谢khotyn提醒)
  2. 删除模板:点击“Delete Template”就能将该模板删除

  1. Template Name:在生成菜单中显示的名称,英文命名
  2. Class Number:该模板须要的输入上下文类的数量,例如:若是为 1,,将当前的类做为输入:$class0;若是为 2,须要用户再选择一个类做为输入:$class0, $class1
  3. Class Name:生成的类的名称,支持经过 Velocity 进行配置,上下文为跟代码模板的相同。

模板上下文

模板上下文包含了如下变量:blog

########################################################################################
##
## 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)"
##
########################################################################################
复制代码

具体用法可参考自带的代码模板,经过模板上下文提供的定制能力,可让每一个用户都定制本身的风格的代码模板。ip

相关文章
相关标签/搜索