我知道有很多用于生成Builder模式类的插件。但如果我上课怎么办?
public class User {
private String name;
}
并想添加方法:
public class User {
private String name;
public User withName(String name){
this.name = name;
return this;
}
}
是否可以生成这样的 withXYZ 方法?
解决
转到Template Dialog了解setter并添加自己的模板:
#set($paramName = $helper.getParamName($field, $project))
public ##
#if($field.modifierStatic)
static void ##
#else
$classname ##
#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
}
答案 0 :(得分:1)