In Intellij IDE you can writte your own templates for generating code. I would like to writte a template for generating my equals method.
The goal is to have a equals method that do not distiguish between null and a empty string like this:
(using StringUtils.IsNotEmpty from apache.commons.language)
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
POJO pojo = (POJO) o;
if(StringUtils.IsNotEmpty(stringAtt) ? !stringAtt.equals(pojo.stringAtt) : StringUtils.IsNotEmpty(pojo.stringAtt)) return false;
return true;
}
I would like to writte this equals as a template so I can generate code for all the POJOs I am using.
The problem is that I do not fully understand the syntax of language and tokens used for intellij template generation. It looks like this:
#parse("equalsHelper.vm")
public boolean equals(##
#if ($settings.generateFinalParameters)
final ##
#end
Object $paramName){
#addEqualsPrologue()
#if ($fields.size() > 0)
#addClassInstance()
#foreach($field in $fields)
#addFieldComparison($field)
#end
#end
return true;
}
##
#macro(addFieldComparison $field)
#if ($field.notNull)
if(!${field.accessor}.equals(${classInstanceName}.$field.accessor))return false;
#else
if($field.accessor != null ? !${field.accessor}.equals(${classInstanceName}.$field.accessor) : ${classInstanceName}.$field.accessor != null)return false;
#end
#end
##
I have looked for it on Intellij doc pages but I cant find a explanation for the syntax.
- Could someone give me a good example or point to some documentation webpage so I can learn how to do it?
- Or what language is this so I can learn it by myself
Thanks.