[Suggestion] Add Subscriber Parameter to @Hook Annotation
Friendly-Banana opened this issue · 3 comments
I would like to register the callback directly in the config when declaring the hook instead of having to use a subscribe method.
Example code:
@Hook(consumer)
public int foo;
I am not sure how multiple methods would be handled, maybe take an array of Consumers
@Hook({consumer1, consumer2})
public int foo;
or just use multiple annotations
@Hook(consumer1)
@Hook(consumer2)
public int foo;
How exactly do you envision this to work? I will assume you're familiar with the fact that Java Annotations can only contain constant values, and thus the only viable solution would be referring to the name of a subscriber method defined on the config model with a string. This makes for quite awkward semantics as that's almost certainly not where that code should be located - the listener should ideally be registered close to the actual code and/or data which it is affecting. If you have any better suggestions for how this could be implemented, please tell us as this is not generally a bad idea
Cheers
Yes, I'm familiar with the fact there is no easy way to pass methods.
The string thing got me thinking though, what if we take a class and a string with the method name and then import/qualify the method using the class? This would allow the method to be anywhere, not only on the config. Alternatively take a string of the class name?
@MyAnnotation(clazz=MyClass.class, method="method")
@interface MyAnnotation {
Class<?> clazz();
String method();
}
Another way would be this StackOverflow answer, passing the complete signature:
@MyAnnotation(clazz=String.class, method="contains", params= {CharSequence.class})
@interface MyAnnotation {
Class<?> clazz();
String method();
Class<?>[] params() default {};
}
MyAnnotation annotation = annotation.clazz().getMethod(annotation.method(), annotation.params());
What do you think @gliscowo would this work?
After considering this again, I don't think that there's really any satisfying approach
While I'd also like to see some form of method referencing in annotations, the current hacks we could use involving magic strings together with reflection are not adequate for owo. There would be no IDE support, some extremely unpretty reflection code in the backend and very little benefit over simply registering the listeners in a static
block of your config model class.
As such, I'm sorry to say that I'll close this as not planned for the time being.
Thanks for bringing up the idea and have a great day