Casting reference array to any-T array.

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu Jan 8 00:00:05 UTC 2015


On 07/01/15 21:24, Brian Goetz wrote:
> Yes, this is definitely unsound (and probably we should help by 
> failing at compile time rather than runtime.)
Yep - this is a javac bug; I'll look into this.

Maurizio
>
> 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