Bug 734 : splice throws ClassCastException when used with an array of custom objects
Last modified: 2008-02-20 14:35




Status:
RESOLVED
Resolution:
FIXED -
Priority:
P1
Severity:
normal

 

Reporter:
antiplastik
Assigned To:
fry

Attachment Type Created Size Actions

Description:   Opened: 2008-02-19 13:01
version 0135

when used with an array of custom objects - say myClass[] - the splice()
method can't be cast to this type, even though using the recommended
statement : (myClass[])splice(...);

code :

void setup() {
Sample[] a = new Sample[]{new Sample(1), new Sample(2), new Sample(3)};
Sample[] b = new Sample[]{new Sample(4), new Sample(5)};
Sample[] c;
c = (Sample[])splice(a, b, a.length); // throws ClassCastException
}

class Sample {
int i;
Sample(int i) { this.i = i; }
}
Additional Comment #1 From fry 2008-02-20 14:34
whups, that was a typo in the splice() code...i'll have the fix in 0136.
thanks for the report, i'm surprised it's taken this long for someone to
find that...

a fixed version you can use until then:

static public Object splice2(Object list, Object v, int index) {
Object[] outgoing = null;
int length = Array.getLength(list);

// check whether item being spliced in is an array
if (v.getClass().getName().charAt(0) == '[') {
int vlength = Array.getLength(v);
outgoing = new Object[length + vlength];
System.arraycopy(list, 0, outgoing, 0, index);
System.arraycopy(v, 0, outgoing, index, vlength);
System.arraycopy(list, index, outgoing, index + vlength, length - index);

} else {
outgoing = new Object[length + 1];
System.arraycopy(list, 0, outgoing, 0, index);
Array.set(outgoing, index, v);
System.arraycopy(list, index, outgoing, index + 1, length - index);
}
return outgoing;
}