use of java.lang.reflect.ParameterizedType 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;
}
}
}
use of java.lang.reflect.ParameterizedType in project retrofit by square.
the class Utils method resolveTypeVariable.
private static Type resolveTypeVariable(Type context, Class<?> contextRawType, TypeVariable<?> unknown) {
Class<?> declaredByRaw = declaringClassOf(unknown);
// We can't reduce this further.
if (declaredByRaw == null)
return unknown;
Type declaredBy = getGenericSupertype(context, contextRawType, declaredByRaw);
if (declaredBy instanceof ParameterizedType) {
int index = indexOf(declaredByRaw.getTypeParameters(), unknown);
return ((ParameterizedType) declaredBy).getActualTypeArguments()[index];
}
return unknown;
}
use of java.lang.reflect.ParameterizedType in project retrofit by square.
the class CallAdapterTest method parameterizedTypeInvalidIndex.
@Test
public void parameterizedTypeInvalidIndex() {
ParameterizedType listOfString = (ParameterizedType) new TypeToken<List<String>>() {
}.getType();
try {
getParameterUpperBound(-1, listOfString);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Index -1 not in range [0,1) for java.util.List<java.lang.String>");
}
try {
getParameterUpperBound(1, listOfString);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Index 1 not in range [0,1) for java.util.List<java.lang.String>");
}
}
use of java.lang.reflect.ParameterizedType in project javapoet by square.
the class ParameterizedTypeName method get.
/** Returns a parameterized type equivalent to {@code type}. */
static ParameterizedTypeName get(ParameterizedType type, Map<Type, TypeVariableName> map) {
ClassName rawType = ClassName.get((Class<?>) type.getRawType());
ParameterizedType ownerType = (type.getOwnerType() instanceof ParameterizedType) && !Modifier.isStatic(((Class<?>) type.getRawType()).getModifiers()) ? (ParameterizedType) type.getOwnerType() : null;
List<TypeName> typeArguments = TypeName.list(type.getActualTypeArguments(), map);
return (ownerType != null) ? get(ownerType, map).nestedClass(rawType.simpleName(), typeArguments) : new ParameterizedTypeName(null, rawType, typeArguments);
}
use of java.lang.reflect.ParameterizedType in project moshi by square.
the class TypesTest method getFirstTypeArgument.
/**
* Given a parameterized type {@code A<B, C>}, returns B. If the specified type is not a generic
* type, returns null.
*/
public static Type getFirstTypeArgument(Type type) throws Exception {
if (!(type instanceof ParameterizedType))
return null;
ParameterizedType ptype = (ParameterizedType) type;
Type[] actualTypeArguments = ptype.getActualTypeArguments();
if (actualTypeArguments.length == 0)
return null;
return Types.canonicalize(actualTypeArguments[0]);
}
Aggregations