11

I find 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 of the statements return false to the caller. so that seems like boxing is not applicable here. My question is if my observation is correct or there are other API who can do this testing correctly?

--------------------------------following up---------------------------------------------

As I said, I mainly want to check if a Object is assignable to a Field when using reflection. I hope the mechanism could be more precise at run time so I made a implementation like this.

 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()); } } 

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

7
  • Isn't autoboxing is accomplished at compile time? Commented Jan 4, 2013 at 6:02
  • I am not sure, I mean statement Integer a = 1 is compilable. Commented Jan 4, 2013 at 6:09
  • 1
    possible duplicate of Does int.class equal Integer.class or Integer.TYPE in Java? Commented Jan 4, 2013 at 6:10
  • @Korben - autoboxing is a compile time process that boxes or unboxes instances. It doesn't apply to classes at all, which is what the isAssignableFrom method operates on. Key thing to keep in mind is - instances can be autoboxed, classes cannot. Commented Jan 4, 2013 at 6:15
  • 1
    @Korben - I'm unsure as to what you are trying to test. Are you trying to verify that autoboxing is available in the version of Java you are using? Or something else? Commented Jan 4, 2013 at 6:22

4 Answers 4

11

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

JavaDoc

Sign up to request clarification or add additional context in comments.

1 Comment

The Apache method will work but will also allow for long is assignable from int to be true as well. You could just attempt to cast the object to the type yourself and then catch the classcastexception if it fails.
6

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

From Java doc Class#isAssignableFrom

Determines if the class or interface represented by this Class object is either the same as, or is a 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 exactly this Class object; otherwise it returns false.

Comments

3

From the documentation on isAssignableFrom:

this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.

Integer cannot be assigned to an int (or vice versa) through this way, so your method will return false - boxing and unboxing are done at compile time, not at runtime - see this article for more info on it

2 Comments

but final Integer a = 1; final int b = new Integer(2); are OK
This is allowed only as a convenience - the compiler will basically turn that into final Integer a = Integer.valueOf(1); and final int b = new Integer(2).intValue() (there may be some optimization in there, but that's the gist of how autoboxing works).
0

Just a hint, the code you pasted above has a bug in it. If you call the method with "field= int.class", it does not matter what type object is, the method will always return true. Seems to be a copy and paster misstake ;)

if (field.getType().equals(Integer.class) || field.getType().equals(int.class)) { return obj.getClass().equals(Integer.class) || field.getType().equals(int.class); 

better would be:

if (field.getType().equals(Integer.class) || field.getType().equals(int.class)) { return obj.getClass().equals(Integer.class) || obj.getType().equals(int.class); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.