Similar interface
s are used for an event system as "listeners"
interface Updatable {
void update();
}
Events (→ all "listen" methods) are called by
spot(Method, Object... event);
To achieve. Get java.lang.reflect.Method
of "listeners" like update
in Updatable
.
Attempt. An option is to use reflection (unsafe) to find out the Method
to call
Usage. Create a constant for every method in the "listener" interface and use it as an argument
interface Updatable {
void update();
Method UPDATE = method(Updatable.class, "update");
}
//anywhere
spot(UPDATE);
using this util function
static public @Nullable Method method(Class<?> of, String name, Class... params) {
try {
return of.getDeclaredMethod(name, params);
}
catch(Exception e) {/*...*/}
}
Questions. Is there a
- safer
- simpler
way to get reflect.Method
s in (< 8) java ?