Let's suppose you're writing a library, and it is to act upon a client class that is passed to it (i.e. client code cannot be changed).
class ClientClass {
public GenericClientClass<AnotherClientClass> someField;
}
What I'm trying to do is instantiate someField through reflection. Something like:
class LibraryClass{
public instantiateAllFields(Object obj){
//iterate through all fields of obj.class, and instantiate them
}
}
The only requirement of the client's fields is that a constructor without arguments exists. (e.g. GenericClientClass() {} )
The purpose of this is to create a highly automated testing library, currently for my own purposes.
I've been able to implement the above for almost all cases (arrays, objects, primitives, generic arrays etc.); however, this particular case has stumped me.
field.getType().newInstance() does not work, since the generic type parameters are removed.
field.getGenericType() doesn't work, because it returns a type, not a class.
If It's a known generic class, I can create a special case to deal with it. For example:
Class<?> genClass = ((Class<?>)((ParameterizedType)field.getGenericType()).getActualTypeArguments()[0]);
JArray jArray = new JArray(true,n,genClass);
field.set(object, jArray);
...I'm guessing I could do this for things like ArrayList, HashMap etc.
However, I would like to cover the general case, and be able to instantiate any generic class (with a single parameter, and a zero-argument constructor) that may be passed into the library.
Any ideas?
Note that the type of the generic parameter is known. I can get at it through field.getGenericType().
Aucun commentaire:
Enregistrer un commentaire