Java Code Examples for java.lang.reflect.ParameterizedType#equals()

The following examples show how to use java.lang.reflect.ParameterizedType#equals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private static boolean isAssignable(ParameterizedType lhsType, ParameterizedType rhsType) {
	if (lhsType.equals(rhsType)) {
		return true;
	}

	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;
}
 
Example 2
Source File: TypeUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
private static boolean isAssignable(ParameterizedType lhsType, ParameterizedType rhsType) {
	if (lhsType.equals(rhsType)) {
		return true;
	}

	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;
}
 
Example 3
Source File: TypeUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static boolean isAssignable(ParameterizedType lhsType, ParameterizedType rhsType) {
	if (lhsType.equals(rhsType)) {
		return true;
	}

	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;
}
 
Example 4
Source File: TypeUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private static boolean isAssignable(ParameterizedType lhsType, ParameterizedType rhsType) {
	if (lhsType.equals(rhsType)) {
		return true;
	}

	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;
}
 
Example 5
Source File: TypeUtils.java    From seny-devpkg with Apache License 2.0 6 votes vote down vote up
private static boolean isAssignable(ParameterizedType lhsType, ParameterizedType rhsType) {
	if (lhsType.equals(rhsType)) {
		return true;
	}

	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;
}
 
Example 6
Source File: TypeUtils.java    From dolphin with Apache License 2.0 6 votes vote down vote up
private static boolean isAssignable(ParameterizedType lhsType, ParameterizedType rhsType) {
	if (lhsType.equals(rhsType)) {
		return true;
	}

	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;
}
 
Example 7
Source File: TypeUtil.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private static boolean isParameterizedAssignable(final ParameterizedType lhs, final ParameterizedType rhs) {
    if (lhs.equals(rhs)) {
        // that was easy
        return true;
    }
    final Type[] lhsTypeArguments = lhs.getActualTypeArguments();
    final Type[] rhsTypeArguments = rhs.getActualTypeArguments();
    final int size = lhsTypeArguments.length;
    if (rhsTypeArguments.length != size) {
        // clearly incompatible types
        return false;
    }
    for (int i = 0; i < size; i++) {
        // verify all type arguments are assignable
        final Type lhsArgument = lhsTypeArguments[i];
        final Type rhsArgument = rhsTypeArguments[i];
        if (!lhsArgument.equals(rhsArgument) &&
            !(lhsArgument instanceof WildcardType &&
                isWildcardAssignable((WildcardType) lhsArgument, rhsArgument))) {
            return false;
        }
    }
    return true;
}
 
Example 8
Source File: Lang_15_TypeUtils_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * <p> 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
 * @param typeVarAssigns a map with type variables
 * @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;
}
 
Example 9
Source File: Lang_15_TypeUtils_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * <p> 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
 * @param typeVarAssigns a map with type variables
 * @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 (TypeVariable<?> var : toTypeVarAssigns.keySet()) {
        Type toTypeArg = unrollVariableAssignments(var, toTypeVarAssigns);
        Type fromTypeArg = unrollVariableAssignments(var, fromTypeVarAssigns);

        // 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;
}
 
Example 10
Source File: TypeUtils.java    From dolphin-platform with Apache License 2.0 4 votes vote down vote up
/**
 * <p>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
 * @param typeVarAssigns      a map with type variables
 * @return {@code true} if {@code type} is assignable to {@code toType}.
 */
private static boolean isAssignable(final Type type, final ParameterizedType toParameterizedType,
                                    final 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
    final 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.
    final 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
    final Map<TypeVariable<?>, Type> toTypeVarAssigns = getTypeArguments(toParameterizedType,
            toClass, typeVarAssigns);

    // now to check each type argument
    for (final TypeVariable<?> var : toTypeVarAssigns.keySet()) {
        final Type toTypeArg = unrollVariableAssignments(var, toTypeVarAssigns);
        final Type fromTypeArg = unrollVariableAssignments(var, fromTypeVarAssigns);

        if (toTypeArg == null && fromTypeArg instanceof Class) {
            continue;
        }

        // 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;
}
 
Example 11
Source File: TypeUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p> 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;
}
 
Example 12
Source File: TypeUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p> 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
 * @param typeVarAssigns a map with type variables
 * @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 (TypeVariable<?> var : toTypeVarAssigns.keySet()) {
        Type toTypeArg = unrollVariableAssignments(var, toTypeVarAssigns);
        Type fromTypeArg = unrollVariableAssignments(var, fromTypeVarAssigns);

        // 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;
}
 
Example 13
Source File: TypeUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p> 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
 * @param typeVarAssigns a map with type variables
 * @return true if <code>type</code> is assignable to <code>toType</code>.
 */
private static boolean isAssignable(final Type type, final ParameterizedType toParameterizedType,
        final 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
    final 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.
    final 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
    final Map<TypeVariable<?>, Type> toTypeVarAssigns = getTypeArguments(toParameterizedType,
            toClass, typeVarAssigns);

    // now to check each type argument
    for (final TypeVariable<?> var : toTypeVarAssigns.keySet()) {
        final Type toTypeArg = unrollVariableAssignments(var, toTypeVarAssigns);
        final Type fromTypeArg = unrollVariableAssignments(var, fromTypeVarAssigns);

        // 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;
}
 
Example 14
Source File: TypeUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p> 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
 * @param typeVarAssigns a map with type variables
 * @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 (TypeVariable<?> var : toTypeVarAssigns.keySet()) {
        Type toTypeArg = unrollVariableAssignments(var, toTypeVarAssigns);
        Type fromTypeArg = unrollVariableAssignments(var, fromTypeVarAssigns);

        // 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;
}