Elou everybody!
I need to say sth ; dont really know if it was right maybe bug in Castor but
a little fix in one class helped me a lot.
What I wanted to achive ....well. I had an abstract class. It was a field of
another class so I could write a handler for creating objects of its type
using factory method. Abstract class contained a map of some attributes. Key
for a map was type of string and value was an array of objects of type of
Abstract class. And here it goes : my factory created for me different types
of classes but all of them inherited from my Abstarct class. I've been
getting one annoying exception from Castor and could not figure out what was
wrong. It (exception) was : "Could not add "sth" to <sth> beacouse of : type
conversion cannot set value of type "myhandler" with value of type sth....
something like this. I spent some time trying to solve this and did it.
There's one class in Castor for arrays : J1CollectionHandlers and I think
it's wrong written or I scamped mapping or sth and had to change this class
;)
public Object add( Object collection, Object object ) {
Object[] array;
if ( collection == null ) {
// If the collection is of primitive type, the
instantiation
// (if it was null) is handled in the FieldHandlerImpl.
We
// can rely here that we deal only with array of object.
(*) Object newArray = Array.newInstance(object.getClass(), 1);
<---- here is (IMHO) BUG
Array.set(newArray, 0, object);
return newArray;
}
...
}
I need to change (*) line to this : Object newArray =
Array.newInstance(Object.class, 1);
Question is why....so here is the answer :
Whem map was empty and first instance of my Abstract class was created
Castor executed its handler for mapItem and above method.
Let's assume that my new class was type B (inherites from Abstarct class) so
newArray is created with type of B. But another instance of
my Abstract class is created - C (inherites from B or Abstract class) and :
collection is not null so :
Class type = collection.getClass();
type = type.getComponentType();
Object newArray = Array.newInstance(type, Array.getLength(collection)+1);
for ( int i = 0 ; i < Array.getLength(collection) ; ++i )
Array.set(newArray, i, Array.get(collection, i ));
Array.set(newArray, Array.getLength(collection), object); <---
IllegalArgumentException : array element type mismatch
Well - please correct me if I'm wrong or confirm existence of a bug
Thanks
Adrian .......still wating for reply ;)
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-user