throw new IllegalArgumentException(errorMessage, except);
} catch (IllegalAccessException except) {
// This should never happen
String errorMessage =
Messages.format("mapping.schemaChangeNoAccess", toString());
throw new CastorIllegalStateException(errorMessage, except);
} catch (InvocationTargetException except) {
// This should never happen
throw new MappingRuntimeException(except.getTargetException());
}
} else if ( value != null ) {
Object collect;
try {
// Get the field value (the collection), add the value to it,
// possibly yielding a new collection (in the case of an array),
// and set that collection back into the field.
if ( _handler != null ) {
collect = _handler.getValue( object );
collect = _colHandler.add( collect, value );
if ( collect != null )
_handler.setValue( object, collect );
} else if ( _field != null ) {
collect = _field.get( object );
if (collect == null) {
// The type of the collection.
Class type = _field.getType();
//-- Handle Arrays, we need to declare the array with
//-- the correct type. The other cases are handled in
//-- the J1CollectionHandler during the
//-- add(collect,value) call
if (type.isArray()) {
Class componentType = type.getComponentType();
Class valueType = value.getClass();
if (componentType.isPrimitive() ||
((!valueType.isArray()) && (valueType != componentType)))
{
try {
collect = Array.newInstance(componentType, 0);
}
catch (Exception e) {
String err = "Unable to instantiate an array of '" +
componentType + "' : " + e;
throw new CastorIllegalStateException(err, e);
}
}
}
}
collect = _colHandler.add( collect, value );
if ( collect != null )
_field.set( object, collect );
}
else if ( _getMethod != null ) {
if ( _getSequence != null )
for ( int i = 0; i < _getSequence.length; i++ )
object = _getSequence[ i ].invoke( object, (Object[]) null );
collect = _getMethod.invoke( object, (Object[]) null );
// If we deal with a collection who is an array of primitive
// and that has not been instantiated, we have to handle the
// instantiation here rather than in J1CollectionHandler,
// because we have acces to the Field object here.
boolean setCollection = false;
if (collect == null) {
// The return type of the get method should be the type of the collection.
Class type = _getMethod.getReturnType();
//-- Handle Arrays, we need to declare the array with
//-- the correct type. The other cases are handled in
//-- the J1CollectionHandler during the
//-- add(collect,value) call
if (type.isArray()) {
Class componentType = type.getComponentType();
Class valueType = value.getClass();
if (componentType.isPrimitive() ||
((!valueType.isArray()) && (valueType != componentType)))
{
try {
collect = Array.newInstance(componentType, 0);
}
catch (Exception e) {
String err = "Unable to instantiate an array of '" +
componentType + "' : " + e;
throw new CastorIllegalStateException(err, e);
}
}
}
setCollection = true;
}