对于IDEA的Getter和Setter的生成,IDEA可以使用Velocity语法编写代码生成的模板。
我们可以编写一个链式赋值的模板,API调用起来就像这样: obj = obj.withName(name).withSex(sex);
模板代码如下:
#set($paramName = $helper.getParamName($field, $project))public ###if($field.modifierStatic)static void ###else$classSignature ###endwith$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) {#if ($field.name == $paramName)#if (!$field.modifierStatic)this.###else$classname.###end#end$field.name = $paramName;#if(!$field.modifierStatic)return this;#end}
参考链接:
- https://www.jetbrains.com/help/idea/2020.1/templates-dialog.html?utm_campaign=IC&utm_content=2020.1&utm_medium=link&utm_source=product
- https://www.jetbrains.com/help/idea/2020.1/generating-code.html?utm_campaign=IC&utm_content=2020.1&utm_medium=link&utm_source=product#customize-templates
- http://velocity.apache.org/
