Question: How do you find the size of a Collection (ie a `Set` or `List`) using reflection in java?

I have something like the example below and am wondering how to find the size of the collection while I'm using reflection.

**edit:** 

```
Class<?> clazz = node.getClass();
Field [] fields = clazz.getDeclaredFields();

for(Field field : fields) {
    System.out.println("declared fields:   "+ field.getType().getCanonicalName());

    //getting a generic type of a collection
    Type returntype = field.getGenericType();
    if (returntype instanceof ParameterizedType) {
        ParameterizedType type = (ParameterizedType) returntype;
        Type[] typeArguments = type.getActualTypeArguments();
        for(Type typeArgument : typeArguments) {
           Class<?> classType = (Class<?>) typeArgument;
           System.out.println("typeArgClass = " + classType.getCanonicalName());
        }
    }
}

```

Answer: 
Assuming `node` is the collection instance.

```
int size;
try { 
  size = (Integer) node.getClass().getMethod("size").invoke(node);
} catch (Exception e) {
  e.printStackTrace();
}

```

Doesn't make much sense to do this via reflection when you just can call `node.size()`, though.
The Scenario would be implementing a generic class that gives you a brief summary of the fields for an object and for the collection instance, you just want to spit out the size. 
I personally needed this to compare certain fields on two objects (o1 and o2) of the same type. I wanted to know if the collection instances have changed. 

```
Field f1 = null;

   try {
f1 = o1.getClass().getDeclaredField(field);
   }
...

if (Collection.class.isAssignableFrom(f1.getType())) { // Making sure it is of type Collection
   int v1Size = Collection.class.cast(v1).size();   //  This is what you need

```