Intellij-安装CodeGenerator插件并且添加Builder模板

Posted 寻找风口的猪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Intellij-安装CodeGenerator插件并且添加Builder模板相关的知识,希望对你有一定的参考价值。

Intellij IDEA 2018.1.2版本

CodeGenerator插件地址:https://github.com/lotabout/CodeGenerator/releases

步骤一:安装插件

  下载CodeGenerator.jar到本地磁盘,打开Intellij安装插件的界面进行安装

  

步骤二:添加Builder模式的模板

  

## Tutorial for writing your templates
##
## 1. First you need to know basic syntax of velocity[1].
## 2. Then it is necessary to understand the variable that CodeGenerator provides
##    and its inner structure for retrieving the information you need for generating code.
## 3. Learn to use the utils provided so that you can ask for further information
##    or reduce your workload.
##
## Variables Provided (Class Mode)
## -------------------------------
## Class mode means you want to create new classes(file).
##
## - ClassName: String     The name spcified by `Target Class Name`
## - PackageName: String   The package name specified by `Target Class Name`
## - class0: ClassEntry    The class that the action is triggered upon
##   - raw: PsiClass
##   - String packageName
##   - importList: List<String>
##   - fields: List<FieldEntry>
##   - allFields: List<FieldEntry>
##   - methods: List<MethodEntry>
##   - allMethods: List<MethodEntry>
##   - innerClasses: List<ClassEntry>
##   - allInnerClasses: List<ClassEntry>
##   - typeParamList:  List<String>
##   - name: String
##   - superName: String
##   - superQualifiedName: String
##   - qualifiedName: String
##   - typeParams: int
##   - hasSuper: boolean
##   - deprecated: boolean
##   - enum: boolean
##   - exception: boolean
##   - abstract: boolean
##   - implementNames: String[]
##   - isImplements(String): bool
##   - isExtends(String): bool
##   - matchName(String): bool
##
## - class1: ClassEntry    The first selected class, where `1` is the postfix
##                         you specify in pipeline
##   ...
##
## - MemberEntry (FieldEntry/MethodEntry common properties)
##    - raw: PsiField(for field), PsiMethod(for method)
##    - name: String
##    - accessor: String
##    - array: boolean
##    - nestedArray: boolean
##    - collection: boolean
##    - map: boolean
##    - primitive: boolean
##    - string: boolean
##    - primitiveArray: boolean
##    - objectArray: boolean
##    - numeric: boolean
##    - object: boolean
##    - date: boolean
##    - set: boolean
##    - list: boolean
##    - stringArray: boolean
##    - calendar: boolean
##    - typeName: String
##    - typeQualifiedName: String
##    - type: String
##    - boolean: boolean
##    - long: boolean
##    - float: boolean
##    - double: boolean
##    - void: boolean
##    - notNull: boolean
##    - char: boolean
##    - byte: boolean
##    - short: boolean
##    - modifierStatic: boolean
##    - modifierPublic: boolean
##    - modifierProtected: boolean
##    - modifierPackageLocal: boolean
##    - modifierPrivate: boolean
##    - modifierFinal: boolean
##
## - FieldEntry
##   - constant: boolean
##   - modifierTransient: boolean
##   - modifierVolatile: boolean
##   - enum: boolean
##   - matchName(String): bool
##
## - MethodEntry
##   - methodName: String
##   - fieldName: String
##   - modifierAbstract: boolean
##   - modifierSynchronzied: boolean
##   - modifierSynchronized: boolean
##   - returnTypeVoid: boolean
##   - getter: boolean
##   - deprecated: boolean
##   - matchName(String): bool
##
## Variables for Body Mode
## -----------------------
## - class0: ClassEntry         The current class
## - fields: List<FieldEntry>   All selected fields
## - methods: List<MethodEntry> All selected methods
## - members: List<MemberEntry> selected fields+methods
## - parentMethod: MethodEntry  The nearest method that surround the current cursor
##
## Utilities
## ---------
## - settings: CodeStyleSettings settings of code style
## - project: Project            The project instance, normally used by Psi related utilities
## - helper: GenerationHelper
## - StringUtil: Class
## - NameUtil: Class
## - PsiShortNamesCache: Class   utility to search classes
## - PsiJavaPsiFacade: Class     Java specific utility to search classes
## - GlobalSearchScope: Class    class to create search scopes, used by above utilities
## - EntryFactory: Class         EntryFactory.of(...) to turn PsiXXX to XXXEntry.
##
## Other feature
## -------------
## - Auto import.      If the generated code contains full qualified name, Code Generator will try to
##                     import the packages automatically and shorten the name.
##                     For example `java.util.List<>` -> `List<>`
##
## References
## ----------
## - Velocity syntax: http://velocity.apache.org/engine/1.7/user-guide.html

public static class Builder {
private ${class0.name} instance = new ${class0.name}();

private Builder() {}

public static Builder getInstance() {
return new Builder();
}
public static Builder getInstance(${class0.name} instance) {
            Builder builder = new Builder();
            builder.instance = instance;
            return builder;
        }
#if ( $members.size() > 0 )
    #foreach( $member in $members )
    #set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($member.element, $project))))
    public Builder add${name}(${member.type} ${member.accessor}) {
    this.instance.set${name}(${member.accessor});
    return this;
    }
    #end
#end
public ${class0.name} build() {
return this.instance;
}
}

 

参考:

  【1】个人博客,http://www.littlefisher.site/2018/04/02/Eclipse%E8%BD%ACIDEA%E6%8C%87%E5%8D%97/

以上是关于Intellij-安装CodeGenerator插件并且添加Builder模板的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 CodeGenerator 实现接口的所有成员

1024 CodeGenerator

1024 CodeGenerator

找不到与命令“dotnet-aspnet-codegenerator”匹配的可执行文件”

H2 JOOQ CodeGenerator

aspnet-codegenerator:没有可用的代码生成器,即使添加了 Microsoft.VisualStudio.Web.CodeGeneration.Design