Compiler bug involving vararg of length 0?

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Fri Mar 22 09:19:11 PDT 2013


I agree this looks bogus - I'll have a look.

Maurizio

On 22/03/13 15:46, Zhong Yu wrote:
> 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