Skip to content

Instantly share code, notes, and snippets.

@onero
Created July 21, 2020 09:46
Show Gist options
  • Save onero/54f61293d9775deb8ca3b71ecc4e1164 to your computer and use it in GitHub Desktop.
Save onero/54f61293d9775deb8ca3b71ecc4e1164 to your computer and use it in GitHub Desktop.
Equals Template for IntelliJ using instanceof, instead of getClass
#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
;
}
@onero
Copy link
Author

onero commented Jul 21, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment