You could try ((ParameterizedType)f.getGenericType()).getActualTypeArguments()
to get a Type[]
array which in your case should have 1 element: X.class
. Note that this only works in cases where you can use reflection (i.e. not for local variables) and if the types are boundaries you'd need to handle it differently. Also ,you'd need to check if getGenericType()
really returns a ParameterizedType
.
Other methods you might want to look into:
- for classes use
getTypeParameters()
, this would return an empty array for non generic classes
- for method return types use
getGenericReturnType()
- for method and constructor parameters use
getGenericParameterTypes()
, this would return an empty array for no-arg methods
Some types/interfaces they might return:
- actual classes for raw types
ParameterizedType
for generic fields, parameters and return types
TypeVariable
for generic type parameters without wildcards
WildcardType
for generic type parameters with a wildcard