TLDR: I need a method to get the being implemented as an interface in a class using reflection. (read the last line too, please...)
The problem is simple, but I can't solve it, I have an interface EventListener, with the following code:
public @IndexSubclasses interface EventListener<T> {
public void onEvent(T event);
}
And I have an implementation, the JoinEventSampleBase (which is only a sample to be copied), with the following code:
@Scope.Server @FrameworkEvent
public class JoinEventBaseSample implements EventListener<JoinEvent> {
@Override
public void onEvent(JoinEvent event) {
// Code here
}
}
I had tried the following on my EventManager:
Class<?> eventIndication = Arrays.stream(listener.getInterfaces())
.filter(aClass -> aClass.getName().equals(EventListener.class.getName()))
.findFirst().get().getComponentType();
It is confuse because it was only a test.
I got the following error:
java.lang.NullPointerException: Cannot invoke "java.lang.Class.getDeclaredField(java.lang.String)" because "eventIndication" is null
The line that is doing the error after the "eventIndication" var is the following:
Object instance = eventIndication.getDeclaredField("instance").get(null);
Finally, my objective is access the instance method into the eventIndication field which is into the JoinEvent class in
implements EventListener<JoinEvent>