public static Optional < Class > resolveInterfaceTypeArgument ( Class type , Class interfaceType ) { Type [ ] genericInterfaces = type . getGenericInterfaces ( ) ; for ( Type genericInterface : genericInterfaces ) { if ( genericInterface instanceof ParameterizedType ) { ParameterizedType pt = ( ParameterizedType ) genericInterface ; if ( pt . getRawType ( ) == interfaceType ) { return resolveSingleTypeArgument ( genericInterface ) ; } } } Class superClass = type . getSuperclass ( ) ; if ( superClass != null && superClass != Object . class ) { return resolveInterfaceTypeArgument ( superClass , interfaceType ) ; } return Optional . empty ( ) ; }
Resolves a single type argument from the given interface of the given class . Also searches superclasses .