Java Reflection Assignable isAssignable(final Class from, final Class to)

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

Description

Determines if the type from is assignable to the type to.

License

Open Source License

Parameter

Parameter Description
from the type of the source
to the type of the destination

Return

true if assignable, false if otherwise

Declaration

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

Method Source Code

//package com.java2s;

import java.util.Map;

public class Main {
    private static Map<Class<?>, Class<?>> assignable;

    /**//from w  w  w .ja  v  a  2 s  .  com
     * Determines if the type from is assignable to the type to.
     *
     * @param from the type of the source
     * @param to the type of the destination
     * @return true if assignable, false if otherwise
     */
    public static boolean isAssignable(final Class<?> from, final Class<?> to) {
        assert from != null;
        assert to != null;
        if (to.isAssignableFrom(from)) {
            return true;
        } else {
            final Class<?> clazz = assignable.get(from);
            return clazz.equals(to);
        }
    }

    /**
     * Determines if the types in c1 are assignable to the types in c2.
     * 
     * @param c1 the array of types
     * @param c2 the array of types
     * @return true if the types in c1 are assignable to the types in c2
     */
    private static boolean isAssignable(final Class<?>[] c1, final Class<?>[] c2) {
        if (c1.length == c2.length) {
            for (int i = c1.length - 1; i >= 0; i--) {
                if (!c2[i].isAssignableFrom(c1[i])) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}

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)
  5. isAssignable(Class lhsType, Class rhsType)
  6. isAssignableFrom(Class class1, Class class2)
  7. isAssignableFrom(Class to, Class from)
  8. isAssignableFrom(Class to, Class from)
  9. isAssignableFrom(Class toClass, Class fromClass)