@Nullable
	public static Type getTypeArgument(Class<?> cls, String typeParameter) {
		while (cls != Object.class) { // walk up the Class hierarchy, searching for the type param
			Class<?> parent = cls.getSuperclass();
			Type parentType = cls.getGenericSuperclass();
			if (parentType instanceof ParameterizedType) {
				Type[] args = ((ParameterizedType) parentType).getActualTypeArguments();
				TypeVariable<?>[] params = parent.getTypeParameters();
				for (int i = 0, length = params.length; i < length; i++) {
					if (params[i].getName().equals(typeParameter)) {
						return args[i];
					}
				}
			}
			cls = parent;
		}
		return null;
	}
@ Nullable public static Type getTypeArgument ( Class < ? > cls , String typeParameter ) { while ( cls != Object . class ) { Class < ? > parent = cls . getSuperclass ( ) ; Type parentType = cls . getGenericSuperclass ( ) ; if ( parentType instanceof ParameterizedType ) { Type [ ] args = ( ( ParameterizedType ) parentType ) . getActualTypeArguments ( ) ; TypeVariable < ? > [ ] params = parent . getTypeParameters ( ) ; for ( int i = 0 , length = params . length ; i < length ; i ++ ) { if ( params [ i ] . getName ( ) . equals ( typeParameter ) ) { return args [ i ] ; } } } cls = parent ; } return null ; }
<p>
Get the Type that has been specified by the class or an ancestor for the generic type
parameter. For example:
</p>
<pre>{@code
class StringList extends ArrayList<String>

Classes.getTypeArgument(StringList.class, "E")
// returns: class java.lang.String
}</pre>

@return null if the type parameter was not found