[concurrency-interest] ParallelArray classcastexception.

Neal Gafter neal at gafter.com
Sun Mar 9 18:05:17 EDT 2008


FJ is cheating with erasure, resulting in possible class cast exceptions
that demonstrate that the casts are not typesafe.  Specifically:

    public static <T> ParallelArray<T> create
        (int size, Class<? super T> elementType,
         ForkJoinExecutor executor) {
        T[] array = *(T[])*Array.newInstance(elementType, size);
        return new ParallelArray<T>(executor, array, size);
    }
    public static <T> ParallelArray<T> createEmpty
        (int size, Class<? super T> elementType,
         ForkJoinExecutor executor) {
        T[] array = *(T[])*Array.newInstance(elementType, size);
        return new ParallelArray<T>(executor, array, 0);
    }

These casts are not safe, as elementType is only guaranteed to be of some
supertype of T, not a subtype of T.

Elsewhere, there are Object[]s being cast to T[].  This isn't typesafe if
those arrays (or their types) are ever exposed, which they are.

This is the kind of code that will make it painful to add reification.

-Neal

On Sun, Mar 9, 2008 at 1:58 PM, Doug Lea <dl at cs.oswego.edu> wrote:

> Rune Schjellerup Philosof wrote:
> > Hello all
> >
> > How come this throws Exception in thread "Thread-5"
> > java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[F
> >
> >         Scan[] scans;
> >         ForkJoinPool fjp = new ForkJoinPool();
> >         ParallelArray<Scan> scanarray =
> > ParallelArray.createFromCopy(scans, fjp);
> >         ParallelArray<float[]> tmp = scanarray.withMapping(new
> > Ops.Op<Scan, float[]>() {
> >             @Override
> >             public float[] op(Scan scan) {
> >                 float[][] raw = scan.getSpectrum();
> >                 return Spectrum.Resample(raw, _mzRange,
> > _resamplingFrequency);
> >             }
> >         }).all();
>
> You should use the elementType version of "all" here. As in:
>   .all(float[].class);
> Otherwise the array is an Object array, where each of the
> objects here is a float[]. Which is fine except for the Java casting
> rules for arrays, which cause exception on your next line:
>
> >         float[][] resampledSpectra = tmp.getArray();   <--- exception
> >
>
> And actually, even this wasn't handled consistently, but now is
> in updated jar.
>
> -Doug
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20080309/66028298/attachment.html 


More information about the Concurrency-interest mailing list