26

I have a utility method that goes through various classes and recursively retrieves the fields. I want to check if that field is a Collection. Here is some sample code:

void myMethod(Class<?> classToCheck)

Field[] fields = classToCheck.getDeclaredFields();

for(Field field:fields)
{
   // check if field if a Collection<?>

}

Thanks in advance for the help.

Robin
  • 34,480
  • 5
  • 43
  • 90
Quantum_Entanglement
  • 1,393
  • 6
  • 20
  • 31
  • 1
    Do you want to test whether the declared type of the field is an implementation of Collection, or whether the actual object referred to by the field in any given object implements Collection? The two results would differ if a) the Field was declared as Object, but an object implementing Collection was assigned to it, or b) the Field was declared as Collection (or a descendant) but it was null. – DJClayworth Dec 07 '11 at 22:32
  • possible duplicate of [Howto find out if a field is instanceof a type via reflection?](http://stackoverflow.com/questions/2262322/howto-find-out-if-a-field-is-instanceof-a-type-via-reflection) – Dave Jarvis Jan 27 '15 at 22:55

6 Answers6

83
if (Collection.class.isAssignableFrom(field.getType())) {

}
Bozho
  • 554,002
  • 136
  • 1,025
  • 1,121
8

You should use Class.isAssignableFrom:

if (Collection.class.isAssignableFrom(field.getType())
    ...
Kirk Woll
  • 70,584
  • 19
  • 171
  • 185
2

Using the getType() method

Field field =  ...;
if ( Collection.class.isAssignableFrom( field.getType() ) ){
  //do something with your collection
}
Bozho
  • 554,002
  • 136
  • 1,025
  • 1,121
Robin
  • 34,480
  • 5
  • 43
  • 90
  • 2
    nope, that's not it. This will get the class that owns the field, rather than the field type – Bozho Dec 07 '11 at 22:20
  • Indeed. I just noticed my mistake by actually reading the javadoc. So far relying on my memory. Corrected my answer – Robin Dec 07 '11 at 22:21
1

//This execute if

 List<String> cashType = split(" ,AlL ");
 if(cashType instanceof Collection){
     System.out.println("cashType instanceof Collection");
 }else{
     System.out.println("cashType is not instanceof Collection");
 }

//This executes else

List<String> cashType = split(" ,AlL ");
 if(cashType instanceof Hashtable){
     System.out.println("cashType instanceof Collection");
 }else{
     System.out.println("cashType is not instanceof Collection");
 }
0
for(Field field:fields) { // check if field if a Collection
  Object myInstance = field.get(classInstance); 
    // where classInstance is the instance in which the fields is stored
    if(myInstance instanceof Collection) {
        //Do your thing
    }
}

This tests whether the actual object referred to by the field 'field' (of the object classInstance) implements Collection. If you want to tests whether the declared type of Field implements Collection then that will be different.

DJClayworth
  • 24,627
  • 8
  • 50
  • 71
0

You can use getType() in the following way:

if (field.getType().equals(Collection.class) {
    // Do something
}

This will only work if the field is declared as a Collection. It will not work if the field is a subtype of Collection, such as List or Vector.

ampersandre
  • 2,936
  • 3
  • 27
  • 36