Apache Commons BeanUtils get list properties
In Apache Commons BeanUtil, how to get a type inside a list ? for example
class Track {
List<Actor> actorList = new ArrayList<Actor>();
}
System.err.println(PropertyUtils.getPropertyType(trackBean, "actorList"));
// it should give 开发者_JAVA技巧me Actor instead of java.util.List
Thanks.
I don't know if it's possible with beanutils. But you can do so with reflection.
Field field = Track.class.getDeclaredField("actorList");
ParameterizedType pt = (ParameterizedType) field.getGenericType();
Class clazz = (Class) pt.getActualTypeArguments()[0];
You will perhaps need a few check above (whether you can cast, whether the actual type arguments exist, etc), but you get the idea.
Type information is erased at runtime, unless it is structural - e.g. the type argument of a field, or of class.
精彩评论