Example usage for java.lang.reflect GenericArrayType equals

List of usage examples for java.lang.reflect GenericArrayType equals

Introduction

In this page you can find the example usage for java.lang.reflect GenericArrayType equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:com.clark.func.Functions.java

/**
 * <p>/*from ww w .ja v a 2 s. c  om*/
 * Checks if the subject type may be implicitly cast to the target generic
 * array type following the Java generics rules.
 * </p>
 * 
 * @param type
 *            the subject type to be assigned to the target type
 * @param toGenericArrayType
 *            the target generic array type
 * @return true if <code>type</code> is assignable to
 *         <code>toGenericArrayType</code>.
 */
private static boolean isAssignable(Type type, GenericArrayType toGenericArrayType,
        Map<TypeVariable<?>, Type> typeVarAssigns) {
    if (type == null) {
        return true;
    }

    // only a null type can be assigned to null type which
    // would have cause the previous to return true
    if (toGenericArrayType == null) {
        return false;
    }

    // all types are assignable to themselves
    if (toGenericArrayType.equals(type)) {
        return true;
    }

    Type toComponentType = toGenericArrayType.getGenericComponentType();

    if (type instanceof Class<?>) {
        Class<?> cls = (Class<?>) type;

        // compare the component types
        return cls.isArray() && isAssignable(cls.getComponentType(), toComponentType, typeVarAssigns);
    }

    if (type instanceof GenericArrayType) {
        // compare the component types
        return isAssignable(((GenericArrayType) type).getGenericComponentType(), toComponentType,
                typeVarAssigns);
    }

    if (type instanceof WildcardType) {
        // so long as one of the upper bounds is assignable, it's good
        for (Type bound : getImplicitUpperBounds((WildcardType) type)) {
            if (isAssignable(bound, toGenericArrayType)) {
                return true;
            }
        }

        return false;
    }

    if (type instanceof TypeVariable<?>) {
        // probably should remove the following logic and just return false.
        // type variables cannot specify arrays as bounds.
        for (Type bound : getImplicitBounds((TypeVariable<?>) type)) {
            if (isAssignable(bound, toGenericArrayType)) {
                return true;
            }
        }

        return false;
    }

    if (type instanceof ParameterizedType) {
        // the raw type of a parameterized type is never an array or
        // generic array, otherwise the declaration would look like this:
        // Collection[]< ? extends String > collection;
        return false;
    }

    throw new IllegalStateException("found an unhandled type: " + type);
}