Casting reference array to any-T array.

Brian Goetz brian.goetz at oracle.com
Wed Jan 7 21:24:02 UTC 2015


Yes, this is definitely unsound (and probably we should help by failing 
at compile time rather than runtime.)

To fix your code, use the "new T[n]" syntax that gives you an array of 
the erasure of T for ref T and primitive arrays for primitive T (and an 
unchecked warning.)

On 1/7/2015 4:19 PM, Michael Barker wrote:
> Hi,
>
> A small thing that tripped me up yesterday.  The following code:
>
> package github.mikeb01;
>
> public class C<any T>
> {
>      T[] values;
>
>      public C()
>      {
>          values = (T[]) new Object[0];
>      }
>
>      public static void main(String[] args)
>      {
>          new C<int>();
>      }
> }
>
> Compiles without error, but fails a runtime with:
>
> Specializing github.mikeb01.C${0=I}; searching for github/mikeb01/C.class
> (not found)
> Specializing github.mikeb01.C${0=I}; searching for github/mikeb01/C.class
> (found)
> Exception in thread "main" java.lang.IllegalStateException: [I
> at
> valhalla.specializer.SignatureSpecializer.asClassName(SignatureSpecializer.java:79)
> at
> valhalla.specializer.SignatureSpecializer$ForType.getDescAsClassName(SignatureSpecializer.java:156)
>
> Should that be a compile time failure, with an invalid cast from a
> reference array to any-T array?
>
> Mike.
>



More information about the valhalla-dev mailing list