From e226cea8b28e2f0d4d4146500ac91f14bdfc56c3 Mon Sep 17 00:00:00 2001 From: "Belk, Thomas W" Date: Sun, 10 Jan 2016 21:12:38 -0500 Subject: [PATCH 1/8] When registering/unregistering within parent classes consumers now have the ability to point to the parent and use producer/subscriber annotated methods within. This uses the child class instance to reflectively trigger the parent class method. --- .../squareup/otto/AnnotatedHandlerFinder.java | 81 ++++++++++++++++++- otto/src/main/java/com/squareup/otto/Bus.java | 53 +++++++++++- .../java/com/squareup/otto/HandlerFinder.java | 14 ++++ 3 files changed, 141 insertions(+), 7 deletions(-) diff --git a/otto/src/main/java/com/squareup/otto/AnnotatedHandlerFinder.java b/otto/src/main/java/com/squareup/otto/AnnotatedHandlerFinder.java index 7d79b71..3067937 100644 --- a/otto/src/main/java/com/squareup/otto/AnnotatedHandlerFinder.java +++ b/otto/src/main/java/com/squareup/otto/AnnotatedHandlerFinder.java @@ -49,18 +49,54 @@ private static void loadAnnotatedProducerMethods(Class listenerClass, loadAnnotatedMethods(listenerClass, producerMethods, subscriberMethods); } + private static void loadAnnotatedProducerMethods(Class fakeListenerClass, + Class listenerClass, Map, Method> producerMethods) { + Map, Set> subscriberMethods = new HashMap, Set>(); + loadAnnotatedMethods(fakeListenerClass, listenerClass, producerMethods, subscriberMethods); + } + private static void loadAnnotatedSubscriberMethods(Class listenerClass, Map, Set> subscriberMethods) { Map, Method> producerMethods = new HashMap, Method>(); loadAnnotatedMethods(listenerClass, producerMethods, subscriberMethods); } + private static void loadAnnotatedSubscriberMethods(Class fakeListenerClass, + Class listenerClass, Map, Set> subscriberMethods) { + Map, Method> producerMethods = new HashMap, Method>(); + loadAnnotatedMethods(fakeListenerClass, listenerClass, producerMethods, subscriberMethods); + } + /** * Load all methods annotated with {@link Produce} or {@link Subscribe} into their respective caches for the * specified class. */ private static void loadAnnotatedMethods(Class listenerClass, Map, Method> producerMethods, Map, Set> subscriberMethods) { + loadAnnotatedMethodsForListenerClass(listenerClass, producerMethods, subscriberMethods); + + PRODUCERS_CACHE.put(listenerClass, producerMethods); + SUBSCRIBERS_CACHE.put(listenerClass, subscriberMethods); + } + + /** + * Load all methods annotated with {@link Produce} or {@link Subscribe} into their respective caches for the + * specified class. + */ + private static void loadAnnotatedMethods(Class fakeListenerClass, Class listenerClass, + Map, Method> producerMethods, Map, Set> subscriberMethods) { + loadAnnotatedMethodsForListenerClass(listenerClass, producerMethods, subscriberMethods); + + PRODUCERS_CACHE.put(fakeListenerClass, producerMethods); + SUBSCRIBERS_CACHE.put(fakeListenerClass, subscriberMethods); + } + + /** + * Load all methods annotated with {@link Produce} or {@link Subscribe} into their respective caches for the + * specified class. + */ + private static void loadAnnotatedMethodsForListenerClass(Class listenerClass, Map, Method> producerMethods, + Map, Set> subscriberMethods) { for (Method method : listenerClass.getDeclaredMethods()) { // The compiler sometimes creates synthetic bridge methods as part of the // type erasure process. As of JDK8 these methods now include the same @@ -124,9 +160,6 @@ private static void loadAnnotatedMethods(Class listenerClass, producerMethods.put(eventType, method); } } - - PRODUCERS_CACHE.put(listenerClass, producerMethods); - SUBSCRIBERS_CACHE.put(listenerClass, subscriberMethods); } /** This implementation finds all methods marked with a {@link Produce} annotation. */ @@ -149,6 +182,26 @@ static Map, EventProducer> findAllProducers(Object listener) { return handlersInMethod; } + /** This implementation finds all methods marked with a {@link Produce} annotation. */ + static Map, EventProducer> findAllProducers(Object listener, Class listenerClass) { + Class fakeListenerClass = listener.getClass(); + Map, EventProducer> handlersInMethod = new HashMap, EventProducer>(); + + Map, Method> methods = PRODUCERS_CACHE.get(fakeListenerClass); + if (null == methods) { + methods = new HashMap, Method>(); + loadAnnotatedProducerMethods(fakeListenerClass, listenerClass, methods); + } + if (!methods.isEmpty()) { + for (Map.Entry, Method> e : methods.entrySet()) { + EventProducer producer = new EventProducer(listener, e.getValue()); + handlersInMethod.put(e.getKey(), producer); + } + } + + return handlersInMethod; + } + /** This implementation finds all methods marked with a {@link Subscribe} annotation. */ static Map, Set> findAllSubscribers(Object listener) { Class listenerClass = listener.getClass(); @@ -172,6 +225,28 @@ static Map, Set> findAllSubscribers(Object listener) { return handlersInMethod; } + static Map, Set> findAllSubscribers(Object listener, Class listenerClass) { + Class fakeListenerClass = listener.getClass(); + Map, Set> handlersInMethod = new HashMap, Set>(); + + Map, Set> methods = SUBSCRIBERS_CACHE.get(fakeListenerClass); + if (null == methods) { + methods = new HashMap, Set>(); + loadAnnotatedSubscriberMethods(fakeListenerClass, listenerClass, methods); + } + if (!methods.isEmpty()) { + for (Map.Entry, Set> e : methods.entrySet()) { + Set handlers = new HashSet(); + for (Method m : e.getValue()) { + handlers.add(new EventHandler(listener, m)); + } + handlersInMethod.put(e.getKey(), handlers); + } + } + + return handlersInMethod; + } + private AnnotatedHandlerFinder() { // No instances. } diff --git a/otto/src/main/java/com/squareup/otto/Bus.java b/otto/src/main/java/com/squareup/otto/Bus.java index 0ab6620..f891f1f 100644 --- a/otto/src/main/java/com/squareup/otto/Bus.java +++ b/otto/src/main/java/com/squareup/otto/Bus.java @@ -187,7 +187,33 @@ public void register(Object object) { } enforcer.enforce(this); - Map, EventProducer> foundProducers = handlerFinder.findAllProducers(object); + register(handlerFinder.findAllProducers(object), handlerFinder.findAllSubscribers(object)); + } + + /** + * Registers all handler methods on {@code object} to receive events and producer methods to provide events. + *

+ * If any subscribers are registering for types which already have a producer they will be called immediately + * with the result of calling that producer. + *

+ * If any producers are registering for types which already have subscribers, each subscriber will be called with + * the value from the result of calling the producer. + * + * @param object object whose handler methods should be registered. + * @throws NullPointerException if the object is null. + */ + public void register(Object object, Class listenerClass) { + if (object == null) { + throw new NullPointerException("Object to register must not be null."); + } + enforcer.enforce(this); + + register(handlerFinder.findAllProducers(object, listenerClass), + handlerFinder.findAllSubscribers(object, listenerClass)); + } + + private void register(Map, EventProducer> foundProducers, + Map, Set> foundHandlersMap) { for (Class type : foundProducers.keySet()) { final EventProducer producer = foundProducers.get(type); @@ -206,7 +232,6 @@ public void register(Object object) { } } - Map, Set> foundHandlersMap = handlerFinder.findAllSubscribers(object); for (Class type : foundHandlersMap.keySet()) { Set handlers = handlersByType.get(type); if (handlers == null) { @@ -266,7 +291,28 @@ public void unregister(Object object) { } enforcer.enforce(this); - Map, EventProducer> producersInListener = handlerFinder.findAllProducers(object); + unregister(object, handlerFinder.findAllProducers(object), handlerFinder.findAllSubscribers(object)); + } + + /** + * Unregisters all producer and handler methods on a registered {@code object}. + * + * @param object object whose producer and handler methods should be unregistered. + * @throws IllegalArgumentException if the object was not previously registered. + * @throws NullPointerException if the object is null. + */ + public void unregister(Object object, Class listenerClass) { + if (object == null) { + throw new NullPointerException("Object to unregister must not be null."); + } + enforcer.enforce(this); + + unregister(object, handlerFinder.findAllProducers(object, listenerClass), + handlerFinder.findAllSubscribers(object, listenerClass)); + } + + private void unregister(Object object, Map, EventProducer> producersInListener, + Map, Set> handlersInListener) { for (Map.Entry, EventProducer> entry : producersInListener.entrySet()) { final Class key = entry.getKey(); EventProducer producer = getProducerForEventType(key); @@ -280,7 +326,6 @@ public void unregister(Object object) { producersByType.remove(key).invalidate(); } - Map, Set> handlersInListener = handlerFinder.findAllSubscribers(object); for (Map.Entry, Set> entry : handlersInListener.entrySet()) { Set currentHandlers = getHandlersForEventType(entry.getKey()); Collection eventMethodsInListener = entry.getValue(); diff --git a/otto/src/main/java/com/squareup/otto/HandlerFinder.java b/otto/src/main/java/com/squareup/otto/HandlerFinder.java index ff61a2c..e222870 100644 --- a/otto/src/main/java/com/squareup/otto/HandlerFinder.java +++ b/otto/src/main/java/com/squareup/otto/HandlerFinder.java @@ -24,18 +24,32 @@ interface HandlerFinder { Map, EventProducer> findAllProducers(Object listener); + Map, EventProducer> findAllProducers(Object listener, Class targetClass); + Map, Set> findAllSubscribers(Object listener); + Map, Set> findAllSubscribers(Object listener, Class targetClass); HandlerFinder ANNOTATED = new HandlerFinder() { + @Override public Map, EventProducer> findAllProducers(Object listener) { return AnnotatedHandlerFinder.findAllProducers(listener); } + @Override + public Map, EventProducer> findAllProducers(Object listener, Class targetClass) { + return AnnotatedHandlerFinder.findAllProducers(listener, targetClass); + } + @Override public Map, Set> findAllSubscribers(Object listener) { return AnnotatedHandlerFinder.findAllSubscribers(listener); } + + @Override + public Map, Set> findAllSubscribers(Object listener, Class targetClass) { + return AnnotatedHandlerFinder.findAllSubscribers(listener, targetClass); + } }; } From 5a53fba93949c368a64ae59a81d3fb0f4caae5c8 Mon Sep 17 00:00:00 2001 From: "Belk, Thomas W" Date: Wed, 20 Jan 2016 15:36:45 -0500 Subject: [PATCH 2/8] Removing unnecessary methods --- .../squareup/otto/AnnotatedHandlerFinder.java | 72 +------------------ otto/src/main/java/com/squareup/otto/Bus.java | 14 +--- .../java/com/squareup/otto/HandlerFinder.java | 14 ---- 3 files changed, 4 insertions(+), 96 deletions(-) diff --git a/otto/src/main/java/com/squareup/otto/AnnotatedHandlerFinder.java b/otto/src/main/java/com/squareup/otto/AnnotatedHandlerFinder.java index 3067937..520cab9 100644 --- a/otto/src/main/java/com/squareup/otto/AnnotatedHandlerFinder.java +++ b/otto/src/main/java/com/squareup/otto/AnnotatedHandlerFinder.java @@ -49,24 +49,12 @@ private static void loadAnnotatedProducerMethods(Class listenerClass, loadAnnotatedMethods(listenerClass, producerMethods, subscriberMethods); } - private static void loadAnnotatedProducerMethods(Class fakeListenerClass, - Class listenerClass, Map, Method> producerMethods) { - Map, Set> subscriberMethods = new HashMap, Set>(); - loadAnnotatedMethods(fakeListenerClass, listenerClass, producerMethods, subscriberMethods); - } - private static void loadAnnotatedSubscriberMethods(Class listenerClass, Map, Set> subscriberMethods) { Map, Method> producerMethods = new HashMap, Method>(); loadAnnotatedMethods(listenerClass, producerMethods, subscriberMethods); } - private static void loadAnnotatedSubscriberMethods(Class fakeListenerClass, - Class listenerClass, Map, Set> subscriberMethods) { - Map, Method> producerMethods = new HashMap, Method>(); - loadAnnotatedMethods(fakeListenerClass, listenerClass, producerMethods, subscriberMethods); - } - /** * Load all methods annotated with {@link Produce} or {@link Subscribe} into their respective caches for the * specified class. @@ -79,18 +67,6 @@ private static void loadAnnotatedMethods(Class listenerClass, SUBSCRIBERS_CACHE.put(listenerClass, subscriberMethods); } - /** - * Load all methods annotated with {@link Produce} or {@link Subscribe} into their respective caches for the - * specified class. - */ - private static void loadAnnotatedMethods(Class fakeListenerClass, Class listenerClass, - Map, Method> producerMethods, Map, Set> subscriberMethods) { - loadAnnotatedMethodsForListenerClass(listenerClass, producerMethods, subscriberMethods); - - PRODUCERS_CACHE.put(fakeListenerClass, producerMethods); - SUBSCRIBERS_CACHE.put(fakeListenerClass, subscriberMethods); - } - /** * Load all methods annotated with {@link Produce} or {@link Subscribe} into their respective caches for the * specified class. @@ -163,8 +139,7 @@ private static void loadAnnotatedMethodsForListenerClass(Class listenerClass, } /** This implementation finds all methods marked with a {@link Produce} annotation. */ - static Map, EventProducer> findAllProducers(Object listener) { - final Class listenerClass = listener.getClass(); + static Map, EventProducer> findAllProducers(Object listener, Class listenerClass) { Map, EventProducer> handlersInMethod = new HashMap, EventProducer>(); Map, Method> methods = PRODUCERS_CACHE.get(listenerClass); @@ -182,29 +157,8 @@ static Map, EventProducer> findAllProducers(Object listener) { return handlersInMethod; } - /** This implementation finds all methods marked with a {@link Produce} annotation. */ - static Map, EventProducer> findAllProducers(Object listener, Class listenerClass) { - Class fakeListenerClass = listener.getClass(); - Map, EventProducer> handlersInMethod = new HashMap, EventProducer>(); - - Map, Method> methods = PRODUCERS_CACHE.get(fakeListenerClass); - if (null == methods) { - methods = new HashMap, Method>(); - loadAnnotatedProducerMethods(fakeListenerClass, listenerClass, methods); - } - if (!methods.isEmpty()) { - for (Map.Entry, Method> e : methods.entrySet()) { - EventProducer producer = new EventProducer(listener, e.getValue()); - handlersInMethod.put(e.getKey(), producer); - } - } - - return handlersInMethod; - } - /** This implementation finds all methods marked with a {@link Subscribe} annotation. */ - static Map, Set> findAllSubscribers(Object listener) { - Class listenerClass = listener.getClass(); + static Map, Set> findAllSubscribers(Object listener, Class listenerClass) { Map, Set> handlersInMethod = new HashMap, Set>(); Map, Set> methods = SUBSCRIBERS_CACHE.get(listenerClass); @@ -225,28 +179,6 @@ static Map, Set> findAllSubscribers(Object listener) { return handlersInMethod; } - static Map, Set> findAllSubscribers(Object listener, Class listenerClass) { - Class fakeListenerClass = listener.getClass(); - Map, Set> handlersInMethod = new HashMap, Set>(); - - Map, Set> methods = SUBSCRIBERS_CACHE.get(fakeListenerClass); - if (null == methods) { - methods = new HashMap, Set>(); - loadAnnotatedSubscriberMethods(fakeListenerClass, listenerClass, methods); - } - if (!methods.isEmpty()) { - for (Map.Entry, Set> e : methods.entrySet()) { - Set handlers = new HashSet(); - for (Method m : e.getValue()) { - handlers.add(new EventHandler(listener, m)); - } - handlersInMethod.put(e.getKey(), handlers); - } - } - - return handlersInMethod; - } - private AnnotatedHandlerFinder() { // No instances. } diff --git a/otto/src/main/java/com/squareup/otto/Bus.java b/otto/src/main/java/com/squareup/otto/Bus.java index f891f1f..9a39a00 100644 --- a/otto/src/main/java/com/squareup/otto/Bus.java +++ b/otto/src/main/java/com/squareup/otto/Bus.java @@ -182,12 +182,7 @@ public Bus(ThreadEnforcer enforcer, String identifier) { * @throws NullPointerException if the object is null. */ public void register(Object object) { - if (object == null) { - throw new NullPointerException("Object to register must not be null."); - } - enforcer.enforce(this); - - register(handlerFinder.findAllProducers(object), handlerFinder.findAllSubscribers(object)); + register(object, object.getClass()); } /** @@ -286,12 +281,7 @@ private void dispatchProducerResultToHandler(EventHandler handler, EventProducer * @throws NullPointerException if the object is null. */ public void unregister(Object object) { - if (object == null) { - throw new NullPointerException("Object to unregister must not be null."); - } - enforcer.enforce(this); - - unregister(object, handlerFinder.findAllProducers(object), handlerFinder.findAllSubscribers(object)); + unregister(object, object.getClass()); } /** diff --git a/otto/src/main/java/com/squareup/otto/HandlerFinder.java b/otto/src/main/java/com/squareup/otto/HandlerFinder.java index e222870..956ff0a 100644 --- a/otto/src/main/java/com/squareup/otto/HandlerFinder.java +++ b/otto/src/main/java/com/squareup/otto/HandlerFinder.java @@ -22,31 +22,17 @@ /** Finds producer and subscriber methods. */ interface HandlerFinder { - Map, EventProducer> findAllProducers(Object listener); - Map, EventProducer> findAllProducers(Object listener, Class targetClass); - Map, Set> findAllSubscribers(Object listener); - Map, Set> findAllSubscribers(Object listener, Class targetClass); HandlerFinder ANNOTATED = new HandlerFinder() { - @Override - public Map, EventProducer> findAllProducers(Object listener) { - return AnnotatedHandlerFinder.findAllProducers(listener); - } - @Override public Map, EventProducer> findAllProducers(Object listener, Class targetClass) { return AnnotatedHandlerFinder.findAllProducers(listener, targetClass); } - @Override - public Map, Set> findAllSubscribers(Object listener) { - return AnnotatedHandlerFinder.findAllSubscribers(listener); - } - @Override public Map, Set> findAllSubscribers(Object listener, Class targetClass) { return AnnotatedHandlerFinder.findAllSubscribers(listener, targetClass); From 135299bef4775740a4a463511bd1ded6796709ba Mon Sep 17 00:00:00 2001 From: "Belk, Thomas W" Date: Wed, 20 Jan 2016 15:45:59 -0500 Subject: [PATCH 3/8] Removing unnecessary methods --- .../squareup/otto/AnnotatedHandlerFinder.java | 15 +++------------ otto/src/main/java/com/squareup/otto/Bus.java | 16 ++++------------ 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/otto/src/main/java/com/squareup/otto/AnnotatedHandlerFinder.java b/otto/src/main/java/com/squareup/otto/AnnotatedHandlerFinder.java index 520cab9..386ce29 100644 --- a/otto/src/main/java/com/squareup/otto/AnnotatedHandlerFinder.java +++ b/otto/src/main/java/com/squareup/otto/AnnotatedHandlerFinder.java @@ -61,18 +61,6 @@ private static void loadAnnotatedSubscriberMethods(Class listenerClass, */ private static void loadAnnotatedMethods(Class listenerClass, Map, Method> producerMethods, Map, Set> subscriberMethods) { - loadAnnotatedMethodsForListenerClass(listenerClass, producerMethods, subscriberMethods); - - PRODUCERS_CACHE.put(listenerClass, producerMethods); - SUBSCRIBERS_CACHE.put(listenerClass, subscriberMethods); - } - - /** - * Load all methods annotated with {@link Produce} or {@link Subscribe} into their respective caches for the - * specified class. - */ - private static void loadAnnotatedMethodsForListenerClass(Class listenerClass, Map, Method> producerMethods, - Map, Set> subscriberMethods) { for (Method method : listenerClass.getDeclaredMethods()) { // The compiler sometimes creates synthetic bridge methods as part of the // type erasure process. As of JDK8 these methods now include the same @@ -136,6 +124,9 @@ private static void loadAnnotatedMethodsForListenerClass(Class listenerClass, producerMethods.put(eventType, method); } } + + PRODUCERS_CACHE.put(listenerClass, producerMethods); + SUBSCRIBERS_CACHE.put(listenerClass, subscriberMethods); } /** This implementation finds all methods marked with a {@link Produce} annotation. */ diff --git a/otto/src/main/java/com/squareup/otto/Bus.java b/otto/src/main/java/com/squareup/otto/Bus.java index 9a39a00..67b70f7 100644 --- a/otto/src/main/java/com/squareup/otto/Bus.java +++ b/otto/src/main/java/com/squareup/otto/Bus.java @@ -203,12 +203,7 @@ public void register(Object object, Class listenerClass) { } enforcer.enforce(this); - register(handlerFinder.findAllProducers(object, listenerClass), - handlerFinder.findAllSubscribers(object, listenerClass)); - } - - private void register(Map, EventProducer> foundProducers, - Map, Set> foundHandlersMap) { + Map, EventProducer> foundProducers = handlerFinder.findAllProducers(object, listenerClass); for (Class type : foundProducers.keySet()) { final EventProducer producer = foundProducers.get(type); @@ -227,6 +222,7 @@ private void register(Map, EventProducer> foundProducers, } } + Map, Set> foundHandlersMap = handlerFinder.findAllSubscribers(object, listenerClass); for (Class type : foundHandlersMap.keySet()) { Set handlers = handlersByType.get(type); if (handlers == null) { @@ -297,12 +293,7 @@ public void unregister(Object object, Class listenerClass) { } enforcer.enforce(this); - unregister(object, handlerFinder.findAllProducers(object, listenerClass), - handlerFinder.findAllSubscribers(object, listenerClass)); - } - - private void unregister(Object object, Map, EventProducer> producersInListener, - Map, Set> handlersInListener) { + Map, EventProducer> producersInListener = handlerFinder.findAllProducers(object, listenerClass); for (Map.Entry, EventProducer> entry : producersInListener.entrySet()) { final Class key = entry.getKey(); EventProducer producer = getProducerForEventType(key); @@ -316,6 +307,7 @@ private void unregister(Object object, Map, EventProducer> producersInL producersByType.remove(key).invalidate(); } + Map, Set> handlersInListener = handlerFinder.findAllSubscribers(object, listenerClass); for (Map.Entry, Set> entry : handlersInListener.entrySet()) { Set currentHandlers = getHandlersForEventType(entry.getKey()); Collection eventMethodsInListener = entry.getValue(); From 32d209ff75b625e06a864e6ce5a905e58f14ae25 Mon Sep 17 00:00:00 2001 From: "Belk, Thomas W" Date: Wed, 20 Jan 2016 16:28:56 -0500 Subject: [PATCH 4/8] updating javadocs, adding throws for proper handling of exceptions --- otto/src/main/java/com/squareup/otto/Bus.java | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/otto/src/main/java/com/squareup/otto/Bus.java b/otto/src/main/java/com/squareup/otto/Bus.java index 67b70f7..856c711 100644 --- a/otto/src/main/java/com/squareup/otto/Bus.java +++ b/otto/src/main/java/com/squareup/otto/Bus.java @@ -178,10 +178,13 @@ public Bus(ThreadEnforcer enforcer, String identifier) { * If any producers are registering for types which already have subscribers, each subscriber will be called with * the value from the result of calling the producer. * - * @param object object whose handler methods should be registered. + * @param object object whose producer and handler methods should be registered. * @throws NullPointerException if the object is null. */ public void register(Object object) { + if (object == null) { + throw new NullPointerException("Object to register must not be null."); + } register(object, object.getClass()); } @@ -194,12 +197,18 @@ public void register(Object object) { * If any producers are registering for types which already have subscribers, each subscriber will be called with * the value from the result of calling the producer. * - * @param object object whose handler methods should be registered. + * @param object object used to trigger registered producer and handler methods. {@code object} must either be an + * instanceof {@code listenerClass} or {@code listenerClass} must be assignable from the class of + * {@code object}; ie a parent class of {@code object}. + * @param listenerClass class whose producer and handler methods should be registered. * @throws NullPointerException if the object is null. */ public void register(Object object, Class listenerClass) { if (object == null) { - throw new NullPointerException("Object to register must not be null."); + throw new NullPointerException("Object to trigger register methods not be null."); + } + if (listenerClass == null) { + throw new NullPointerException("Class to register must not be null."); } enforcer.enforce(this); @@ -277,19 +286,28 @@ private void dispatchProducerResultToHandler(EventHandler handler, EventProducer * @throws NullPointerException if the object is null. */ public void unregister(Object object) { + if (object == null) { + throw new NullPointerException("Object to unregister must not be null."); + } unregister(object, object.getClass()); } /** * Unregisters all producer and handler methods on a registered {@code object}. * - * @param object object whose producer and handler methods should be unregistered. + * @param object object used to trigger registered producer and handler methods. {@code object} must either be an + * instanceof {@code listenerClass} or {@code listenerClass} must be assignable from the class of + * {@code object}; ie a parent class of {@code object}. + * @param listenerClass class whose producer and handler methods should be unregistered. * @throws IllegalArgumentException if the object was not previously registered. * @throws NullPointerException if the object is null. */ public void unregister(Object object, Class listenerClass) { if (object == null) { - throw new NullPointerException("Object to unregister must not be null."); + throw new NullPointerException("Object to trigger register methods not be null."); + } + if (listenerClass == null) { + throw new NullPointerException("Class to unregister must not be null."); } enforcer.enforce(this); @@ -301,7 +319,7 @@ public void unregister(Object object, Class listenerClass) { if (value == null || !value.equals(producer)) { throw new IllegalArgumentException( - "Missing event producer for an annotated method. Is " + object.getClass() + "Missing event producer for an annotated method. Is " + listenerClass + " registered?"); } producersByType.remove(key).invalidate(); @@ -314,7 +332,7 @@ public void unregister(Object object, Class listenerClass) { if (currentHandlers == null || !currentHandlers.containsAll(eventMethodsInListener)) { throw new IllegalArgumentException( - "Missing event handler for an annotated method. Is " + object.getClass() + "Missing event handler for an annotated method. Is " + listenerClass + " registered?"); } From 8aeac3756aab144ffcd829ddcf3461dc2fce9b0c Mon Sep 17 00:00:00 2001 From: "Belk, Thomas W" Date: Wed, 20 Jan 2016 16:32:55 -0500 Subject: [PATCH 5/8] updating throws javadocs --- otto/src/main/java/com/squareup/otto/Bus.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/otto/src/main/java/com/squareup/otto/Bus.java b/otto/src/main/java/com/squareup/otto/Bus.java index 856c711..05021bb 100644 --- a/otto/src/main/java/com/squareup/otto/Bus.java +++ b/otto/src/main/java/com/squareup/otto/Bus.java @@ -179,7 +179,7 @@ public Bus(ThreadEnforcer enforcer, String identifier) { * the value from the result of calling the producer. * * @param object object whose producer and handler methods should be registered. - * @throws NullPointerException if the object is null. + * @throws NullPointerException if {@code object} or {@code listenerClass} is null. */ public void register(Object object) { if (object == null) { @@ -300,7 +300,7 @@ public void unregister(Object object) { * {@code object}; ie a parent class of {@code object}. * @param listenerClass class whose producer and handler methods should be unregistered. * @throws IllegalArgumentException if the object was not previously registered. - * @throws NullPointerException if the object is null. + * @throws NullPointerException if {@code object} or {@code listenerClass} is null. */ public void unregister(Object object, Class listenerClass) { if (object == null) { From b6f9489c6502cf4fceece2a2d3ffba15a986c2a7 Mon Sep 17 00:00:00 2001 From: "Belk, Thomas W" Date: Wed, 20 Jan 2016 16:34:47 -0500 Subject: [PATCH 6/8] fixing more javadoc issues --- otto/src/main/java/com/squareup/otto/Bus.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/otto/src/main/java/com/squareup/otto/Bus.java b/otto/src/main/java/com/squareup/otto/Bus.java index 05021bb..aeaf49d 100644 --- a/otto/src/main/java/com/squareup/otto/Bus.java +++ b/otto/src/main/java/com/squareup/otto/Bus.java @@ -179,7 +179,7 @@ public Bus(ThreadEnforcer enforcer, String identifier) { * the value from the result of calling the producer. * * @param object object whose producer and handler methods should be registered. - * @throws NullPointerException if {@code object} or {@code listenerClass} is null. + * @throws NullPointerException if {@code object} is null. */ public void register(Object object) { if (object == null) { @@ -201,7 +201,7 @@ public void register(Object object) { * instanceof {@code listenerClass} or {@code listenerClass} must be assignable from the class of * {@code object}; ie a parent class of {@code object}. * @param listenerClass class whose producer and handler methods should be registered. - * @throws NullPointerException if the object is null. + * @throws NullPointerException if {@code object} or {@code listenerClass} is null. */ public void register(Object object, Class listenerClass) { if (object == null) { @@ -283,7 +283,7 @@ private void dispatchProducerResultToHandler(EventHandler handler, EventProducer * * @param object object whose producer and handler methods should be unregistered. * @throws IllegalArgumentException if the object was not previously registered. - * @throws NullPointerException if the object is null. + * @throws NullPointerException if {@code object} is null. */ public void unregister(Object object) { if (object == null) { From 2c815b60932cb2e24afa21c5ab581bbd14e18d17 Mon Sep 17 00:00:00 2001 From: "Belk, Thomas W" Date: Wed, 20 Jan 2016 17:11:59 -0500 Subject: [PATCH 7/8] fixing test failures --- .../java/com/squareup/otto/UnregisteringHandlerTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/otto/src/test/java/com/squareup/otto/UnregisteringHandlerTest.java b/otto/src/test/java/com/squareup/otto/UnregisteringHandlerTest.java index 50b9990..694470e 100644 --- a/otto/src/test/java/com/squareup/otto/UnregisteringHandlerTest.java +++ b/otto/src/test/java/com/squareup/otto/UnregisteringHandlerTest.java @@ -98,13 +98,13 @@ public int compare(EventHandler eventHandler, EventHandler eventHandler1) { }; @Override - public Map, EventProducer> findAllProducers(Object listener) { - return HandlerFinder.ANNOTATED.findAllProducers(listener); + public Map, EventProducer> findAllProducers(Object listener, Class targetClass) { + return HandlerFinder.ANNOTATED.findAllProducers(listener, targetClass); } @Override - public Map, Set> findAllSubscribers(Object listener) { - Map, Set> found = HandlerFinder.ANNOTATED.findAllSubscribers(listener); + public Map, Set> findAllSubscribers(Object listener, Class targetClass) { + Map, Set> found = HandlerFinder.ANNOTATED.findAllSubscribers(listener, targetClass); Map, Set> sorted = new HashMap, Set>(); for (Map.Entry, Set> entry : found.entrySet()) { SortedSet handlers = new TreeSet(handlerComparator); From 9e63e720269beb99bcf5100e49cbae8acbedcbd9 Mon Sep 17 00:00:00 2001 From: "Belk, Thomas W" Date: Thu, 21 Jan 2016 07:46:14 -0500 Subject: [PATCH 8/8] updating javadocs and adding a new throwable case --- otto/src/main/java/com/squareup/otto/Bus.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/otto/src/main/java/com/squareup/otto/Bus.java b/otto/src/main/java/com/squareup/otto/Bus.java index aeaf49d..3ac83c6 100644 --- a/otto/src/main/java/com/squareup/otto/Bus.java +++ b/otto/src/main/java/com/squareup/otto/Bus.java @@ -197,9 +197,8 @@ public void register(Object object) { * If any producers are registering for types which already have subscribers, each subscriber will be called with * the value from the result of calling the producer. * - * @param object object used to trigger registered producer and handler methods. {@code object} must either be an - * instanceof {@code listenerClass} or {@code listenerClass} must be assignable from the class of - * {@code object}; ie a parent class of {@code object}. + * @param object object used to trigger registered producer and handler methods. The {@code object}s class must be + * assignable to objects of the type {@code listenerClass}. * @param listenerClass class whose producer and handler methods should be registered. * @throws NullPointerException if {@code object} or {@code listenerClass} is null. */ @@ -210,6 +209,9 @@ public void register(Object object, Class listenerClass) { if (listenerClass == null) { throw new NullPointerException("Class to register must not be null."); } + if (!listenerClass.isAssignableFrom(object.getClass())) { + throw new IllegalArgumentException("Class to unregister must be an instance of or assignable from Object class."); + } enforcer.enforce(this); Map, EventProducer> foundProducers = handlerFinder.findAllProducers(object, listenerClass); @@ -295,9 +297,8 @@ public void unregister(Object object) { /** * Unregisters all producer and handler methods on a registered {@code object}. * - * @param object object used to trigger registered producer and handler methods. {@code object} must either be an - * instanceof {@code listenerClass} or {@code listenerClass} must be assignable from the class of - * {@code object}; ie a parent class of {@code object}. + * @param object object used to trigger registered producer and handler methods. The {@code object}s class must be + * assignable to objects of the type {@code listenerClass}. * @param listenerClass class whose producer and handler methods should be unregistered. * @throws IllegalArgumentException if the object was not previously registered. * @throws NullPointerException if {@code object} or {@code listenerClass} is null. @@ -309,6 +310,9 @@ public void unregister(Object object, Class listenerClass) { if (listenerClass == null) { throw new NullPointerException("Class to unregister must not be null."); } + if (!listenerClass.isAssignableFrom(object.getClass())) { + throw new IllegalArgumentException("Class to unregister must be an instance of or assignable from Object class."); + } enforcer.enforce(this); Map, EventProducer> producersInListener = handlerFinder.findAllProducers(object, listenerClass);