String prop,
String colType,
boolean ignoreNotFoundField,
ValueAdapter valueAdapter)
{
FieldInfo fieldInfo = FieldInfo.getFieldInfo(o.getClass(), prop, !ignoreNotFoundField);
if(fieldInfo == null)
{
return;
}
Class fieldType = fieldInfo.getType();
if(valueAdapter != null)
{
value = valueAdapter.cast(value, fieldType);
}
if(Collection.class.isAssignableFrom(fieldType) &&
!Collection.class.isAssignableFrom(value.getClass()))
{
Collection col = (Collection)fieldInfo.getValue(o);
if(col == null)
{
if(colType == null)
{
col = new ArrayList();
}
else
{
Class colCls;
try
{
colCls = Thread.currentThread().getContextClassLoader().loadClass(colType);
}
catch(ClassNotFoundException e)
{
throw new JBossXBRuntimeException("Failed to load collection type: " + colType);
}
try
{
col = (Collection)colCls.newInstance();
}
catch(Exception e)
{
throw new JBossXBRuntimeException("Failed to create an instance of " + colCls);
}
}
fieldInfo.setValue(o, col);
}
//System.out.println("col.add(value): " + prop + "=" + value);
col.add(value);
}
/*
else if(fieldType.isArray() &&
value != null &&
(fieldType.getComponentType().isAssignableFrom(value.getClass()) ||
fieldType.getComponentType().isPrimitive() &&
Classes.getPrimitiveWrapper(fieldType.getComponentType()) == value.getClass()
))
{
Object arr = fieldInfo.getValue(o);
int length = 0;
if(arr == null)
{
arr = Array.newInstance(fieldType.getComponentType(), 1);
}
else
{
Object tmp = arr;
length = Array.getLength(arr);
arr = Array.newInstance(fieldType.getComponentType(), length + 1);
System.arraycopy(tmp, 0, arr, 0, length);
throw new JBossXBRuntimeException("copied array (2)");
}
Array.set(arr, length, value);
fieldInfo.setValue(o, arr);
}
*/
else
{
// todo: unmarshalling should produce the right type instead
Class valueClass = value == null ? null : value.getClass();
if (valueClass != null && fieldType.isArray() && Collection.class.isAssignableFrom(valueClass))
{
Collection col = (Collection) value;
Class compType = fieldType.getComponentType();
value = Array.newInstance(compType, col.size());
if (compType.isPrimitive())
{
int i = 0;
for (Iterator iter = col.iterator(); iter.hasNext();)
{
Array.set(value, i++, iter.next());
}
}
else
{
value = col.toArray((Object[]) value);
}
}
fieldInfo.setValue(o, value);
}
}