开发者

Conditional Breakpoint Error - Type does not implement selector max and signature

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 s开发者_开发技巧uccess.


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.


Most of the time It will happen when you call a static method inside same scope with class reference prefix

Example

public final class SomeUtilClass{
    public static boolean utilMethod1() {
     //
    }

    public static boolean utilMethod2() {

        if(SomeUtilClass.utilMethod1()) {
           // 
        }
    }
}

in the above example with in the utilMethod2 we have used utilMethod1 with prfixing the class name (ie SomeUtilClass.utilMethod1()) which is unnecessary since both are in the same scope, it is just like a calling a local private method,

In this case the prifix SomeUtilClass." is not needed, it will cause these kind of problem while debugging if we remove "SomeUtilClass." prfix inside the if condition it will work just fine.. since utilMethod2 does not need any class reference to call utilMethod1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜