Home > Java > javaTutorial > Detailed explanation of guava eventbus example code

Detailed explanation of guava eventbus example code

PHP中文网
Release: 2017-06-21 09:25:33
Original
2025 people have browsed it

Before analyzing guava eventbus, let’s take a look at how the traditional observer pattern is written:

The Subject interface is an abstract topic, equivalent to the observed, it holds a listener The list of observers, the attach method registers the listener in this list, the detach method cancels the listener, and the notify method is used to notify the listeners in the list when an event occurs

is usually called in the notify implementation method The listener's update method.

Observer is an abstract observer with an update method. The update method is called by the notify method of the specific theme.

This is a traditional programming method for interfaces. The difference is that eventbus uses ""implicit interface", a programming method based on java Annotation.

The difference is: the corresponding relationship of this "implicit interface" is generated when the program is running , and based on the true meaning of the corresponding relationship between the interface and the implementation is established at compile time, in contrast, the "implicit interface" is more flexible

Let's analyze how the implicit interface and implementation establish binding For a definite relationship, look at the code:

 1 ##SubscriberRegistry类的register方法 2 void register(Object listener) { 3     Multimap<Class<?>, Subscriber> listenerMethods = findAllSubscribers(listener); 4  5     for (Map.Entry<Class<?>, Collection<Subscriber>> entry : listenerMethods.asMap().entrySet()) { 6       Class<?> eventType = entry.getKey(); 7       Collection<Subscriber> eventMethodsInListener = entry.getValue(); 8  9       CopyOnWriteArraySet<Subscriber> eventSubscribers = subscribers.get(eventType);10 11       if (eventSubscribers == null) {12         CopyOnWriteArraySet<Subscriber> newSet = new CopyOnWriteArraySet<Subscriber>();13         eventSubscribers = MoreObjects.firstNonNull(14             subscribers.putIfAbsent(eventType, newSet), newSet);15       }16 17       eventSubscribers.addAll(eventMethodsInListener);18     }19   }
Copy after login

The useful part of this method is line 3. A rough analysis of the rest of the code is to facilitate this Mutimap, which combines listeners of the same type of events. Subsriber is added to the corresponding set. If the set of Subsriber for the current type of event is empty, then add an empty set first.

Follow the method in line 3 above:

 1 /** 2    * Returns all subscribers for the given listener grouped by the type of event they subscribe to. 3    */ 4   private Multimap<Class<?>, Subscriber> findAllSubscribers(Object listener) { 5     Multimap<Class<?>, Subscriber> methodsInListener = HashMultimap.create(); 6     Class<?> clazz = listener.getClass(); 7     for (Method method : getAnnotatedMethods(clazz)) { 8       Class<?>[] parameterTypes = method.getParameterTypes(); 9       Class<?> eventType = parameterTypes[0];10       methodsInListener.put(eventType, Subscriber.create(bus, listener, method));11     }12     return methodsInListener;13   }
Copy after login

The core method in this method is line 7, which obtains all methods with Subscribe annotation of a specific class. The rest of the code means to get these methods, put them into a multi-value map, and then return ##.

#Follow up the method in line 7 above:

1 private static ImmutableList<Method> getAnnotatedMethods(Class<?> clazz) {2     return subscriberMethodsCache.getUnchecked(clazz);3   }
Copy after login
After following this, eclipse has no choice but to follow. It is suspected that the internal anonymous class has called a certain method ( Eclipse also displays incomplete call links for internal anonymous classes)

Let’s take a look at this

subscriberMethodsCache
Copy after login
1 private static final LoadingCache<Class<?>, ImmutableList<Method>> subscriberMethodsCache =2       CacheBuilder.newBuilder()3           .weakKeys()4           .build(new CacheLoader<Class<?>, ImmutableList<Method>>() {5             @Override6             public ImmutableList<Method> load(Class<?> concreteClass) throws Exception {7               return getAnnotatedMethodsNotCached(concreteClass);8             }9           });
Copy after login
When the load method is called, the getAnnotatedMethodsNOtCached of the current class is called. Method, follow this method:

 1 private static ImmutableList<Method> getAnnotatedMethodsNotCached(Class<?> clazz) { 2     Set<? extends Class<?>> supertypes = TypeToken.of(clazz).getTypes().rawTypes(); 3     Map<MethodIdentifier, Method> identifiers = Maps.newHashMap(); 4     for (Class<?> supertype : supertypes) { 5       for (Method method : supertype.getDeclaredMethods()) { 6         if (method.isAnnotationPresent(Subscribe.class) && !method.isSynthetic()) { 7           // TODO(cgdecker): Should check for a generic parameter type and error out 8           Class<?>[] parameterTypes = method.getParameterTypes(); 9           checkArgument(parameterTypes.length == 1,10               "Method %s has @Subscribe annotation but has %s parameters."11                   + "Subscriber methods must have exactly 1 parameter.",12               method, parameterTypes.length);13 14           MethodIdentifier ident = new MethodIdentifier(method);15           if (!identifiers.containsKey(ident)) {16             identifiers.put(ident, method);17           }18         }19       }20     }21     return ImmutableList.copyOf(identifiers.values());22   }
Copy after login
The second line means to get the current class itself + all the classes of the current class’s parent class and interface, and put them in a Set

Line 4 traverses this Set

Line 5 traverses all methods of each class

Line 6 calls Method’s isAnnotationPresent method to determine that the target method has the @Subscribe annotation, and This method cannot be a "composite method"

Put the compound condition method into the map on line 16 and return it on line 21!

The above is the detailed content of Detailed explanation of guava eventbus example code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template