IDEA 代码生成插件 CodeMaker

怼烎@ 2022-04-17 03:53 411阅读 0赞
  1. `Java 开发过程中经常会遇到编写重复代码的事情,例如说:编写领域类和持久类的时候,大部分时候它们的变量名称,类型是一样的,在编写领域类的时候常常要重复写类似的代码。类似的问题太多……这里介绍一个 IDEA 的代码生成插件,通过Velocity支持自定义代码模板来生成代码。`

项目地址:CodeMaker
下载地址:CodeMaker V1.2

主要功能
支持增加自定义代码模板(Velocity)
支持选择多个类作为代码模板的上下文

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

使用
在 Java 类编辑界面右键“Generate”,选择对应模板即可自动生成代码到当前类的包,大部分情况下生成的代码已经解决了百分之八十的问题,只需稍作修改,移动到合适的包中,就能快速完成代码编写。
在这里插入图片描述

完整生成:

在这里插入图片描述

如果代码模板需要除了当前类之外的类作为上下文,可以通过类选择框进行选择。

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

上面两个模板是我自己工作中常用的模板,仅供大家参考,自带的模板可能满足不了大家的需求,所以插件支持自定义新的代码模板。

模板配置
在这里插入图片描述

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

在这里插入图片描述

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

模板上下文

模板上下文包含了以下变量:

  1. ########################################################################################
  2. ##
  3. ## Common variables:
  4. ## $YEAR - yyyy
  5. ## $TIME - yyyy-MM-dd HH:mm:ss
  6. ## $USER - user.name
  7. ##
  8. ## Available variables:
  9. ## $class0 - the context class
  10. ## $class1 - the selected class, like $class2, $class2
  11. ## $ClassName - generate by the config of "Class Name", the generated class name
  12. ##
  13. ## Class Entry Structure:
  14. ## $class0.className - the class Name
  15. ## $class0.packageName - the packageName
  16. ## $class0.importList - the list of imported classes name
  17. ## $class0.fields - the list of the class fields
  18. ## - type: the field type
  19. ## - name: the field name
  20. ## - modifier: the field modifier, like "private"
  21. ## $class0.methods - the list of class methods
  22. ## - name: the method name
  23. ## - modifier: the method modifier, like "private static"
  24. ## - returnType: the method returnType
  25. ## - params: the method params, like "(String name)"
  26. ##
  27. ########################################################################################

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

如下是我根据自己公司代码规范写的VM转换模版:
在这里插入图片描述

myselfConverter

  1. ########################################################################################
  2. ##
  3. ## Common variables:
  4. ## $YEAR - yyyy
  5. ## $TIME - yyyy-MM-dd HH:mm:ss
  6. ## $USER - user.name
  7. ##
  8. ## Available variables:
  9. ## $class0 - the context class, alias: $class
  10. ## $class1 - the selected class, like $class1, $class2
  11. ## $ClassName - generate by the config of "Class Name", the generated class name
  12. ##
  13. ## Class Entry Structure:
  14. ## $class0.className - the class Name
  15. ## $class0.packageName - the packageName
  16. ## $class0.importList - the list of imported classes name
  17. ## $class0.fields - the list of the class fields
  18. ## - type: the field type
  19. ## - name: the field name
  20. ## - modifier: the field modifier, like "private",or "@Setter private" if include annotations
  21. ## $class0.allFields - the list of the class fields include all fields of superclass
  22. ## - type: the field type
  23. ## - name: the field name
  24. ## - modifier: the field modifier, like "private",or "@Setter private" if include annotations
  25. ## $class0.methods - the list of class methods
  26. ## - name: the method name
  27. ## - modifier: the method modifier, like "private static"
  28. ## - returnType: the method returnType
  29. ## - params: the method params, like "(String name)"
  30. ## $class0.allMethods - the list of class methods include all methods of superclass
  31. ## - name: the method name
  32. ## - modifier: the method modifier, like "private static"
  33. ## - returnType: the method returnType
  34. ## - params: the method params, like "(String name)"#
  35. ########################################################################################
  36. #macro (cap $strIn)$strIn.valueOf($strIn.charAt(0)).toUpperCase()$strIn.substring(1)#end
  37. #macro (low $strIn)$strIn.valueOf($strIn.charAt(0)).toLowerCase()$strIn.substring(1)#end
  38. #set($class0Var = "#low(${class0.ClassName})")
  39. #set($class1Var = "#low(${class1.ClassName})")
  40. package $class0.PackageName;
  41. #foreach($importer in $class0.ImportList)
  42. import $importer;
  43. #end
  44. import $class1.packageName.$class1.className;
  45. /**
  46. *
  47. * @author $USER
  48. * @version ${ClassName}.java, v 0.1 $TIME $USER
  49. */
  50. @Data
  51. public class $ClassName {
  52. /**
  53. * Convert ${class1.ClassName} to ${class0.ClassName}
  54. * @param ${class1Var}
  55. * @return
  56. */
  57. public static $class0.ClassName convertTo${class0.ClassName}($class1.ClassName #low($class1.ClassName)) {
  58. if (${class1Var} == null) {
  59. return null;
  60. }
  61. $class0.ClassName ${class0Var} = new ${class0.ClassName}();
  62. #foreach($field in $class0.Fields)
  63. #if( $field.modifier.equals("private"))
  64. ${class0Var}.set#cap($field.Name)(${class1Var}.get#cap($field.Name)());
  65. #end
  66. #end
  67. return ${class0Var};
  68. }
  69. /**
  70. * Convert ${class0.ClassName} to ${class1.ClassName}
  71. * @param ${class0Var}
  72. * @return
  73. */
  74. public static $class1.ClassName convertTo${class1.ClassName}($class0.ClassName #low($class0.ClassName)) {
  75. if (${class0Var} == null) {
  76. return null;
  77. }
  78. $class1.ClassName ${class1Var} = new ${class1.ClassName}();
  79. #foreach($field in $class1.Fields)
  80. #if( $field.modifier.equals("private"))
  81. ${class1Var}.set#cap($field.Name)(${class0Var}.get#cap($field.Name)());
  82. #end
  83. #end
  84. return ${class1Var};
  85. }
  86. }

发表评论

表情:
评论列表 (有 0 条评论,411人围观)

还没有评论,来说两句吧...

相关阅读

    相关 IDEA 代码生成 CodeMaker

        `Java 开发过程中经常会遇到编写重复代码的事情,例如说:编写领域类和持久类的时候,大部分时候它们的变量名称,类型是一样的,在编写领域类的时候常常要重复写类似的代码。

    相关 IDEA代码生成 CodeMaker

    前言 Java 开发过程中经常会遇到编写重复代码的事情,例如说:编写领域类和持久类的时候,大部分时候它们的变量名称,类型是一样的,在编写领域类的时候常常要重复写类似的代码