Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 833
Korben Avatar asked Jan 04 '13 05:01

Korben


People also ask

What is isAssignableFrom in Java?

The isAssignableFrom() method of java. lang. Class class is used to check if the specified class's object is compatible to be assigned to the instance of this Class. It will be compatible if both the classes are the same, or the specified class is a superclass or superinterface.

Is Java getClass reflection?

getClass() is reflection per se since it can be used to examine the given object.

Is assignable from VS Instanceof?

In other words, instanceof operator checks if the left object is same or subclass of right class, while isAssignableFrom checks if we can assign object of the parameter class (from) to the reference of the class on which the method is called.


2 Answers

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

JavaDoc

like image 113
Miro Hudak Avatar answered Oct 15 '22 00:10

Miro Hudak


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.

like image 29
Amit Deshpande Avatar answered Oct 14 '22 23:10

Amit Deshpande