0

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.

The Impaler
  • 45,731
  • 9
  • 39
  • 76
  • 5
    Well you simply can implement unit tests for each custom class where you tests for `equals`. – Herr Derb Oct 25 '17 at 15:26
  • 1
    @HerrDerb but how do you ensure those tests are implemented? – Andy Turner Oct 25 '17 at 15:28
  • 1
    @AndyTurner Well I guess that's very bad quality than. How do you ensure good quality? That's surly not the responsibility of a compiler. – Herr Derb Oct 25 '17 at 15:30
  • 1
    Some tools, such as SonarQube, will warn you if you use an unsuitable class as a map key. SonarQube typically runs as part of the continuous integration process. By the way, note that it's not enough just to define `equals` and `hashCode`. You also must ensure that the key is not modified while it is inside a map; preferably by making its class immutable. – DodgyCodeException Oct 25 '17 at 15:30
  • 1
    I was reading the other question and I (subjectively) don't think this is a duplicate. My interest is not on "forcing" the implementation, but just to find out if it's missing. In simple words I would like to see the red flag popping out in Jenkins. – The Impaler Oct 25 '17 at 15:34
  • @HerrDerb I didn't say it had to be enforced by the compiler; I am just pointing out that "well, just write tests" isn't really any different an approach from "well, just override equals", because a) it relies on *some* means of checking that it's done; b) it relies upon you implementing something correctly. It's easy to write tests which check *something* about your equals/hashCode implementations; that doesn't mean they work. – Andy Turner Oct 25 '17 at 15:40
  • @HerrDerb more importantly, unit tests of the custom class won't actually check that all of the classes used as map keys have implementations of equals/hashCode. They would merely check that if *that* class is used as a key, it stands a better chance of working. – Andy Turner Oct 25 '17 at 15:44
  • Nope, I still think this question is not a duplicate of either of those. If we had top level developers those would work. If we just could have any tool that would help us see when there's a problem? – The Impaler Oct 25 '17 at 15:52
  • @TheImpaler if you're asking for a tool recommendation, that's off-topic. You *have* been given a tool recommendation in the comments, however. – Andy Turner Oct 25 '17 at 16:03

0 Answers0