Java Reflection Assignable isAssignableFrom(Class to, Class from)

Here you can find the source of isAssignableFrom(Class to, Class from)

Description

Just like Class.isAssignableFrom(), but does the right thing when considering autoboxing.

License

Open Source License

Declaration

public static boolean isAssignableFrom(Class<?> to, Class<?> from) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.HashMap;
import java.util.Map;

public class Main {
    /** A map of the primitive types to their wrapper types */
    private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER = new HashMap<>();

    /**//from  www .ja v  a  2  s  .  co m
     * Just like Class.isAssignableFrom(), but does the right thing when considering autoboxing.
     */
    public static boolean isAssignableFrom(Class<?> to, Class<?> from) {
        Class<?> notPrimitiveTo = to.isPrimitive() ? PRIMITIVE_TO_WRAPPER.get(to) : to;
        Class<?> notPrimitiveFrom = from.isPrimitive() ? PRIMITIVE_TO_WRAPPER.get(from) : from;

        return notPrimitiveTo.isAssignableFrom(notPrimitiveFrom);
    }
}

Related

  1. isAssignable(Class lhsType, Class rhsType)
  2. isAssignable(Class lhsType, Class rhsType)
  3. isAssignable(final Class from, final Class to)
  4. isAssignableFrom(Class class1, Class class2)
  5. isAssignableFrom(Class to, Class from)
  6. isAssignableFrom(Class toClass, Class fromClass)
  7. isAssignableValue(Class type, Object value)
  8. isAssignableValue(Class type, Object value)
  9. isClassAssignableFrom(Class formal, Class actual)