-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Interfaces like in guava #67
Comments
Good call! Somehow it didn't occur to me but it obviously makes a lot of sense. I will include that into the next release. That also raises the question of how to deal with conflicting Handler definitions when multiple interfaces define the same handler. I think I would disallow such a configuration with a validation error. Any thoughts on that? |
Since Java 8 this also has the advantage that it would be possible to add handlers using lambda-expressions that implement the annotated interface. I think just disallowing two @handler definitions on the same method implementation should be sufficient. Otherwise you would need to have some sophisticated overwrite logic. |
Agree to the |
Lambdas support is really lacking |
I've added basic support (no Enveloped) for Java 8 lambdas ( Code snippet (in Kotlin): import com.azagroup.anizoptera.common.reflection.invokeMethod
import net.engio.mbassy.common.ReflectionUtils
import net.engio.mbassy.listener.*
public class AzaMetadataReader : MetadataReader()
{
override fun getMessageListener(target: Class<*>): MessageListener<*>
{
val listener = super.getMessageListener(target)
if (listener.getHandlers().isNotEmpty())
return listener
// FunctionalInterface and similars support
// We support only classes with one method
val methods = target.getDeclaredMethods().filter { !it.isBridge() }
if (methods.size() > 1)
return listener
// We support only methods with one parameter
val handler = methods.first()
if (handler.getParameterCount() != 1)
return listener
// Parameter should be specific, not java.lang.Object
val parameter = handler.getParameterTypes().first()
if (parameter == javaClass<Any>())
return listener
val handlerConfig: Handler = ReflectionUtils.getAnnotation(target, javaClass<Handler>())
?: ReflectionUtils.getAnnotation(javaClass<Stub>(), javaClass<Handler>())
val handlerProperties = MessageHandler.Properties.Create(
handler, handlerConfig, getFilter0(handlerConfig), listener)
val handlerMetadata = MessageHandler(handlerProperties)
listener addHandler handlerMetadata
return listener
}
private fun getFilter0(subscription: Handler): Array<IMessageFilter<*>>?
// Reflection call to private method, ugly, but working :)
= invokeMethod("getFilter", subscription) as Array<IMessageFilter<*>>?
// Stub for clear Handler annotation instance
private Handler class Stub
} May be this snippet will help to add support to the very library :) |
Valuable enhancement! |
Hi, I am a big fan of this library and have used it in multiple projects now. Thanks for all your hard work on it! I would love to be able to subscribe to event using lambda expressions... so was going to have a go at implementing a solution to this. Just wondering if there was any more recent thoughts on how to achieve this? Given that I haven't really much experience with the codebase - is this reletively simple to implement? I too like the idea of a simple "latest handler overrides the previous" approach to multiple handler implementations. Cheers! |
Add-on to previous message: I have a solution for the first part of this (basic interface support). How do you feel about using Java8 idioms within the codebase (I guess it remains supporting 1.6?) But either way adding support for this part seems straight forward. The second part seems a little trickier. I implemented a simple functional interface, something like: public interface IListenerFunction<T> {
@Handler
void handle(T message);
} This doesn't seem to play nicely, however, when I override the So for now I have a working solution to subscribing using interfaces (however not allowing for enveloping yet), but currently no solution to support lambda. Sorry if I have not yet understood the code well enough to be asking clear questions! Please let me know if I can clarify further! :) EDIT: Updated after learning more about implenting a |
I have created a PR (#153) with just the interface support for now, and not followed up on supporting lambda. If get more time I will continue to try and include the lambda support - but thought the interface support might be useful anyway. |
Hello. Would be great if mbassador supports interfaces
like:
public interface HasSessionEventHandlers {
@handler
public void onBind(BindEvent bindEvent);
}
The text was updated successfully, but these errors were encountered: