I have something like:
Field [] fields = claz.getDeclaredFields();
for(Field f : fields){
f.setAccessible(true);
if(f.getType().equals(List.class)){
Method m = f.getType().getMethod("size");
int length = (Integer)m.invoke(f.get(node));
System.out.println("length "+ length);
}
}
it does work, but I’m wondering to know if there is any other good approaches, like something that will work for all other collections (TreeSet, HashSet, etc…), or the only way is that, I have to check for each type like what I already have?
thanks
In a
Fieldobject you could retrieve the Type and pass it as parameter for the methodisAssignableFromofCollection.class.Like:
From documentation the method
isAssignableFrom:The same should be done with
Map: