I am using introspection to retrieve a beans get methods and i’d like to be able to determine if the result is an Array or a Primitive, but I always get a false response. Even when I know that the type is an ArrayList. On that same note, will isArray() work for all types of Arrays such as primitive Array, ArrayList etc?
Class<?> className = bean1.getClass();
ArrayList<ComparatorValue> updateIndexes = new ArrayList<ComparatorValue>();
BeanInfo beanInfo = Introspector.getBeanInfo(className);
PropertyDescriptor classProperties[] = beanInfo.getPropertyDescriptors();
for(int i=0; i<classProperties.length; i++) {
Method getter = classProperties[i].getReadMethod();
System.out.println(getter.invoke(bean1).getClass().isArray()); //Always false.
System.out.println(getter.invoke(bean1).getClass().isPrimitive()); //Always false.
}
When you invoke the method, a primitive return value will get boxed into its wrapper class.
Since you are calling
getClasson the wrapped return value,isPrimitvewill return false. You can retrieve the actual return type by callinggetter.getReturnType().