Created
July 21, 2020 09:46
-
-
Save onero/54f61293d9775deb8ca3b71ecc4e1164 to your computer and use it in GitHub Desktop.
Equals Template for IntelliJ using instanceof, instead of getClass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#parse("equalsHelper.vm") | |
public boolean equals(## | |
#if ($settings.generateFinalParameters) | |
final ## | |
#end | |
Object $paramName){ | |
#addEqualsPrologue() | |
#addClassInstance() | |
return ## | |
#set($i = 0) | |
#foreach($field in $fields) | |
#if ($i > 0) | |
&& | |
#end | |
#set($i = $i + 1) | |
#if ($field.primitive) | |
#if ($field.double || $field.float) | |
#addDoubleFieldComparisonConditionDirect($field) ## | |
#else | |
#addPrimitiveFieldComparisonConditionDirect($field) ## | |
#end | |
#elseif ($field.enum) | |
#addPrimitiveFieldComparisonConditionDirect($field) ## | |
#elseif ($field.array) | |
java.util.Arrays.equals($field.accessor, ${classInstanceName}.$field.accessor)## | |
#elseif ($field.notNull) | |
${field.accessor}.equals(${classInstanceName}.$field.accessor)## | |
#else | |
java.util.Objects.equals($field.accessor, ${classInstanceName}.$field.accessor)## | |
#end | |
#end | |
; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is for adding a equals template to IntelliJ (v14.1+) that uses an instanceof implementation, instead of getClass, in the case that a non-final class overrides of the equals method.
To read more about this issue I can recommend the following sources:
instanceof or getClass
instanceof versus getClass in equals Methods