For an int
field getType
will return int.class
. This has been the case since before auto-
boxing was introduced to Java and so, correctly, if you're preserving backward compatibility, Class.isAssignableFrom(Class)
returns false
when the object type is passed the primitive type.
Or as in your case:
int.class.isAssignableFrom(int.class)
would return true
, while:
int.class.isAssignableFrom(Integer.class)
would return false
.
The quick fix it to write a method that when presented with the object type, checks for the that type and the primitive or use the classes in libraries like Jakarta Commons (ClassUtils.isAssignable(Class, Class, boolean)).