# hutool-postfix-generator **Repository Path**: loolly_admin/hutool-postfix-generator ## Basic Information - **Project Name**: hutool-postfix-generator - **Description**: idea插件intellij-postfix-templates rule生成器,适配hutool - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 7 - **Forks**: 4 - **Created**: 2021-11-17 - **Last Updated**: 2023-10-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 1 [intellij-postfix-templates](https://plugins.jetbrains.com/plugin/9862-custom-postfix-templates) idea代码模板生成插件 ### 1.1 背景 hutool 和 intellij-postfix-templates 就像 arthas 和 arthas-idea-plugin插件一样 搭配着用效果更好 可以 快速生成hutool中的方法, 自动导包 ### 1.2 插件说明 (intellij-postfix-templates) 这个开源插件允许您为 Java、Scala、SQL、PHP、Kotlin、Python、Dart、JavaScript、Ruby、Rust、Go、Groovy定义自己的后缀模板。 它还附带了数百个社区共享的有用的后缀模板。 插件 比 IDEA的后缀模板 使用更方便, 语法更灵活。 与IDEA的后缀模板 相比的一些优点: 1您可以为同一模板名称定义不同的模板规则(基于上下文(参数类型)),例如,toList对于数组和集合的行为应该不同。 2您可以使用用户在应用模板时填写的模板变量(例如$varName$)。 3可以使用live template宏自动填充某些模板变量(例如$var:suggestVariableName()$),也可以定义默认值。 4您可以将模板或模板规则的可用性限制为某些类或库的可用性(例如,如果Lombok可用,则将"test".val扩展到val s="test")。 5它允许您使用静态导入而不是类导入(例如,如果将[use_static_imports]添加到规则中,array.toList可以扩展为asList(array)而不是array.asList(array))。 6它附带了500多个可编辑的后缀模板和700多个模板规则,例如: string.toInt→ 整数解析(字符串) 数组.toList→ Arrays.asList(数组) 文件行→ Files.readAllLines(file.toPath(),Charset.forName("UTF-8")) file.getName().val→ 最终字符串名=file.getName() 7有多种方法可以轻松地与他人共享模板。 1直接将规则上传到 github 或 gitee ,再分享原始数据url (url中带 raw. 或 /raw/ ) (插件配置: idea->setting->editor->custom postfix templates -> template files '+' 符号 -> 选择语言 -> 选择模板文件type 为 web template file(URL)-> 输入raw url 2拉取原始项目,修改 webTemplatFiles.yaml 或 templates目录下文件, 再提 pull request ### 1.3 [intellij-postfix-templates](https://plugins.jetbrains.com/plugin/9862-custom-postfix-templates) 插件使用效果 ![Screen Cast](videos/vid1/vid1.png) ### 1.4 高级用法,新增语言支持,类型参数 ([参考](https://github.com/xylo/intellij-postfix-templates)) $NAME#NO:EXPRESSION:DEFAULT_VALUE$ MATCHING_TYPE [REQUIRED_CLASS] → TEMPLATE_CODE [FLAG] ## 2 适配 hutool 的 [intellij-postfix-templates](https://plugins.jetbrains.com/plugin/9862-custom-postfix-templates) 生成工具 hutool-postfix-generator ### 2.1 与[intellij-postfix-templates-generator ](https://github.com/xylo/intellij-postfix-templates-generator) 区别 && 新增功能点 1 开发语言 从 scala 改成 java 8 2 适配 hutool,自动识别 hutool 下全部模块, 可以通过配置指定模块 或 排除模块 再生成规则文件,(看讨论 hutool-6.x 会去掉 或 新增模块) 3 .postfixTemplates 文件后,添加 hutool 版本 4 生成的规则 按类名,方法名排序,防止每次一样的结果 因顺序不一样 误以为新增了规则 5 Main方法中 集成 规则数统计 和 冲突检测 同时执行 6 通过 mvn dependency:build-classpath 收集每个模块依赖信息, 不用手动再指定 true 的依赖 7 每条postfix规则中方法的注释 改成 指定前缀 + 参数类型 + .java源码中 方法的 首行汉字注释(比英文注释更直观) 8 可配置生成规则时 是否 继承父类的方法,默认不继承 使用子类 调用 父类 静态方法, sonar 扫描会定义坏味道 SonarLint: Use static access with "cn.hutool.core.text.CharSequenceUtil" for "isNotBlank". StrUtil.isNotBlank(""); 9 指定添加非 util类生成规则 10 hutool-all.postfixTemplates 生成后,可复制到指定 idea/intellij-postfix-templates_templates 目录下, 直接使用 11 适配生成 hutool-all/java版本(kotlin兼容) 和 hutool-all/scala ![Screen Cast](videos/vid1/show.png) ## 2.2 hutool-postfix-generator 使用说明 0 环境依赖 java8 ; maven 环境变量 ; hutool master分支源码 ; hutool-parent 有m aven-dependency-plugin 插件 1 目录结构 src\\main\\java 工具代码目录 src\\main\\resources\\templates\\java 生成的java 规则模板 src\\main\\resources\\templates\\scala 生成的scala规则模板 src\\main\\resources\\config.setting 配置文件 2 直接执行 D:\xxxxx\hutool\hutool-postfix-generator\src\test\java\cn\hutool\postfix\generator\Main.java 3 规则文件引入idea(需先下载 intellij-postfix-templates插件) ,config.setting 中设置好后, Main.java 自动导入 java 对应引入需要的 java\\hutool-xxxx.postfixTemplates 到 C:\Users\xxxxx\AppData\Roaming\JetBrains\IntelliJIdea2021.2\plugins\intellij-postfix-templates_templates\templates\java scala 对应引入需要的 scala\\hutool-xxxx.postfixTemplates 到 C:\Users\xxxxx\AppData\Roaming\JetBrains\IntelliJIdea2021.2\plugins\intellij-postfix-templates_templates\templates\scala ## 3 IDEA插件 intellij-postfix-templates, 适配国内 gitee ### 1 github连接太卡, [适配国内 gitee](https://gitee.com/huayu696/intellij-postfix-templates.git) 导致idea打开 intellij-postfix-templates 配置卡死, 启动时也变慢; 修改插件jar包,适配国内 gitee 通过ASM 或 jclasslib 修改插件中指定github 地址为 gitee自己的项目路径 https://gitee.com/huayu696/intellij-postfix-templates.git 插件最新版(2.12.1.213 Oct 29, 2021) 适配 intellij 最新版(2021.3 (eap)) 修改gitee地址后最新插件jar [下载地址](https://gitee.com/huayu696/intellij-postfix-templates/blob/master/resources/intellij-postfix-templates-2.12.1.212.jar) 下载插件后替换 intellij-postfix-templates-2.12.1.212.jar , 将 webTemplateFiles.yaml 中 github地址 的替换成gitee 的 C:\Users\xxxxx\AppData\Roaming\JetBrains\IntelliJIdea2021.2\plugins\intellij-postfix-templates\lib\intellij-postfix-templates-2.12.1.212.jar C:\Users\xxxxx\AppData\Roaming\JetBrains\IntelliJIdea2021.2\plugins\intellij-postfix-templates_templates\templates\webTemplateFiles.yaml 修改类 : intellij-postfix-templates-2.12.1.212.jar 下 de\endrullis\idea\postfixtemplates\language\CptUtil.class 修改方法 : downloadWebTemplatesInfoFile()V ![jclasslib](videos/vid1/clazz.png)