Java Class.isAssignable Due to Confusion

I find a primitive type problem

System.out.println("Integer.class.isAssignableFrom(int.class) = " + Integer.class.isAssignableFrom(int.class)); System.out.println("int.class.isAssignableFrom(Integer.class) = "+int.class.isAssignableFrom(Integer.class)); 

both statements return false to the caller. so it seems that boxing is not applicable here. My question is, is my observation correct or is there another API that can correctly perform this testing?

-------------------------------- continued ------------- ---- ----------------------------

As I said, I basically want to check if Object can be assigned to a field when using reflection. I hope the mechanism can be more accurate at runtime, so I made such an implementation.

  public static boolean isAssignableFrom(final Field field, final Object obj) { if (field.getType().equals(Integer.class) || field.getType().equals(int.class)) { return obj.getClass().equals(Integer.class) || field.getType().equals(int.class); } else if (field.getType().equals(Float.class) || field.getType().equals(float.class)) { return obj.getClass().equals(Float.class) || field.getType().equals(float.class); } else if (field.getType().equals(Double.class) || field.getType().equals(double.class)) { return obj.getClass().equals(Double.class) || field.getType().equals(double.class); } else if (field.getType().equals(Character.class) || field.getType().equals(char.class)) { return obj.getClass().equals(Character.class) || field.getType().equals(char.class); } else if (field.getType().equals(Long.class) || field.getType().equals(long.class)) { return obj.getClass().equals(Long.class) || field.getType().equals(long.class); } else if (field.getType().equals(Short.class) || field.getType().equals(short.class)) { return obj.getClass().equals(Short.class) || field.getType().equals(short.class); } else if (field.getType().equals(Boolean.class) || field.getType().equals(boolean.class)) { return obj.getClass().equals(Boolean.class) || field.getType().equals(boolean.class); } else if (field.getType().equals(Byte.class) || field.getType().equals(byte.class)) { return obj.getClass().equals(Byte.class) || field.getType().equals(byte.class); } return field.getType().isAssignableFrom(obj.getClass()); } } 

It seems the best I can do -_-! thanks

+6
source share
3 answers

I suppose ClassUtils.isAssignable(Class, Class, boolean) from Apache commons-lang is the one that might help.

Javoc

+7
source

int.class and Integer.class are two separate class objects. check this answer for more details

From Java doc Class#isAssignableFrom

Determines whether the class or interface represented by this class object is either the same or the superclass or superinterface of the class or interface represented by the specified class parameter. It returns true if so; otherwise it returns false. If this class object represents a primitive type, this method returns true if the specified class parameter is this class object; otherwise, it returns false.

+5
source

From the documentation in isAssignableFrom :

this method checks whether the type represented by the specified class parameter can be converted to the type represented by this class object, by means of identifier conversion or by using an extended reference transformation. See the Java Language Specification, sections 5.1.1 and 5.1.4 for details.

Integer cannot be assigned int (or vice versa) this way, so your method will return false - boxing and unboxing are executed at compile time rather than at run time - see this article for more information about this

+3
source

All Articles