Custom equals/hascode template
Answered
Hello
is it possible to change template for generating equals and hashCode methods? in example instead of inline 'if' do it with curly braces? Or with usage of class Objects?
Please sign in to leave a comment.
Hello,
It's possible to create custom templates from "Generate" wizard: https://www.jetbrains.com/help/idea/generate-equals-and-hashcode-wizard.html
I know this is a bit old, but ran in to the same problem and the above link just simply seem to point to verbose documentation and then to the Velocity project for info on additional customizations. Not very helpful when you are looking for a simple customization such as the above. After playing around with it a bit, managed to create a customized template by removing the `#addEqualsPrologue()` line and replacing it with the following template:
if ($paramName == this) {return true;
}
if (!($paramName instanceof $class.name)) {
return false;
}
Templates used for generating getters and setters, as well as
equals(),hashCode(), andtoString()methods are written in the Velocity template language.More information on using VTL syntax in code templates in the IntelliJ IDEA can be found here:
How or where can I edit the output of #addEqualsPrologue() ?
You can't, it's hardcoded: https://github.com/JetBrains/intellij-community/blob/master/plugins/groovy/src/org/jetbrains/plugins/groovy/actions/generate/equals/GroovyGenerateEqualsHelper.java#L184.