Compiler bug involving vararg of length 0?
Zhong Yu
zhong.j.yu at gmail.com
Fri Mar 22 08:46:25 PDT 2013
The following code looks type safe, and compiles fine (jdk7u2b11)
@SafeVarargs
static <E> E[] newArray(int length, E... array)
{
return Arrays.copyOf(array, length);
}
public static void main(String[] args)
{
List<String>[] array = newArray(4);
}
but at runtime it throws
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast
to [Ljava.util.List;
Apparently, a new Object[0] is passed in as the vararg. Should that be a bug?
If the type argument is explicitly provided
List<String>[] array = Test.<List<String>>newArray(4);
no runtime exception. But shouldn't it be equivalent to the inferred case?
Zhong Yu
More information about the compiler-dev
mailing list