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);
}
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;
}
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));
}
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);
}
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);
}
Aggregations