Search in sources :

Example 16 with WildcardType

use of java.lang.reflect.WildcardType in project robovm by robovm.

the class OldGenericReflectionCornerCases method testMultipleBoundedWildcard.

@SuppressWarnings("unchecked")
public void testMultipleBoundedWildcard() throws Exception {
    Class<? extends MultipleBoundedWildcardEquality> clazz = MultipleBoundedWildcardEquality.class;
    Method method = clazz.getDeclaredMethod("multipleBoundedWildcardEquality", Pair.class);
    TypeVariable<?>[] typeParameters = clazz.getTypeParameters();
    assertLenghtOne(typeParameters);
    TypeVariable<?> typeParameter = typeParameters[0];
    Type[] typeParameterBounds = typeParameter.getBounds();
    assertEquals(2, typeParameterBounds.length);
    assertEquals(Object.class, typeParameterBounds[0]);
    assertInstanceOf(ParameterizedType.class, typeParameterBounds[1]);
    ParameterizedType parameterizedType = (ParameterizedType) typeParameterBounds[1];
    assertEquals(Comparable.class, parameterizedType.getRawType());
    Type[] typeArguments = parameterizedType.getActualTypeArguments();
    assertLenghtOne(typeArguments);
    assertInstanceOf(ParameterizedType.class, typeArguments[0]);
    ParameterizedType type = (ParameterizedType) typeArguments[0];
    assertEquals(typeParameter, type.getActualTypeArguments()[0]);
    assertEquals(MultipleBoundedWildcardEquality.class, type.getRawType());
    Type[] parameterTypes = method.getGenericParameterTypes();
    assertLenghtOne(parameterTypes);
    Type parameter = parameterTypes[0];
    assertInstanceOf(ParameterizedType.class, parameter);
    ParameterizedType paramType = (ParameterizedType) parameter;
    Type[] actualTypeArguments = paramType.getActualTypeArguments();
    assertEquals(2, actualTypeArguments.length);
    Type firstArgument = actualTypeArguments[0];
    assertInstanceOf(WildcardType.class, firstArgument);
    WildcardType firstWildcardArgument = (WildcardType) firstArgument;
    Type secondArgument = actualTypeArguments[1];
    assertInstanceOf(WildcardType.class, secondArgument);
    WildcardType secondWildcardArgument = (WildcardType) secondArgument;
    assertEquals(firstWildcardArgument, secondWildcardArgument);
    Type[] firstWildcardArgumentUpperBounds = firstWildcardArgument.getUpperBounds();
    assertLenghtOne(firstWildcardArgumentUpperBounds);
    Type firstWildcardArgumentUpperBoundsType = firstWildcardArgumentUpperBounds[0];
    Type[] secondWildcardArgumentUpperBounds = secondWildcardArgument.getUpperBounds();
    assertLenghtOne(secondWildcardArgumentUpperBounds);
    Type secondWildcardArgumentLoweroundsType = secondWildcardArgumentUpperBounds[0];
    assertEquals(firstWildcardArgumentUpperBoundsType, secondWildcardArgumentLoweroundsType);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) Method(java.lang.reflect.Method)

Example 17 with WildcardType

use of java.lang.reflect.WildcardType in project spring-framework by spring-projects.

the class TypeUtils method isAssignable.

private static boolean isAssignable(WildcardType lhsType, Type rhsType) {
    Type[] lUpperBounds = lhsType.getUpperBounds();
    // supply the implicit upper bound if none are specified
    if (lUpperBounds.length == 0) {
        lUpperBounds = new Type[] { Object.class };
    }
    Type[] lLowerBounds = lhsType.getLowerBounds();
    // supply the implicit lower bound if none are specified
    if (lLowerBounds.length == 0) {
        lLowerBounds = new Type[] { null };
    }
    if (rhsType instanceof WildcardType) {
        // both the upper and lower bounds of the right-hand side must be
        // completely enclosed in the upper and lower bounds of the left-
        // hand side.
        WildcardType rhsWcType = (WildcardType) rhsType;
        Type[] rUpperBounds = rhsWcType.getUpperBounds();
        if (rUpperBounds.length == 0) {
            rUpperBounds = new Type[] { Object.class };
        }
        Type[] rLowerBounds = rhsWcType.getLowerBounds();
        if (rLowerBounds.length == 0) {
            rLowerBounds = new Type[] { null };
        }
        for (Type lBound : lUpperBounds) {
            for (Type rBound : rUpperBounds) {
                if (!isAssignableBound(lBound, rBound)) {
                    return false;
                }
            }
            for (Type rBound : rLowerBounds) {
                if (!isAssignableBound(lBound, rBound)) {
                    return false;
                }
            }
        }
        for (Type lBound : lLowerBounds) {
            for (Type rBound : rUpperBounds) {
                if (!isAssignableBound(rBound, lBound)) {
                    return false;
                }
            }
            for (Type rBound : rLowerBounds) {
                if (!isAssignableBound(rBound, lBound)) {
                    return false;
                }
            }
        }
    } else {
        for (Type lBound : lUpperBounds) {
            if (!isAssignableBound(lBound, rhsType)) {
                return false;
            }
        }
        for (Type lBound : lLowerBounds) {
            if (!isAssignableBound(rhsType, lBound)) {
                return false;
            }
        }
    }
    return true;
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) WildcardType(java.lang.reflect.WildcardType)

Example 18 with WildcardType

use of java.lang.reflect.WildcardType in project robovm by robovm.

the class WildcardTypeTest method checkUpperBoundedParameter.

private void checkUpperBoundedParameter(Method method) {
    assertLenghtOne(method.getGenericParameterTypes());
    Type genericParameterType = method.getGenericParameterTypes()[0];
    assertInstanceOf(ParameterizedType.class, genericParameterType);
    ParameterizedType parameterizedType = (ParameterizedType) genericParameterType;
    Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
    assertLenghtOne(actualTypeArguments);
    assertInstanceOf(WildcardType.class, actualTypeArguments[0]);
    WildcardType wildcardType = (WildcardType) actualTypeArguments[0];
    assertLenghtZero(wildcardType.getLowerBounds());
    Type[] upperBounds = wildcardType.getUpperBounds();
    assertLenghtOne(upperBounds);
    Type upperBound = upperBounds[0];
    assertEquals(getTypeParameter(method), upperBound);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) WildcardType(java.lang.reflect.WildcardType)

Example 19 with WildcardType

use of java.lang.reflect.WildcardType in project retrofit by square.

the class Utils method resolve.

static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
    // This implementation is made a little more complicated in an attempt to avoid object-creation.
    while (true) {
        if (toResolve instanceof TypeVariable) {
            TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
            toResolve = resolveTypeVariable(context, contextRawType, typeVariable);
            if (toResolve == typeVariable) {
                return toResolve;
            }
        } else if (toResolve instanceof Class && ((Class<?>) toResolve).isArray()) {
            Class<?> original = (Class<?>) toResolve;
            Type componentType = original.getComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : new GenericArrayTypeImpl(newComponentType);
        } else if (toResolve instanceof GenericArrayType) {
            GenericArrayType original = (GenericArrayType) toResolve;
            Type componentType = original.getGenericComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : new GenericArrayTypeImpl(newComponentType);
        } else if (toResolve instanceof ParameterizedType) {
            ParameterizedType original = (ParameterizedType) toResolve;
            Type ownerType = original.getOwnerType();
            Type newOwnerType = resolve(context, contextRawType, ownerType);
            boolean changed = newOwnerType != ownerType;
            Type[] args = original.getActualTypeArguments();
            for (int t = 0, length = args.length; t < length; t++) {
                Type resolvedTypeArgument = resolve(context, contextRawType, args[t]);
                if (resolvedTypeArgument != args[t]) {
                    if (!changed) {
                        args = args.clone();
                        changed = true;
                    }
                    args[t] = resolvedTypeArgument;
                }
            }
            return changed ? new ParameterizedTypeImpl(newOwnerType, original.getRawType(), args) : original;
        } else if (toResolve instanceof WildcardType) {
            WildcardType original = (WildcardType) toResolve;
            Type[] originalLowerBound = original.getLowerBounds();
            Type[] originalUpperBound = original.getUpperBounds();
            if (originalLowerBound.length == 1) {
                Type lowerBound = resolve(context, contextRawType, originalLowerBound[0]);
                if (lowerBound != originalLowerBound[0]) {
                    return new WildcardTypeImpl(new Type[] { Object.class }, new Type[] { lowerBound });
                }
            } else if (originalUpperBound.length == 1) {
                Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
                if (upperBound != originalUpperBound[0]) {
                    return new WildcardTypeImpl(new Type[] { upperBound }, EMPTY_TYPE_ARRAY);
                }
            }
            return original;
        } else {
            return toResolve;
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 20 with WildcardType

use of java.lang.reflect.WildcardType in project moshi by square.

the class Types method resolve.

static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
    // This implementation is made a little more complicated in an attempt to avoid object-creation.
    while (true) {
        if (toResolve instanceof TypeVariable) {
            TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
            toResolve = resolveTypeVariable(context, contextRawType, typeVariable);
            if (toResolve == typeVariable)
                return toResolve;
        } else if (toResolve instanceof Class && ((Class<?>) toResolve).isArray()) {
            Class<?> original = (Class<?>) toResolve;
            Type componentType = original.getComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : arrayOf(newComponentType);
        } else if (toResolve instanceof GenericArrayType) {
            GenericArrayType original = (GenericArrayType) toResolve;
            Type componentType = original.getGenericComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : arrayOf(newComponentType);
        } else if (toResolve instanceof ParameterizedType) {
            ParameterizedType original = (ParameterizedType) toResolve;
            Type ownerType = original.getOwnerType();
            Type newOwnerType = resolve(context, contextRawType, ownerType);
            boolean changed = newOwnerType != ownerType;
            Type[] args = original.getActualTypeArguments();
            for (int t = 0, length = args.length; t < length; t++) {
                Type resolvedTypeArgument = resolve(context, contextRawType, args[t]);
                if (resolvedTypeArgument != args[t]) {
                    if (!changed) {
                        args = args.clone();
                        changed = true;
                    }
                    args[t] = resolvedTypeArgument;
                }
            }
            return changed ? new ParameterizedTypeImpl(newOwnerType, original.getRawType(), args) : original;
        } else if (toResolve instanceof WildcardType) {
            WildcardType original = (WildcardType) toResolve;
            Type[] originalLowerBound = original.getLowerBounds();
            Type[] originalUpperBound = original.getUpperBounds();
            if (originalLowerBound.length == 1) {
                Type lowerBound = resolve(context, contextRawType, originalLowerBound[0]);
                if (lowerBound != originalLowerBound[0]) {
                    return supertypeOf(lowerBound);
                }
            } else if (originalUpperBound.length == 1) {
                Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
                if (upperBound != originalUpperBound[0]) {
                    return subtypeOf(upperBound);
                }
            }
            return original;
        } else {
            return toResolve;
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Aggregations

WildcardType (java.lang.reflect.WildcardType)236 ParameterizedType (java.lang.reflect.ParameterizedType)208 Type (java.lang.reflect.Type)180 GenericArrayType (java.lang.reflect.GenericArrayType)160 TypeVariable (java.lang.reflect.TypeVariable)134 Test (org.junit.Test)24 GenericClass (org.evosuite.utils.generic.GenericClass)14 Method (java.lang.reflect.Method)10 WildcardTypeImpl (org.evosuite.utils.generic.WildcardTypeImpl)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)8 Test (org.junit.jupiter.api.Test)8 CaptureType (com.googlecode.gentyref.CaptureType)7 JSONException (com.alibaba.fastjson.JSONException)5 JSONType (com.alibaba.fastjson.annotation.JSONType)5 CatalogType (org.spongepowered.api.CatalogType)4 GenericDeclaration (java.lang.reflect.GenericDeclaration)3 HashSet (java.util.HashSet)3 List (java.util.List)3 MediaType (javax.ws.rs.core.MediaType)3