Home > Blockchain >  Intellij custom getter template: How to encapsulate collection field
Intellij custom getter template: How to encapsulate collection field

Time:08-20

I want to generate a custom getter like this:

public List<String> getMyStrings() {
    if (this.myStrings == null)
        return Collections.emptyList();
    return myStrings;
}

with custom getter:

#if($field.modifierStatic)
static ##
#end
$field.type ##
#if($field.recordComponent)
  ${field.name}##
#else
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)
  is##
#else
  get##
#end
${name}##
#end
() {
    if (this.$field.name == null)
      return Collections.emptyList();   
  return $field.name;
}

but I want to apply this template on collecion fields only, not with normal fields. Any idea? Thanks.

CodePudding user response:

You can use a $field.collection expression. Alternatively $field.list, $field.map or $field.set could also be used. You can use code completion to see what else is available.

As an example, you can modify the template like this:

#if($field.modifierStatic)
static ##
#end
$field.type ##
#if($field.recordComponent)
  ${field.name}##
#else
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)
  is##
#else
  get##
#end
${name}##
#end
() {
  #if($field.collection)
  if ($field.name == null) return java.util.Collections.emptyList();
  #end
  return $field.name;
}
  • Related