Search in sources :

Example 56 with ParameterizedType

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

the class Java8CallAdapterFactory method get.

@Override
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    if (getRawType(returnType) != CompletableFuture.class) {
        return null;
    }
    if (!(returnType instanceof ParameterizedType)) {
        throw new IllegalStateException("CompletableFuture return type must be parameterized" + " as CompletableFuture<Foo> or CompletableFuture<? extends Foo>");
    }
    Type innerType = getParameterUpperBound(0, (ParameterizedType) returnType);
    if (getRawType(innerType) != Response.class) {
        // Generic type is not Response<T>. Use it for body-only adapter.
        return new BodyCallAdapter<>(innerType);
    }
    // Generic type is Response<T>. Extract T and create the Response version of the adapter.
    if (!(innerType instanceof ParameterizedType)) {
        throw new IllegalStateException("Response must be parameterized" + " as Response<Foo> or Response<? extends Foo>");
    }
    Type responseType = getParameterUpperBound(0, (ParameterizedType) innerType);
    return new ResponseCallAdapter<>(responseType);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 57 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project flink by apache.

the class GroupReduceOperator method checkCombinability.

private boolean checkCombinability() {
    if (function instanceof GroupCombineFunction || function instanceof CombineFunction) {
        // check if the generic types of GroupCombineFunction and GroupReduceFunction match, i.e.,
        //   GroupCombineFunction<IN, IN> and GroupReduceFunction<IN, OUT>.
        // This is a best effort check. If the check cannot be done, we might fail at runtime.
        Type[] reduceTypes = null;
        Type[] combineTypes = null;
        Type[] genInterfaces = function.getClass().getGenericInterfaces();
        for (Type genInterface : genInterfaces) {
            if (genInterface instanceof ParameterizedType) {
                // get parameters of GroupReduceFunction
                if (((ParameterizedType) genInterface).getRawType().equals(GroupReduceFunction.class)) {
                    reduceTypes = ((ParameterizedType) genInterface).getActualTypeArguments();
                // get parameters of GroupCombineFunction
                } else if ((((ParameterizedType) genInterface).getRawType().equals(GroupCombineFunction.class)) || (((ParameterizedType) genInterface).getRawType().equals(CombineFunction.class))) {
                    combineTypes = ((ParameterizedType) genInterface).getActualTypeArguments();
                }
            }
        }
        if (reduceTypes != null && reduceTypes.length == 2 && combineTypes != null && combineTypes.length == 2) {
            if (reduceTypes[0].equals(combineTypes[0]) && reduceTypes[0].equals(combineTypes[1])) {
                return true;
            } else {
                LOG.warn("GroupCombineFunction cannot be used as combiner for GroupReduceFunction. " + "Generic types are incompatible.");
                return false;
            }
        } else if (reduceTypes == null || reduceTypes.length != 2) {
            LOG.warn("Cannot check generic types of GroupReduceFunction. " + "Enabling combiner but combine function might fail at runtime.");
            return true;
        } else {
            LOG.warn("Cannot check generic types of GroupCombineFunction. " + "Enabling combiner but combine function might fail at runtime.");
            return true;
        }
    }
    return false;
}
Also used : CombineFunction(org.apache.flink.api.common.functions.CombineFunction) GroupCombineFunction(org.apache.flink.api.common.functions.GroupCombineFunction) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) GroupCombineFunction(org.apache.flink.api.common.functions.GroupCombineFunction)

Example 58 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project gradle by gradle.

the class ProtocolToModelAdapter method convert.

private static Object convert(Type targetType, Object sourceObject, ViewDecoration decoration, ViewGraphDetails graphDetails) {
    if (targetType instanceof ParameterizedType) {
        ParameterizedType parameterizedTargetType = (ParameterizedType) targetType;
        if (parameterizedTargetType.getRawType() instanceof Class) {
            Class<?> rawClass = (Class<?>) parameterizedTargetType.getRawType();
            if (Iterable.class.isAssignableFrom(rawClass)) {
                Type targetElementType = getElementType(parameterizedTargetType, 0);
                return convertCollectionInternal(rawClass, targetElementType, (Iterable<?>) sourceObject, decoration, graphDetails);
            }
            if (Map.class.isAssignableFrom(rawClass)) {
                Type targetKeyType = getElementType(parameterizedTargetType, 0);
                Type targetValueType = getElementType(parameterizedTargetType, 1);
                return convertMap(rawClass, targetKeyType, targetValueType, (Map<?, ?>) sourceObject, decoration, graphDetails);
            }
        }
    }
    if (targetType instanceof Class) {
        if (((Class) targetType).isPrimitive()) {
            return sourceObject;
        }
        return createView((Class) targetType, sourceObject, decoration, graphDetails);
    }
    throw new UnsupportedOperationException(String.format("Cannot convert object of %s to %s.", sourceObject.getClass(), targetType));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 59 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project android-classyshark by google.

the class MetaObjectClass method getDeclaredFields.

@Override
public FieldInfo[] getDeclaredFields() {
    Field[] implFields = clazz.getDeclaredFields();
    List<FieldInfo> result = new ArrayList<>();
    for (Field field : implFields) {
        FieldInfo fi = new FieldInfo();
        fi.typeName = field.getType().getName();
        fi.modifiers = field.getModifiers();
        fi.annotations = convertAnnotations(field.getAnnotations());
        fi.name = field.getName();
        Type type = field.getGenericType();
        if (type instanceof ParameterizedType) {
            ParameterizedType pType = (ParameterizedType) type;
            fi.genericStr = getFieldGenericsString(pType.getActualTypeArguments());
        }
        result.add(fi);
    }
    FieldInfo[] array = new FieldInfo[result.size()];
    return result.toArray(array);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList)

Example 60 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project android-classyshark by google.

the class MetaObjectClass method convertParameters.

private ParameterInfo[] convertParameters(Class<?>[] parameterTypes, Type[] genericParameterTypes) {
    List<ParameterInfo> result = new ArrayList<>();
    for (int i = 0; i < parameterTypes.length; i++) {
        Class param = parameterTypes[i];
        ParameterInfo pi = new ParameterInfo();
        pi.parameterStr = param.getName();
        if (genericParameterTypes != null && i < genericParameterTypes.length) {
            Type genericParameterType = genericParameterTypes[i];
            if (genericParameterType instanceof ParameterizedType) {
                ParameterizedType aType = (ParameterizedType) genericParameterType;
                Type[] parameterArgTypes = aType.getActualTypeArguments();
                pi.genericStr = getFieldGenericsString(parameterArgTypes);
            }
        }
        result.add(pi);
    }
    ParameterInfo[] array = new ParameterInfo[result.size()];
    return result.toArray(array);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)704 Type (java.lang.reflect.Type)532 GenericArrayType (java.lang.reflect.GenericArrayType)226 WildcardType (java.lang.reflect.WildcardType)182 TypeVariable (java.lang.reflect.TypeVariable)137 ArrayList (java.util.ArrayList)94 Method (java.lang.reflect.Method)71 Test (org.junit.Test)59 List (java.util.List)55 Field (java.lang.reflect.Field)49 Map (java.util.Map)47 HashMap (java.util.HashMap)42 Collection (java.util.Collection)26 MediaType (javax.ws.rs.core.MediaType)23 Annotation (java.lang.annotation.Annotation)18 HashSet (java.util.HashSet)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)13 ImmutableList (com.google.common.collect.ImmutableList)12 TypeLiteral (com.google.inject.TypeLiteral)12 IOException (java.io.IOException)11