code: public static Type resolveGenericType(Type initialType, Class<?> targetType) {
          Class<?> rawType;
          if (initialType instanceof ParameterizedType)
              rawType = (Class<?>) ((ParameterizedType) initialType).getRawType();
          else
              rawType = (Class<?>) initialType;
  
          if (targetType.equals(rawType))
              return initialType;
  
          Type result;
          if (targetType.isInterface()) {
              for (Type superInterface : rawType.getGenericInterfaces())
                  if (superInterface != null && !superInterface.equals(Object.class))
                      if ((result = resolveGenericType(superInterface, targetType)) != null)
                          return result;
          }
  
          Type superType = rawType.getGenericSuperclass();
          if (superType != null && !superType.equals(Object.class))
              if ((result = resolveGenericType(superType, targetType)) != null)
                  return result;
  
          return null;
      }
docs: Resolves the generic Type for the {@code targetType} by walking the type hierarchy upwards from
  the {@code initialType}.