1

As many people before, I hit IllegalArgumentException: Comparison method violates its general contract! I need to check the legacy code for all possible occurences of this. The best way would be a static analysis tool that will go through all the code and detect possible issues.


  1. How does TimSort detect these issues?
  2. Is it possible to run static analysis of the code and find the issues?
  3. Is there such a tool available? Maybe as a rule for already existing static analysis tool?
MartinTeeVarga
  • 10,478
  • 12
  • 61
  • 98

1 Answers1

1

It will detect them in the merge phase when it's merging two runs together. Typically it just means that your comparison function is not following the rules, things like both a < b and b < a being true at the same time, or getting true values from all of a > b, b > c and c > a.

I don't know of any static analysis tool that would help here but I'm not sure it would even be needed.

Your best bet is to just eyeball the comparison function and ensure it returns the correct value for various data sets. Actually finding the comparison functions should be relatively simple by just searching all files for Comparator.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thanks, I was afraid of that. Actually I need to find all `Comparator` and `Comparable`, which is of course not a problem. Just that there are 379 of them. – MartinTeeVarga Jun 26 '13 at 04:01
  • @sm4, wow, that's ... quite a lot, even for a _big_ application. One has to wonder whether they're all really necessary. – paxdiablo Jun 26 '13 at 04:03
  • Unfortunate legacy. My best bet will probably be to identify those that are not covered by any tests and reduce this number. – MartinTeeVarga Jun 26 '13 at 04:07