* or generic array type, but not wildcard type) or {@code null} if unable to resolve at
* all
*/
public static Type resolveTypeVariable(List<Type> context, TypeVariable<?> typeVariable) {
// determine where the type variable was declared
GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
if (genericDeclaration instanceof Class<?>) {
Class<?> rawGenericDeclaration = (Class<?>) genericDeclaration;
// check if the context extends that declaration
int contextIndex = context.size();
ParameterizedType parameterizedType = null;
while (parameterizedType == null && --contextIndex >= 0) {
parameterizedType =
getSuperParameterizedType(context.get(contextIndex), rawGenericDeclaration);
}
if (parameterizedType != null) {
// find the type variable's index in the declaration's type parameters
TypeVariable<?>[] typeParameters = genericDeclaration.getTypeParameters();
int index = 0;
for (; index < typeParameters.length; index++) {
TypeVariable<?> typeParameter = typeParameters[index];
if (typeParameter.equals(typeVariable)) {
break;