Domanda

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?
È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top