Example usage for java.lang.reflect WildcardType equals

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

Introduction

In this page you can find the example usage for java.lang.reflect WildcardType 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   w w  w .jav  a 2 s.c om*/
 * Checks if the subject type may be implicitly cast to the target wildcard
 * type following the Java generics rules.
 * </p>
 * 
 * @param type
 *            the subject type to be assigned to the target type
 * @param toWildcardType
 *            the target wildcard type
 * @return true if <code>type</code> is assignable to
 *         <code>toWildcardType</code>.
 */
private static boolean isAssignable(Type type, WildcardType toWildcardType,
        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 (toWildcardType == null) {
        return false;
    }

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

    Type[] toUpperBounds = getImplicitUpperBounds(toWildcardType);
    Type[] toLowerBounds = getImplicitLowerBounds(toWildcardType);

    if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        Type[] upperBounds = getImplicitUpperBounds(wildcardType);
        Type[] lowerBounds = getImplicitLowerBounds(wildcardType);

        for (Type toBound : toUpperBounds) {
            // if there are assignments for unresolved type variables,
            // now's the time to substitute them.
            toBound = substituteTypeVariables(toBound, typeVarAssigns);

            // each upper bound of the subject type has to be assignable to
            // each
            // upper bound of the target type
            for (Type bound : upperBounds) {
                if (!isAssignable(bound, toBound, typeVarAssigns)) {
                    return false;
                }
            }
        }

        for (Type toBound : toLowerBounds) {
            // if there are assignments for unresolved type variables,
            // now's the time to substitute them.
            toBound = substituteTypeVariables(toBound, typeVarAssigns);

            // each lower bound of the target type has to be assignable to
            // each
            // lower bound of the subject type
            for (Type bound : lowerBounds) {
                if (!isAssignable(toBound, bound, typeVarAssigns)) {
                    return false;
                }
            }
        }

        return true;
    }

    for (Type toBound : toUpperBounds) {
        // if there are assignments for unresolved type variables,
        // now's the time to substitute them.
        if (!isAssignable(type, substituteTypeVariables(toBound, typeVarAssigns), typeVarAssigns)) {
            return false;
        }
    }

    for (Type toBound : toLowerBounds) {
        // if there are assignments for unresolved type variables,
        // now's the time to substitute them.
        if (!isAssignable(substituteTypeVariables(toBound, typeVarAssigns), type, typeVarAssigns)) {
            return false;
        }
    }

    return true;
}