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
if (node instanceof Collection) length = ((Collection)node).size()
? – eran Jun 25 '12 at 19:48if (f.get(node) instanceof Collection)
, so reflection would still be used to access the field. – Marko Topolnik Jun 25 '12 at 19:50node
is the object whose fields are checked... – eran Jun 25 '12 at 19:55