the*_*nov 3 java setter intellij-idea mutators
是否可以在 IntelliJ IDEA 中生成可链接的字段变异器?最好使用 not 以外的前缀,set以避免破坏约定。比方说withX是 的可链接伴侣setX。
例如,给定这个类存根
class SomeClass {
private String foo;
}
Run Code Online (Sandbox Code Playgroud)
我可以使用Generate -> Setters 来生成一个public void setFoo(String foo)方法。
class SomeClass {
private String foo;
public void setFoo(String foo) {
this.foo = foo;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有类似的方法来生成public SomeClass withFoo(String foo)这样的方法?
class SomeClass {
private String foo;
public SomeClass withFoo(String foo) {
this.foo = foo;
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
您实际上不需要安装任何第三方插件。IDEA 提供了开箱即用的此功能。以下是实现此目标的步骤:
#set($paramName = $helper.getParamName($field, $project))
public ##
#if($field.modifierStatic)
static void ##
#else
$classSignature ##
#end
with$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
}
Run Code Online (Sandbox Code Playgroud)
你应该只做一次。将来您只需选择 setter 模板即可生成它。
| 归档时间: |
|
| 查看次数: |
813 次 |
| 最近记录: |