In our Java projects we tend to use quite a lot of data structures using Map<K, V>
and Set<E>
where the keys K
and E
are not simple existing classes (like Integer or String), but classes we develop: think of a new class CarVO
as the key of a Map.
When we do that we need to make sure we do it safely, that is we need to make sure we implement equals()
and hashCode()
in the new class CarVO
. However, by default Java does not force us to do it, since java.lang.Object
already implements them with a default logic that is pretty much useless for the project. This can go unnoticed.
The thing is, the default implementation for these methods adds real risk to our projects.
Do you guys know any way of forcing the compiler, another tool, or code analiser (maybe checkstyle?) to find if any of the developers "forgot" to implement equals() and hashCode() on a class used in a Map<> or Set<>?
I know there may be no a clean solution, but any (preferrably automated through Jenkins or Ant) workaround will surely help.
Thank you.