Example usage for java.lang.reflect ParameterizedType equals

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

Introduction

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

Prototype

public boolean equals(Object obj) 

Source Link

Document

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

Usage

From source file:TypeUtils.java

private static boolean isAssignable(ParameterizedType lhsType, ParameterizedType rhsType) {
    if (lhsType.equals(rhsType)) {
        return true;
    }//from www.ja  v a 2 s. c om
    Type[] lhsTypeArguments = lhsType.getActualTypeArguments();
    Type[] rhsTypeArguments = rhsType.getActualTypeArguments();
    if (lhsTypeArguments.length != rhsTypeArguments.length) {
        return false;
    }
    for (int size = lhsTypeArguments.length, i = 0; i < size; ++i) {
        Type lhsArg = lhsTypeArguments[i];
        Type rhsArg = rhsTypeArguments[i];
        if (!lhsArg.equals(rhsArg)
                && !(lhsArg instanceof WildcardType && isAssignable((WildcardType) lhsArg, rhsArg))) {
            return false;
        }
    }
    return true;
}

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

/**
 * <p>/*  w w w . j a va  2s .  c  o m*/
 * Checks if the subject type may be implicitly cast to the target
 * parameterized type following the Java generics rules.
 * </p>
 * 
 * @param type
 *            the subject type to be assigned to the target type
 * @param toParameterizedType
 *            the target parameterized type
 * @return true if <code>type</code> is assignable to <code>toType</code>.
 */
private static boolean isAssignable(Type type, ParameterizedType toParameterizedType,
        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 (toParameterizedType == null) {
        return false;
    }

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

    // get the target type's raw type
    Class<?> toClass = getRawType(toParameterizedType);
    // get the subject type's type arguments including owner type arguments
    // and supertype arguments up to and including the target class.
    Map<TypeVariable<?>, Type> fromTypeVarAssigns = getTypeArguments(type, toClass, null);

    // null means the two types are not compatible
    if (fromTypeVarAssigns == null) {
        return false;
    }

    // compatible types, but there's no type arguments. this is equivalent
    // to comparing Map< ?, ? > to Map, and raw types are always assignable
    // to parameterized types.
    if (fromTypeVarAssigns.isEmpty()) {
        return true;
    }

    // get the target type's type arguments including owner type arguments
    Map<TypeVariable<?>, Type> toTypeVarAssigns = getTypeArguments(toParameterizedType, toClass,
            typeVarAssigns);

    // now to check each type argument
    for (Map.Entry<TypeVariable<?>, Type> entry : toTypeVarAssigns.entrySet()) {
        Type toTypeArg = entry.getValue();
        Type fromTypeArg = fromTypeVarAssigns.get(entry.getKey());

        // parameters must either be absent from the subject type, within
        // the bounds of the wildcard type, or be an exact match to the
        // parameters of the target type.
        if (fromTypeArg != null && !toTypeArg.equals(fromTypeArg) && !(toTypeArg instanceof WildcardType
                && isAssignable(fromTypeArg, toTypeArg, typeVarAssigns))) {
            return false;
        }
    }

    return true;
}