The code below compiles fine but throws an exception at runtime. Is this the expected behaviour and why?
Code:
public static void main(String[] args) {
A<Integer> a = new A<> ();
System.out.println(a.min()); //prints null as expected
System.out.println(a.max()); //throws exception
}
static class A<T extends Number & Comparable<? super T>> {
Stream<T> s = Stream.empty();
public T min() { return s.min((t1, t2) -> t1.compareTo(t2)).orElse(null); }
public T max() { return s.max(T::compareTo).orElse(null); }
}
Output:
null
Exception in thread "main" java.lang.BootstrapMethodError: call site initialization exception
at java.lang.invoke.CallSite.makeSite(CallSite.java:341)
at java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(MethodHandleNatives.java:307)
at java.lang.invoke.MethodHandleNatives.linkCallSite(MethodHandleNatives.java:297)
at abc$A.max(abc.java:19)
at abc.main(abc.java:8)
Caused by: java.lang.invoke.LambdaConversionException: Invalid receiver type class java.lang.Number; not a subtype of implementation type interface java.lang.Comparable
at java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(AbstractValidatingLambdaMetafactory.java:233)
at java.lang.invoke.LambdaMetafactory.metafactory(LambdaMetafactory.java:303)
at java.lang.invoke.CallSite.makeSite(CallSite.java:302)
... 4 more