I am trying to debug a piece of code of mine where (Integer) values in a map sometimes get surprisingly high.
When I do this
Collection<Integer> vals = newState.values();
int max = Collections.max(vals);
return newState; // breakpoint here
I can set a conditional breakpoint at the line of the return statement with a condition involving max, e.g. max > 10. When I leave out the middle line and set the breakpoint condition to Collections.max(vals) > 10, I get a runtime exception in debug mode.
"Reason: Type does not implement selector max and signature (Ljava/util/Collection;)Ljava/lang/Comparable;"
I can get what I want with the code fragment above, but I still wonder what is going on here. Guessing it could be the Collections method failing to realize that Integer is indeed comparable, I tried Collections.<Integer>max(v) and some (bad syntax) variations, without success.
Collections.max() has an interesting trick in its signature:
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
As you can see, extends Object looks redundant here. The trick is that during type erasure T turns into its first generic bound (i.e. Object), so that the actual erased signature of this method looks like this:
public static Object max(Collection coll)
It's made for retaining binary compatibility with pre-generic version of this method: code compiled against pre-generic version expects return type to be Object, and using this trick new version satisfies its expectation.
However, as you can see, debugger incorrectly assumes that erause of T is Comparable. Perhaps it's a bug in debugger.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With