use of java.lang.reflect.WildcardType in project mockito by mockito.
the class GenericMetadataSupport method registerTypeVariablesOn.
protected void registerTypeVariablesOn(Type classType) {
if (!(classType instanceof ParameterizedType)) {
return;
}
ParameterizedType parameterizedType = (ParameterizedType) classType;
TypeVariable<?>[] typeParameters = ((Class<?>) parameterizedType.getRawType()).getTypeParameters();
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
for (int i = 0; i < actualTypeArguments.length; i++) {
TypeVariable<?> typeParameter = typeParameters[i];
Type actualTypeArgument = actualTypeArguments[i];
if (actualTypeArgument instanceof TypeVariable) {
/*
* If actualTypeArgument is a TypeVariable, and it is not present in
* the context map then it is needed to try harder to gather more data
* from the type argument itself. In some case the type argument do
* define upper bounds, this allow to look for them if not in the
* context map.
*/
registerTypeVariableIfNotPresent((TypeVariable<?>) actualTypeArgument);
// class.
if (contextualActualTypeParameters.containsKey(typeParameter)) {
continue;
}
}
if (actualTypeArgument instanceof WildcardType) {
contextualActualTypeParameters.put(typeParameter, boundsOf((WildcardType) actualTypeArgument));
} else if (typeParameter != actualTypeArgument) {
contextualActualTypeParameters.put(typeParameter, actualTypeArgument);
}
// logger.log("For '" + parameterizedType + "' found type variable : { '" +
// typeParameter + "(" + System.identityHashCode(typeParameter) + ")" + "' : '" +
// actualTypeArgument + "(" + System.identityHashCode(typeParameter) + ")" + "' }");
}
}
use of java.lang.reflect.WildcardType in project logging-log4j2 by apache.
the class TypeUtil method isWildcardAssignable.
private static boolean isWildcardAssignable(final WildcardType lhs, final Type rhs) {
final Type[] lhsUpperBounds = getEffectiveUpperBounds(lhs);
final Type[] lhsLowerBounds = getEffectiveLowerBounds(lhs);
if (rhs instanceof WildcardType) {
// oh boy, this scenario requires checking a lot of assignability!
final WildcardType rhsType = (WildcardType) rhs;
final Type[] rhsUpperBounds = getEffectiveUpperBounds(rhsType);
final Type[] rhsLowerBounds = getEffectiveLowerBounds(rhsType);
for (final Type lhsUpperBound : lhsUpperBounds) {
for (final Type rhsUpperBound : rhsUpperBounds) {
if (!isBoundAssignable(lhsUpperBound, rhsUpperBound)) {
return false;
}
}
for (final Type rhsLowerBound : rhsLowerBounds) {
if (!isBoundAssignable(lhsUpperBound, rhsLowerBound)) {
return false;
}
}
}
for (final Type lhsLowerBound : lhsLowerBounds) {
for (final Type rhsUpperBound : rhsUpperBounds) {
if (!isBoundAssignable(rhsUpperBound, lhsLowerBound)) {
return false;
}
}
for (final Type rhsLowerBound : rhsLowerBounds) {
if (!isBoundAssignable(rhsLowerBound, lhsLowerBound)) {
return false;
}
}
}
} else {
// phew, far less bounds to check
for (final Type lhsUpperBound : lhsUpperBounds) {
if (!isBoundAssignable(lhsUpperBound, rhs)) {
return false;
}
}
for (final Type lhsLowerBound : lhsLowerBounds) {
if (!isBoundAssignable(lhsLowerBound, rhs)) {
return false;
}
}
}
return true;
}
use of java.lang.reflect.WildcardType in project logging-log4j2 by apache.
the class TypeUtil method isAssignable.
/**
* Indicates if two {@link Type}s are assignment compatible.
*
* @param lhs the left hand side to check assignability to
* @param rhs the right hand side to check assignability from
* @return {@code true} if it is legal to assign a variable of type {@code rhs} to a variable of type {@code lhs}
* @see Class#isAssignableFrom(Class)
*/
public static boolean isAssignable(final Type lhs, final Type rhs) {
Objects.requireNonNull(lhs, "No left hand side type provided");
Objects.requireNonNull(rhs, "No right hand side type provided");
if (lhs.equals(rhs)) {
return true;
}
if (Object.class.equals(lhs)) {
// everything is assignable to Object
return true;
}
// raw type on left
if (lhs instanceof Class<?>) {
final Class<?> lhsClass = (Class<?>) lhs;
if (rhs instanceof Class<?>) {
// no generics involved
final Class<?> rhsClass = (Class<?>) rhs;
return lhsClass.isAssignableFrom(rhsClass);
}
if (rhs instanceof ParameterizedType) {
// check to see if the parameterized type has the same raw type as the lhs; this is legal
final Type rhsRawType = ((ParameterizedType) rhs).getRawType();
if (rhsRawType instanceof Class<?>) {
return lhsClass.isAssignableFrom((Class<?>) rhsRawType);
}
}
if (lhsClass.isArray() && rhs instanceof GenericArrayType) {
// check for compatible array component types
return isAssignable(lhsClass.getComponentType(), ((GenericArrayType) rhs).getGenericComponentType());
}
}
// parameterized type on left
if (lhs instanceof ParameterizedType) {
final ParameterizedType lhsType = (ParameterizedType) lhs;
if (rhs instanceof Class<?>) {
final Type lhsRawType = lhsType.getRawType();
if (lhsRawType instanceof Class<?>) {
return ((Class<?>) lhsRawType).isAssignableFrom((Class<?>) rhs);
}
} else if (rhs instanceof ParameterizedType) {
final ParameterizedType rhsType = (ParameterizedType) rhs;
return isParameterizedAssignable(lhsType, rhsType);
}
}
// generic array type on left
if (lhs instanceof GenericArrayType) {
final Type lhsComponentType = ((GenericArrayType) lhs).getGenericComponentType();
if (rhs instanceof Class<?>) {
// raw type on right
final Class<?> rhsClass = (Class<?>) rhs;
if (rhsClass.isArray()) {
return isAssignable(lhsComponentType, rhsClass.getComponentType());
}
} else if (rhs instanceof GenericArrayType) {
return isAssignable(lhsComponentType, ((GenericArrayType) rhs).getGenericComponentType());
}
}
// wildcard type on left
if (lhs instanceof WildcardType) {
return isWildcardAssignable((WildcardType) lhs, rhs);
}
// strange...
return false;
}
use of java.lang.reflect.WildcardType in project logging-log4j2 by apache.
the class CommandLine method getTypeAttribute.
private static Class<?>[] getTypeAttribute(final Field field) {
final Class<?>[] explicit = field.isAnnotationPresent(Parameters.class) ? field.getAnnotation(Parameters.class).type() : field.getAnnotation(Option.class).type();
if (explicit.length > 0) {
return explicit;
}
if (field.getType().isArray()) {
return new Class<?>[] { field.getType().getComponentType() };
}
if (isMultiValue(field)) {
// e.g. Map<Long, ? extends Number>
final Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
final ParameterizedType parameterizedType = (ParameterizedType) type;
// e.g. ? extends Number
final Type[] paramTypes = parameterizedType.getActualTypeArguments();
final Class<?>[] result = new Class<?>[paramTypes.length];
for (int i = 0; i < paramTypes.length; i++) {
// e.g. Long
if (paramTypes[i] instanceof Class) {
result[i] = (Class<?>) paramTypes[i];
continue;
}
if (paramTypes[i] instanceof WildcardType) {
// e.g. ? extends Number
final WildcardType wildcardType = (WildcardType) paramTypes[i];
// e.g. []
final Type[] lower = wildcardType.getLowerBounds();
if (lower.length > 0 && lower[0] instanceof Class) {
result[i] = (Class<?>) lower[0];
continue;
}
// e.g. Number
final Type[] upper = wildcardType.getUpperBounds();
if (upper.length > 0 && upper[0] instanceof Class) {
result[i] = (Class<?>) upper[0];
continue;
}
}
// too convoluted generic type, giving up
Arrays.fill(result, String.class);
// too convoluted generic type, giving up
return result;
}
// we inferred all types from ParameterizedType
return result;
}
// field is multi-value but not ParameterizedType
return new Class<?>[] { String.class, String.class };
}
// not a multi-value field
return new Class<?>[] { field.getType() };
}
use of java.lang.reflect.WildcardType in project logging-log4j2 by apache.
the class TypeUtil method typeParametersMatch.
private static boolean typeParametersMatch(final Type required, final Type found) {
if (required instanceof Class<?> || required instanceof ParameterizedType || required instanceof GenericArrayType) {
if (found instanceof Class<?> || found instanceof ParameterizedType || found instanceof GenericArrayType) {
return typesMatch(getReferenceType(required), getReferenceType(found));
}
if (found instanceof TypeVariable<?>) {
return typeParametersMatch(required, (TypeVariable<?>) found);
}
}
if (required instanceof WildcardType) {
final WildcardType wildcardType = (WildcardType) required;
if (found instanceof Class<?> || found instanceof ParameterizedType || found instanceof GenericArrayType) {
return typeParametersMatch(wildcardType, found);
}
if (found instanceof TypeVariable<?>) {
return typeParametersMatch(wildcardType, (TypeVariable<?>) found);
}
}
if (required instanceof TypeVariable<?>) {
if (found instanceof TypeVariable<?>) {
final Type[] foundBounds = getTopBounds(((TypeVariable<?>) found).getBounds());
final Type[] requiredBounds = getTopBounds(((TypeVariable<?>) required).getBounds());
return areBoundsStricter(foundBounds, requiredBounds);
}
}
return false;
}
Aggregations