user595234
user595234

Reputation: 6249

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 me Actor instead of java.util.List

Thanks.

Upvotes: 1

Views: 4612

Answers (1)

Bozho
Bozho

Reputation: 597076

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.

Upvotes: 4

Related Questions