I was wondering if any knows hows to get the size of an array object using reflection?
I have a Vehicles component containing an array object of type Car.
Vehicles.java
public class Vehicles{
private Car[] cars;
// Getter and Setters
}
Car.java
public class Car{
private String type;
private String make;
private String model;
// Getter and Setters
}
I was wondering how I would be able to get the size of the cars array within the vehicles component using Java Reflection?
I current have the following:
final Field[] fields = vehicles.getClass().getDeclaredFields();
if(fields.length != 0){
for(Field field : fields){
if(field.getType().isArray()){
System.out.println("Array of: " + field.getType());
System.out.println(" Length: " + Array.getLength(field.getType()));
}
}
}
which results in the following error:
java.lang.IllegalArgumentException: Argument is not an array
at java.lang.reflect.Array.getLength(Native Method)
Any ideas?