I'm trying to run the following code which is compiled fine under JDK8 thanks to type inference:
public static <A,B> B convert(A a) {
return (B) new CB();
}
public static void main(String[] args) {
CA a = new CA();
CB b = convert(a); //this runs fine
List<CB> bl = Arrays.asList(b); //this also runs fine
List<CB> bl1 = Arrays.asList(convert(a)); //ClassCastException here
}
However, running this throws ClassCastException: CB cannot be cast to [Ljava.lang.Object, but the CB b = convert(a) works fine.
Any idea why?
java.lang.VerifyError: Bad type on operand stack
with 1.8.0_92. Btw, using a type witness works for me:List<CB> bl1 = Arrays.asList(Test.<CA, CB>convert(a));
– Darnleyjava.lang.VerifyError
result. Forgot to mention that I've used the Eclipse builtin compiler. – DarnleyCB[]
instead ofObject[]
. Not much better. – DarnleyCB[]
rather than inferringCB
and do varargs processing. – Convulsive