Search in sources :

Example 56 with WildcardType

use of java.lang.reflect.WildcardType in project scout.rt by eclipse.

the class VerboseUtility method dumpGenericsImpl.

private static String dumpGenericsImpl(Map<Type, String> shortDecl, Map<Type, String> longDecl, Type type) {
    String shortText = shortDecl.get(type);
    if (shortText != null) {
        return shortText;
    }
    // register short declaration first
    if (type == null) {
        shortText = "null";
    } else if (type instanceof Class) {
        Class<?> c = (Class<?>) type;
        shortText = "Class[" + c.getSimpleName() + "]";
    } else if (type instanceof GenericArrayType) {
        GenericArrayType g = (GenericArrayType) type;
        shortText = "GenericArrayType[" + dumpGenericsRec(shortDecl, longDecl, g.getGenericComponentType()) + "]";
    } else if (type instanceof ParameterizedType) {
        ParameterizedType p = (ParameterizedType) type;
        shortText = "ParameterizedType[" + dumpGenericsRec(shortDecl, longDecl, p.getActualTypeArguments()) + "]";
    } else if (type instanceof TypeVariable<?>) {
        TypeVariable v = (TypeVariable) type;
        shortText = "TypeVariable[" + v.getName() + "]";
    } else if (type instanceof WildcardType) {
        WildcardType w = (WildcardType) type;
        shortText = "WildcardType[" + dumpGenericsRec(shortDecl, longDecl, w.getLowerBounds()) + ", " + dumpGenericsRec(shortDecl, longDecl, w.getUpperBounds()) + "]";
    } else {
        shortText = "UNKNOWN[" + type.getClass().getSimpleName() + "]";
    }
    shortDecl.put(type, shortText);
    // add long declaration
    String longText = null;
    if (type == null) {
    // nop
    } else if (type instanceof Class) {
        Class<?> c = (Class<?>) type;
        longText = "Class[name=" + c.getName() + ", typeParameters=" + dumpGenericsRec(shortDecl, longDecl, c.getTypeParameters()) + "]";
    } else if (type instanceof GenericArrayType) {
    // nop
    } else if (type instanceof ParameterizedType) {
    // nop
    } else if (type instanceof TypeVariable<?>) {
    // nop
    } else if (type instanceof WildcardType) {
        WildcardType w = (WildcardType) type;
        longText = "WildcardType[lowerBounds=" + dumpGenericsRec(shortDecl, longDecl, w.getLowerBounds()) + ", upperBounds=" + dumpGenericsRec(shortDecl, longDecl, w.getUpperBounds()) + "]";
    }
    if (longText != null) {
        longDecl.put(type, longText);
    }
    return shortText;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 57 with WildcardType

use of java.lang.reflect.WildcardType in project vertigo by KleeGroup.

the class SwaggerApiBuilder method createSchemaObject.

private Map<String, Object> createSchemaObject(final Type type) {
    if (type == null) {
        // Si le type est null, on a pas réussi à récupérer la class : souvant dans le cas des generics
        return unknownObjectRef;
    }
    // -----
    final Map<String, Object> schema = new LinkedHashMap<>();
    final Class<?> objectClass = WebServiceTypeUtil.castAsClass(type);
    final String[] typeAndFormat = toSwaggerType(objectClass);
    schema.put("type", typeAndFormat[0]);
    if (typeAndFormat[1] != null) {
        schema.put("format", typeAndFormat[1]);
    }
    if (WebServiceTypeUtil.isAssignableFrom(Collection.class, type)) {
        // we known that List has one parameterized type
        final Type itemsType = ((ParameterizedType) type).getActualTypeArguments()[0];
        // Si le itemsType est null, on prend le unknownObject
        // type argument can't be void
        schema.put("items", createSchemaObject(itemsType));
    } else if ("object".equals(typeAndFormat[0])) {
        final String objectName;
        final Class<?> parameterClass;
        if (type instanceof ParameterizedType && (((ParameterizedType) type).getActualTypeArguments().length == 1 || FacetedQueryResult.class.isAssignableFrom(objectClass)) && !(((ParameterizedType) type).getActualTypeArguments()[0] instanceof WildcardType)) {
            // We have checked there is one parameter or we known that FacetedQueryResult has two parameterized type
            final Type itemsType = ((ParameterizedType) type).getActualTypeArguments()[0];
            parameterClass = WebServiceTypeUtil.castAsClass(itemsType);
            objectName = objectClass.getSimpleName() + "&lt;" + parameterClass.getSimpleName() + "&gt;";
        } else {
            objectName = objectClass.getSimpleName();
            parameterClass = null;
        }
        schema.put("$ref", "#/definitions/" + objectName);
        schema.remove("type");
        if (!builderDefinitions.containsKey(objectName)) {
            final Map<String, Object> definition = new LinkedHashMap<>();
            // we put definitions first to avoid infinite resolution loop
            builderDefinitions.put(objectName, definition);
            if (DtObject.class.isAssignableFrom(objectClass)) {
                final Class<? extends DtObject> dtClass = (Class<? extends DtObject>) objectClass;
                appendPropertiesDtObject(definition, dtClass);
            } else {
                appendPropertiesObject(definition, objectClass, parameterClass);
            }
        }
    }
    return schema;
}
Also used : DtObject(io.vertigo.dynamo.domain.model.DtObject) FacetedQueryResult(io.vertigo.dynamo.collections.model.FacetedQueryResult) LinkedHashMap(java.util.LinkedHashMap) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) WebServiceParamType(io.vertigo.vega.webservice.metamodel.WebServiceParam.WebServiceParamType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) DtObject(io.vertigo.dynamo.domain.model.DtObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 58 with WildcardType

use of java.lang.reflect.WildcardType in project ceylon by eclipse.

the class ReflectionType method isRaw.

@Override
public boolean isRaw() {
    if (type instanceof ParameterizedType) {
        // we're raw if our type is a parameterised type that should have type params
        ParameterizedType ptype = ((ParameterizedType) type);
        Class<?> klass = (Class<?>) ptype.getRawType();
        return klass.getTypeParameters().length != ptype.getActualTypeArguments().length;
    }
    if (type instanceof GenericArrayType)
        return getComponentType().isRaw();
    if (type instanceof TypeVariable)
        return false;
    if (type instanceof WildcardType)
        return false;
    if (type instanceof Class) {
        // we're raw if our type is a parameterised type that should have type params
        return ((Class<?>) type).getTypeParameters().length != 0;
    }
    throw new RuntimeException("Unknown type: " + type);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 59 with WildcardType

use of java.lang.reflect.WildcardType in project spring-cloud-gateway by spring-cloud.

the class ServletOutputToInputConverter method exchange.

private ResponseEntity<T> exchange(RequestEntity<?> requestEntity) {
    Type type = this.responseType;
    if (type instanceof TypeVariable || type instanceof WildcardType) {
        type = Object.class;
    }
    RequestCallback requestCallback = rest.httpEntityCallback((Object) requestEntity, type);
    ResponseExtractor<ResponseEntity<T>> responseExtractor = rest.responseEntityExtractor(type);
    return rest.execute(requestEntity.getUrl(), requestEntity.getMethod(), requestCallback, responseExtractor);
}
Also used : WildcardType(java.lang.reflect.WildcardType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ResponseEntity(org.springframework.http.ResponseEntity) RequestCallback(org.springframework.web.client.RequestCallback) TypeVariable(java.lang.reflect.TypeVariable)

Example 60 with WildcardType

use of java.lang.reflect.WildcardType in project mule by mulesoft.

the class GenericsUtils method extractTypeFromParameterizedType.

/**
 * Extract the generic type from the given ParameterizedType object.
 *
 * @param methodParam the method parameter specification
 * @param ptype the ParameterizedType to check
 * @param source the expected raw source type (can be <code>null</code>)
 * @param typeIndex the index of the actual type argument
 * @param nestingLevel the nesting level of the target type
 * @param currentLevel the current nested level
 * @return the generic type as Class, or <code>null</code> if none
 */
private static Class<?> extractTypeFromParameterizedType(MethodParameter methodParam, ParameterizedType ptype, Class<?> source, int typeIndex, int nestingLevel, int currentLevel) {
    if (!(ptype.getRawType() instanceof Class<?>)) {
        return null;
    }
    Class<?> rawType = (Class<?>) ptype.getRawType();
    Type[] paramTypes = ptype.getActualTypeArguments();
    if (nestingLevel - currentLevel > 0) {
        int nextLevel = currentLevel + 1;
        Integer currentTypeIndex = (methodParam != null ? methodParam.getTypeIndexForLevel(nextLevel) : null);
        // Default is last parameter type: Collection element or Map value.
        int indexToUse = (currentTypeIndex != null ? currentTypeIndex : paramTypes.length - 1);
        Type paramType = paramTypes[indexToUse];
        return extractType(methodParam, paramType, source, typeIndex, nestingLevel, nextLevel);
    }
    if (source != null && !source.isAssignableFrom(rawType)) {
        return null;
    }
    Class<?> fromSuperclassOrInterface = extractTypeFromClass(methodParam, rawType, source, typeIndex, nestingLevel, currentLevel);
    if (fromSuperclassOrInterface != null) {
        return fromSuperclassOrInterface;
    }
    if (paramTypes == null || typeIndex >= paramTypes.length) {
        return null;
    }
    Type paramType = paramTypes[typeIndex];
    if (paramType instanceof TypeVariable<?> && methodParam != null && methodParam.typeVariableMap != null) {
        Type mappedType = methodParam.typeVariableMap.get(paramType);
        if (mappedType != null) {
            paramType = mappedType;
        }
    }
    if (paramType instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) paramType;
        Type[] upperBounds = wildcardType.getUpperBounds();
        if (upperBounds != null && upperBounds.length > 0 && !Object.class.equals(upperBounds[0])) {
            paramType = upperBounds[0];
        } else {
            Type[] lowerBounds = wildcardType.getLowerBounds();
            if (lowerBounds != null && lowerBounds.length > 0 && !Object.class.equals(lowerBounds[0])) {
                paramType = lowerBounds[0];
            }
        }
    }
    if (paramType instanceof ParameterizedType) {
        paramType = ((ParameterizedType) paramType).getRawType();
    }
    if (paramType instanceof GenericArrayType) {
        // A generic array type... Let's turn it into a straight array type if possible.
        Type compType = ((GenericArrayType) paramType).getGenericComponentType();
        if (compType instanceof Class<?>) {
            Class<?> compClass = (Class<?>) compType;
            return Array.newInstance(compClass, 0).getClass();
        }
    } else if (paramType instanceof Class<?>) {
        // We finally got a straight Class...
        return (Class<?>) paramType;
    }
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) 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) 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