Search in sources :

Example 11 with WildcardType

use of java.lang.reflect.WildcardType in project fastjson by alibaba.

the class TypeUtils method getCollectionItemType.

public static Type getCollectionItemType(Type fieldType) {
    Type itemType = null;
    Class<?> clazz = null;
    if (fieldType instanceof ParameterizedType) {
        Type actualTypeArgument = ((ParameterizedType) fieldType).getActualTypeArguments()[0];
        if (actualTypeArgument instanceof WildcardType) {
            WildcardType wildcardType = (WildcardType) actualTypeArgument;
            Type[] upperBounds = wildcardType.getUpperBounds();
            if (upperBounds.length == 1) {
                actualTypeArgument = upperBounds[0];
            }
        }
        itemType = actualTypeArgument;
    } else if (//
    fieldType instanceof Class<?> && !(clazz = (Class<?>) fieldType).getName().startsWith("java.")) {
        Type superClass = clazz.getGenericSuperclass();
        itemType = TypeUtils.getCollectionItemType(superClass);
    }
    if (itemType == null) {
        itemType = Object.class;
    }
    return itemType;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JSONType(com.alibaba.fastjson.annotation.JSONType) WildcardType(java.lang.reflect.WildcardType)

Example 12 with WildcardType

use of java.lang.reflect.WildcardType in project aries by apache.

the class ReferenceDependency method getBindType.

private BindType getBindType(Type type) {
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = cast(type);
        Type rawType = parameterizedType.getRawType();
        if (Instance.class.isAssignableFrom(cast(rawType))) {
            _instance = true;
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            return getBindType(actualTypeArguments[0]);
        } else if (Map.class.isAssignableFrom(cast(rawType))) {
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            Type first = actualTypeArguments[0];
            Type second = actualTypeArguments[1];
            if (!(first instanceof ParameterizedType) && String.class.isAssignableFrom(cast(first))) {
                if ((!(second instanceof ParameterizedType) && (second == Object.class)) || (second instanceof WildcardType)) {
                    return BindType.SERVICE_PROPERTIES;
                }
            }
            return BindType.SERVICE;
        } else if (ServiceReference.class.isAssignableFrom(cast(rawType))) {
            return BindType.SERVICE_REFERENCE;
        }
        return BindType.SERVICE;
    } else if (ServiceReference.class.isAssignableFrom(cast(type))) {
        return BindType.SERVICE_REFERENCE;
    }
    return BindType.SERVICE;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) Map(java.util.Map) ServiceReference(org.osgi.framework.ServiceReference)

Example 13 with WildcardType

use of java.lang.reflect.WildcardType in project spock by spockframework.

the class GenericTypeReflector method getExactDirectSuperTypes.

/**
	 * Returns the direct supertypes of the given type. Resolves type parameters.
	 */
private static Type[] getExactDirectSuperTypes(Type type) {
    if (type instanceof ParameterizedType || type instanceof Class) {
        Class<?> clazz;
        if (type instanceof ParameterizedType) {
            clazz = (Class<?>) ((ParameterizedType) type).getRawType();
        } else {
            clazz = (Class<?>) type;
            if (clazz.isArray())
                return getArrayExactDirectSuperTypes(clazz);
        }
        Type[] superInterfaces = clazz.getGenericInterfaces();
        Type superClass = clazz.getGenericSuperclass();
        Type[] result;
        int resultIndex;
        if (superClass == null) {
            result = new Type[superInterfaces.length];
            resultIndex = 0;
        } else {
            result = new Type[superInterfaces.length + 1];
            resultIndex = 1;
            result[0] = mapTypeParameters(superClass, type);
        }
        for (Type superInterface : superInterfaces) {
            result[resultIndex++] = mapTypeParameters(superInterface, type);
        }
        return result;
    } else if (type instanceof TypeVariable) {
        TypeVariable<?> tv = (TypeVariable<?>) type;
        return tv.getBounds();
    } else if (type instanceof WildcardType) {
        // But it does happen if the upper bound of a type variable contains a wildcard
        return ((WildcardType) type).getUpperBounds();
    } else if (type instanceof CaptureType) {
        return ((CaptureType) type).getUpperBounds();
    } else if (type instanceof GenericArrayType) {
        return getArrayExactDirectSuperTypes(type);
    } else {
        throw new RuntimeException("not implemented type: " + type);
    }
}
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 14 with WildcardType

use of java.lang.reflect.WildcardType in project spock by spockframework.

the class GenericTypeReflector method capture.

/**
	 * Applies capture conversion to the given type.
	 */
public static Type capture(Type type) {
    VarMap varMap = new VarMap();
    List<CaptureTypeImpl> toInit = new ArrayList<CaptureTypeImpl>();
    if (type instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) type;
        Class<?> clazz = (Class<?>) pType.getRawType();
        Type[] arguments = pType.getActualTypeArguments();
        TypeVariable<?>[] vars = clazz.getTypeParameters();
        Type[] capturedArguments = new Type[arguments.length];
        assert arguments.length == vars.length;
        for (int i = 0; i < arguments.length; i++) {
            Type argument = arguments[i];
            if (argument instanceof WildcardType) {
                CaptureTypeImpl captured = new CaptureTypeImpl((WildcardType) argument, vars[i]);
                argument = captured;
                toInit.add(captured);
            }
            capturedArguments[i] = argument;
            varMap.add(vars[i], argument);
        }
        for (CaptureTypeImpl captured : toInit) {
            captured.init(varMap);
        }
        Type ownerType = (pType.getOwnerType() == null) ? null : capture(pType.getOwnerType());
        return new ParameterizedTypeImpl(clazz, capturedArguments, ownerType);
    } else {
        return type;
    }
}
Also used : ArrayList(java.util.ArrayList) 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)

Example 15 with WildcardType

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

the class OldGenericReflectionCornerCases method testMultipleBoundedWildcardUnEquality.

@SuppressWarnings("unchecked")
public void testMultipleBoundedWildcardUnEquality() throws Exception {
    Class<? extends MultipleBoundedWildcardUnEquality> clazz = MultipleBoundedWildcardUnEquality.class;
    // new WildcardEquality<Object>().wildcardEquality(new Pair<String,
    // Integer>());
    Method method = clazz.getDeclaredMethod("multipleBoundedWildcardUnEquality", 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(MultipleBoundedWildcardUnEquality.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;
    assertNotEquals(firstWildcardArgument, secondWildcardArgument);
    Type[] firstWildcardArgumentUpperBounds = firstWildcardArgument.getUpperBounds();
    assertLenghtOne(firstWildcardArgumentUpperBounds);
    Type firstWildcardArgumentUpperBoundsType = firstWildcardArgumentUpperBounds[0];
    Type[] secondWildcardArgumentLowerBounds = secondWildcardArgument.getLowerBounds();
    assertLenghtOne(secondWildcardArgumentLowerBounds);
    Type secondWildcardArgumentLoweroundsType = secondWildcardArgumentLowerBounds[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)

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