Skip to content

Commit

Permalink
Merge pull request #153 from chriskingnet/add-support-for-interfaces
Browse files Browse the repository at this point in the history
Adding support for defining message handlers in interfaces
  • Loading branch information
bennidi committed Mar 7, 2018
2 parents 3e72321 + 6af5217 commit 4e8692b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
25 changes: 17 additions & 8 deletions src/main/java/net/engio/mbassy/common/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,29 @@ public static Method[] getMethods(IPredicate<Method> condition, Class<?> target)

public static void getMethods(IPredicate<Method> condition, Class<?> target, ArrayList<Method> methods) {
try {
for ( Method method : target.getDeclaredMethods() ) {
if ( condition.apply( method ) ) {
methods.add( method );
for (Method method : target.getDeclaredMethods()) {
if (condition.apply(method)) {
methods.add(method);
}
}

for (Class superType : getSuperTypes(target)) {
if (superType.equals(Object.class)) {
continue;
}

for (Method superTypeMethod : superType.getDeclaredMethods()) {
if (condition.apply(superTypeMethod )) {
methods.add(superTypeMethod);
}
}
}
}
catch ( Exception e ) {
catch (Exception e) {
//nop
}
if ( !target.equals( Object.class ) ) {
getMethods(condition, target.getSuperclass(), methods);
}
}

/**
* Traverses the class hierarchy upwards, starting at the given subclass, looking
* for an override of the given methods -> finds the bottom most override of the given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static final class Properties{
*/
public static final Map<String, Object> Create(Method handler,
Handler handlerConfig,
Enveloped enveloped,
IMessageFilter[] filter,
MessageListener listenerConfig){
if(handler == null){
Expand All @@ -53,7 +54,6 @@ public static final Map<String, Object> Create(Method handler,
if(filter == null){
filter = new IMessageFilter[]{};
}
Enveloped enveloped = ReflectionUtils.getAnnotation( handler, Enveloped.class );
Class[] handledMessages = enveloped != null
? enveloped.messages()
: handler.getParameterTypes();
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/net/engio/mbassy/listener/MetadataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,23 @@ public MessageListener getMessageListener(Class target) {
if (!ReflectionUtils.containsOverridingMethod(allHandlers, handler)) {

// for each handler there will be no overriding method that specifies @Handler annotation
// but an overriding method does inherit the listener configuration of the overwritten method
// but an overriding method does inherit the listener configuration of the overridden method

Handler handlerConfig = ReflectionUtils.getAnnotation(handler, Handler.class);
Enveloped enveloped = ReflectionUtils.getAnnotation( handler, Enveloped.class );

if (!handlerConfig.enabled() || !isValidMessageHandler(handler)) {
continue; // disabled or invalid listeners are ignored
}

Method overriddenHandler = ReflectionUtils.getOverridingMethod(handler, target);
// if a handler is overwritten it inherits the configuration of its parent method
// if a handler is overridden it inherits the configuration of its parent method
Map<String, Object> handlerProperties = MessageHandler.Properties.Create(overriddenHandler == null ? handler : overriddenHandler,
handlerConfig,
enveloped,
getFilter(handler, handlerConfig),
listenerMetadata);

MessageHandler handlerMetadata = new MessageHandler(handlerProperties);
listenerMetadata.addHandler(handlerMetadata);
}
Expand Down
36 changes: 29 additions & 7 deletions src/test/java/net/engio/mbassy/MetadataReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ public void testListenerWithoutInheritance() {
validator.check(listener);
}

/*
@Test
public void testInterfaced() {
MessageListener listener = reader.getMessageListener(InterfacedListener.class);
ListenerValidator validator = new ListenerValidator()
.expectHandlers(1, Object.class);
.expectHandlers(1, String.class);
validator.check(listener);
}

@Test
public void testInterfacedEnveloped() {
MessageListener listener = reader.getMessageListener(EnvelopedInterfacedListener.class);
ListenerValidator validator = new ListenerValidator()
.expectHandlers(1, Integer.class);
validator.check(listener);
} WIP */
}


@Test
Expand Down Expand Up @@ -207,18 +215,32 @@ public void handleEnveloped2(MessageEnvelope o) {

}

public static interface ListenerInterface{
public interface EnvelopedListenerInterface {

@Handler
@Enveloped(messages = Object.class)
@Enveloped(messages = Integer.class)
void handle(MessageEnvelope envelope);
}

public class InterfacedListener implements ListenerInterface{
public class EnvelopedInterfacedListener implements EnvelopedListenerInterface {

@Override
public void handle(MessageEnvelope envelope) {
//

}
}

public interface ListenerInterface {

@Handler
void handle(String str);
}

public class InterfacedListener implements ListenerInterface {

@Override
public void handle(String str) {

}
}

Expand Down

0 comments on commit 4e8692b

Please sign in to comment.