public static Type extractTypeArgument ( Type t , int index ) throws InvalidTypesException { if ( t instanceof ParameterizedType ) { Type [ ] actualTypeArguments = ( ( ParameterizedType ) t ) . getActualTypeArguments ( ) ; if ( index < 0 || index >= actualTypeArguments . length ) { throw new InvalidTypesException ( "Cannot extract the type argument with index " + index + " because the type has only " + actualTypeArguments . length + " type arguments." ) ; } else { return actualTypeArguments [ index ] ; } } else { throw new InvalidTypesException ( "The given type " + t + " is not a parameterized type." ) ; } }
This method extracts the n - th type argument from the given type . An InvalidTypesException is thrown if the type does not have any type arguments or if the index exceeds the number of type arguments .