I'm doing a mapping using reflection.
With reflection I can know if each of the the Field
s of a Class refers to an attribute of type "array", and also I can get the Class with the type of the array.
Now, I want to set the value of the field of an object. Now, for some reason, I can get the data to fill the object field, but it's not in the form of an array. Is, instead, a Collection<?>
.
Just to make it easy to visualize, I have some code like that:
/** create a new T instance, with the data taken from something*/
<T> T convert(Class<T> cls, Object something) {
T obj = cls.newInstance();
for (Field field : cls.getFields()) {
if (field.getType().isArray()) {
Class<?> arrayType = field.getType().getComponentType();
Collection<?> collection = this.getData(arrayType, something);
Object array = CONVERT(collection);
field.set(obj, arrayData);
}
}
}
At the end of the day, I need to convert a Collection<?>
object to (hypotetically) a ?[]
object, where "?" is dinamically correct.
I know there is collData.toArray()
, but it builds an Object[]
, witch is incompatible; and I know there is collData.toArray(T[])
, but I have no idea of how to use it in this context.
Aucun commentaire:
Enregistrer un commentaire