@SuppressWarnings("PMD.AvoidDeeplyNestedIfStmts")
    public static Optional<Type> findGenericParameter(Type type, Class<?> parameterizedSupertype, int n) {
        if (type instanceof Class) {
            Type genericSuper = ((Class<?>) type).getGenericSuperclass();
            if (genericSuper instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) genericSuper;
                if (pt.getRawType() == parameterizedSupertype) {
                    return Optional.ofNullable(pt.getActualTypeArguments()[n]);
                }
            }
        }

        // fall back to the type reflector
        return Optional.ofNullable(GenericTypeReflector.getTypeParameter(type, parameterizedSupertype.getTypeParameters()[n]));
    }
For the given type which extends parameterizedSupertype, returns the
nth generic parameter for the parameterized supertype, if concretely
expressed.

<p>
Example:
</p>
<ul>
<li>if {@code type} is {@code ArrayList<String>},
{@code parameterizedSupertype} is {@code List.class},
and {@code n} is {@code 0},
returns {@code Optional.of(String.class)}.</li>

<li>if {@code type} is {@code Map<String, Integer>},
{@code parameterizedSupertype} is {@code Map.class},
and {@code n} is {@code 1},
returns {@code Optional.of(Integer.class)}.</li>

<li>if {@code type} is {@code ArrayList.class} (raw),
{@code parameterizedSupertype} is {@code List.class},
and {@code n} is {@code 0},
returns {@code Optional.empty()}.</li>
</ul>

@param type the subtype of parameterizedSupertype
@param parameterizedSupertype the parameterized supertype from which we want the generic parameter
@param n the index in {@code Foo<X, Y, Z, ...>}
@return the parameter on the supertype, if it is concretely defined.
@throws ArrayIndexOutOfBoundsException if n &gt; the number of type variables the type has
core/src/main/java/org/jdbi/v3/core/generic/GenericTypes.java