use of java.lang.reflect.TypeVariable in project robovm by robovm.
the class GenericArrayTypeTest method testGetGenericComponentType.
public void testGetGenericComponentType() throws Exception {
@SuppressWarnings("unchecked") Class<? extends A> clazz = GenericArrayTypeTest.A.class;
Field field = clazz.getDeclaredField("array");
Type genericType = field.getGenericType();
assertInstanceOf(GenericArrayType.class, genericType);
Type componentType = ((GenericArrayType) genericType).getGenericComponentType();
assertEquals(getTypeParameter(clazz), componentType);
assertInstanceOf(TypeVariable.class, componentType);
TypeVariable<?> componentTypeVariable = (TypeVariable<?>) componentType;
assertEquals("T", componentTypeVariable.getName());
assertEquals(clazz, componentTypeVariable.getGenericDeclaration());
}
use of java.lang.reflect.TypeVariable in project bazel by bazelbuild.
the class GenericTypeHelper method getActualReturnType.
/**
* Resolves the return type of a method, in particular if the generic return
* type ({@link Method#getGenericReturnType()}) is a type variable
* ({@link TypeVariable}), by checking all super-classes and directly
* implemented interfaces.
*
* <p>The method m must be defined by the given type or by its raw class type.
*
* @throws AssertionError if the generic return type could not be resolved
*/
// TODO(bazel-team): also check enclosing classes and indirectly implemented
// interfaces, which can also contribute type variables. This doesn't happen
// in the existing use cases.
public static Type getActualReturnType(Type type, Method method) {
Type returnType = method.getGenericReturnType();
if (returnType instanceof Class<?>) {
return returnType;
} else if (returnType instanceof ParameterizedType) {
return returnType;
} else if (returnType instanceof TypeVariable<?>) {
TypeVariable<?> variable = (TypeVariable<?>) returnType;
while (type != null) {
Type candidate = matchTypeVariable(type, variable);
if (candidate != null) {
return candidate;
}
Class<?> rawType = getRawType(type);
for (Type interfaceType : rawType.getGenericInterfaces()) {
candidate = matchTypeVariable(interfaceType, variable);
if (candidate != null) {
return candidate;
}
}
type = rawType.getGenericSuperclass();
}
}
throw new AssertionError("The type " + returnType + " is not a Class, ParameterizedType, or TypeVariable");
}
use of java.lang.reflect.TypeVariable 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.TypeVariable in project moshi by square.
the class Types 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 : arrayOf(newComponentType);
} else if (toResolve instanceof GenericArrayType) {
GenericArrayType original = (GenericArrayType) toResolve;
Type componentType = original.getGenericComponentType();
Type newComponentType = resolve(context, contextRawType, componentType);
return componentType == newComponentType ? original : arrayOf(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 supertypeOf(lowerBound);
}
} else if (originalUpperBound.length == 1) {
Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
if (upperBound != originalUpperBound[0]) {
return subtypeOf(upperBound);
}
}
return original;
} else {
return toResolve;
}
}
}
use of java.lang.reflect.TypeVariable in project platform_frameworks_base by android.
the class TypeReference method toString.
private static void toString(Type type, StringBuilder out) {
if (type == null) {
return;
} else if (type instanceof TypeVariable<?>) {
// T
out.append(((TypeVariable<?>) type).getName());
} else if (type instanceof Class<?>) {
Class<?> klass = (Class<?>) type;
out.append(klass.getName());
toString(klass.getTypeParameters(), out);
} else if (type instanceof ParameterizedType) {
// "Foo<T1, T2, T3, ... Tn>"
ParameterizedType p = (ParameterizedType) type;
out.append(((Class<?>) p.getRawType()).getName());
toString(p.getActualTypeArguments(), out);
} else if (type instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) type;
toString(gat.getGenericComponentType(), out);
out.append("[]");
} else {
// WildcardType, BoundedType
// TODO:
out.append(type.toString());
}
}
Aggregations