From 5e2bcc2907751bfbb68bdab64fde53bf88af0741 Mon Sep 17 00:00:00 2001 From: md_5 Date: Mon, 15 Apr 2013 09:15:20 +1000 Subject: [PATCH] Use custom AnnotatedHandlerFinder and pretend all events are thread safe --- .../eventbus/AnnotatedHandlerFinder.java | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 api/src/main/java/com/google/common/eventbus/AnnotatedHandlerFinder.java diff --git a/api/src/main/java/com/google/common/eventbus/AnnotatedHandlerFinder.java b/api/src/main/java/com/google/common/eventbus/AnnotatedHandlerFinder.java new file mode 100644 index 00000000..8ea01451 --- /dev/null +++ b/api/src/main/java/com/google/common/eventbus/AnnotatedHandlerFinder.java @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2007 The Guava Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.common.eventbus; + +import com.google.common.base.Throwables; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Multimap; +import com.google.common.reflect.TypeToken; +import com.google.common.util.concurrent.UncheckedExecutionException; + +import java.lang.reflect.Method; +import java.util.Set; + +/** + * A {@link HandlerFindingStrategy} for collecting all event handler methods that are marked with + * the {@link Subscribe} annotation. + * + * @author Cliff Biffle + * @author Louis Wasserman + */ +class AnnotatedHandlerFinder implements HandlerFindingStrategy { + /** + * A thread-safe cache that contains the mapping from each class to all methods in that class and + * all super-classes, that are annotated with {@code @Subscribe}. The cache is shared across all + * instances of this class; this greatly improves performance if multiple EventBus instances are + * created and objects of the same class are registered on all of them. + */ + private static final LoadingCache, ImmutableList> handlerMethodsCache = + CacheBuilder.newBuilder() + .weakKeys() + .build(new CacheLoader, ImmutableList>() { + @Override + public ImmutableList load(Class concreteClass) throws Exception { + return getAnnotatedMethodsInternal(concreteClass); + } + }); + + /** + * {@inheritDoc} + * + * This implementation finds all methods marked with a {@link Subscribe} annotation. + */ + @Override + public Multimap, EventHandler> findAllHandlers(Object listener) { + Multimap, EventHandler> methodsInListener = HashMultimap.create(); + Class clazz = listener.getClass(); + for (Method method : getAnnotatedMethods(clazz)) { + Class[] parameterTypes = method.getParameterTypes(); + Class eventType = parameterTypes[0]; + EventHandler handler = new EventHandler(listener, method); + methodsInListener.put(eventType, handler); + } + return methodsInListener; + } + + private static ImmutableList getAnnotatedMethods(Class clazz) { + try { + return handlerMethodsCache.getUnchecked(clazz); + } catch (UncheckedExecutionException e) { + throw Throwables.propagate(e.getCause()); + } + } + + private static ImmutableList getAnnotatedMethodsInternal(Class clazz) { + Set> supers = TypeToken.of(clazz).getTypes().rawTypes(); + ImmutableList.Builder result = ImmutableList.builder(); + for (Method method : clazz.getMethods()) { + /* + * Iterate over each distinct method of {@code clazz}, checking if it is annotated with + * @Subscribe by any of the superclasses or superinterfaces that declare it. + */ + for (Class c : supers) { + try { + Method m = c.getMethod(method.getName(), method.getParameterTypes()); + if (m.isAnnotationPresent(Subscribe.class)) { + Class[] parameterTypes = method.getParameterTypes(); + if (parameterTypes.length != 1) { + throw new IllegalArgumentException("Method " + method + + " has @Subscribe annotation, but requires " + parameterTypes.length + + " arguments. Event handler methods must require a single argument."); + } + Class eventType = parameterTypes[0]; + result.add(method); + break; + } + } catch (NoSuchMethodException ignored) { + // Move on. + } + } + } + return result.build(); + } +}