Korben
Korben

Reputation: 736

Java Class.isAssignableFrom confusion

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

Upvotes: 11

Views: 14463

Answers (4)

user3525375
user3525375

Reputation: 1

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

Upvotes: 0

Miro Hudak
Miro Hudak

Reputation: 2215

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

JavaDoc

Upvotes: 11

Krease
Krease

Reputation: 16225

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

Upvotes: 3

Amit Deshpande
Amit Deshpande

Reputation: 19185

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.

Upvotes: 6

Related Questions