I have an interface called Listener:
public interface Listener<T> {
void listen(T t);
}
Afterwards, i create some anonymous instances of this listener and add them to a list, like
EventManager.registerListener((Listener<PacketSendEvent>) packetSendEvent ->
System.out.println("Packet has been sent: " + packetSendEvent.getPacket().getClass().getSimpleName()));
and registerListener simply adds them to my list.
Later in the program, i iterate throug this list and get the generic parameters names of these listeners. In the case of the listener shown above, this should be "PacketSendEvent":
listeners.forEach(listener -> {
String[] split = listener.getClass().getGenericInterfaces()[0].getTypeName().split("\\.");
String className = split[split.length - 1];
className = className.substring(0, className.length() - 1);
System.out.println("Loaded listener " + listener.getClass().getName() + " which listens to " + className);
});
For listeners where i create a normal class this works as it should and prints the generic parameter's class name, but for my anonymous instances it doest not work.
What i figured out:
For every normal class that implements Listener, the method listener.getClass().getGenericInterfaces()
returns the generic interfaces. But for my anonymous class it does not return my generic interfaces, it returns the same as listener.getClass().getInterfaces()
.
I really dont know how i should fix this, probably some of you know the solution. I appreciate every help i can get.
Kind regards Luca