I have this interface:
public interface EventHandler {
void handle(T event);
}
And this class implementing it:>
Resolve the type of T
by the generic interface. E.g.
public interface SomeInterface {
}
public class SomeImplementation implements SomeInterface {
public Class getGenericInterfaceType(){
Class clazz = getClass();
ParameterizedType parameterizedType = (ParameterizedType) clazz.getGenericInterfaces()[0];
Type[] typeArguments = parameterizedType.getActualTypeArguments();
Class> typeArgument = (Class>) typeArguments[0];
return typeArgument;
}
}
public static void main(String[] args) {
SomeImplementation someImplementation = new SomeImplementation();
System.out.println(someImplementation.getGenericInterfaceType());
}
PS: Keep in mind that the acutalTypeArguments are of type Type
. They must not be a Class
. In your case it is a Class because your type definition is EventHandler
.