Java Reflection Assignable isAssignable(Class clazz, Class toClass)

Here you can find the source of isAssignable(Class clazz, Class toClass)

Description

is Assignable

License

LGPL

Declaration

public static boolean isAssignable(Class clazz, Class toClass) 

Method Source Code

//package com.java2s;
/*/*from   w ww.ja  v a2s .c  om*/
 * JFox - The most lightweight Java EE Application Server!
 * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
 *
 * JFox is licenced and re-distributable under GNU LGPL.
 */

import java.util.HashMap;

import java.util.Map;

public class Main {
    private static Map<String, Class> primitiveStringToClass = new HashMap<String, Class>();

    public static boolean isAssignable(Class clazz, Class toClass) {
        if (clazz == null || toClass == null) {
            return false;
        }
        if (clazz.equals(toClass)) {
            return true;
        }

        Class newClazz = clazz;
        Class newToClass = toClass;
        if (clazz.isPrimitive()) {
            newClazz = primitiveStringToClass.get(clazz.getName());
        }
        if (toClass.isPrimitive()) {
            newToClass = primitiveStringToClass.get(toClass.getName());
        }
        return newToClass.isAssignableFrom(newClazz);
    }

    public static boolean isAssignable(Class[] classArray, Class[] toClassArray) {
        if (classArray == null || toClassArray == null) {
            return false;
        }
        if (classArray.length != toClassArray.length) {
            return false;
        }

        for (int i = 0; i < classArray.length; i++) {
            if (!isAssignable(classArray[i], toClassArray[i])) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. isAssignable(Class lhsType, Class rhsType)
  2. isAssignable(Class cls, final Class toClass)
  3. isAssignable(Class lhsType, Class rhsType)
  4. isAssignable(Class lhsType, Class rhsType)