Generate Multiple Getters in Template
已回答
I was trying generate an encrypted and unencrypted getter/setter, the encrypted is public and then I have one to unencrypt which is a protected. I wanted to use the generate getter/setter and based on the attribute name ( in this case if they contain the word key or pass in them ) it would automatically generate both. However it
appears to be limited to only returning one method, is there any way around this? VTL code is below.
Thanks in advance,
Tim.
public ##
#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)
#if ($StringUtil.startsWithIgnoreCase($name, 'is'))
#set($name = $StringUtil.decapitalize($name))
#else
is##
#end
#else
get##
#end
${name}() {
return $field.name;
}
#if ((($StringUtil.containsIgnoreCase(${field.name},"pass")) || $StringUtil.containsIgnoreCase(${field.name},"key")) && !($field.boolean && $field.primitive))
protected ##
#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
getUnencrypted${name}() {
return EncryptedStringHelper.decrypt($field.name);
}
#end
请先登录再写评论。
Hi, Tim.
there is no way to generate both methods simultaneous.
You may create 2 different templates and generate encrypted methods manually when applicable. Also you may create a plugin with this functionality - e.g. JavaFX generates multiple getters for its properties.
The problem with this functionality is how to separate velocity templates from each other, without making the configuration ofl templates more complex (I believe that it's not a common case to create multiple getters, please correct me if I am wrong)
Thanks
Actually, you can't do this - I tried to generate two different setter types and (a fluent and a traditional) and, as Tim says you get a Velocity error in that case. So I tried to create a different profile - one that has fluent and one that has traditional but now IntelliJ says that the class already has setters. It seems a poor design that I get one shot at it or I have to write plugins for a proprietary system.