-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathSubsciberMethodHunter.java
More file actions
179 lines (162 loc) · 6.85 KB
/
SubsciberMethodHunter.java
File metadata and controls
179 lines (162 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2015 Umeng, Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.simple.eventbus;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* the subscriber method hunter, find all of the subscriber's methods which
* annotated with @Subcriber.
*
* @author mrsimple
*/
public class SubsciberMethodHunter {
/**
* the event bus's subscriber's map
*/
Map<EventType, CopyOnWriteArrayList<Subscription>> mSubcriberMap;
/**
* @param subscriberMap
*/
public SubsciberMethodHunter(Map<EventType, CopyOnWriteArrayList<Subscription>> subscriberMap) {
mSubcriberMap = subscriberMap;
}
/**
* 查找订阅对象中的所有订阅函数,订阅函数的参数只能有一个.找到订阅函数之后构建Subscription存储到Map中
*
* @param subscriber 订阅对象
* @return
*/
public void findSubcribeMethods(Object subscriber) {
if (mSubcriberMap == null) {
throw new NullPointerException("the mSubcriberMap is null. ");
}
Class<?> clazz = subscriber.getClass();
// 查找类中符合要求的注册方法,直到Object类
while (clazz != null && !isSystemCalss(clazz.getName())) {
final Method[] allMethods = clazz.getDeclaredMethods();
for (int i = 0; i < allMethods.length; i++) {
Method method = allMethods[i];
// 根据注解来解析函数
Subscriber annotation = method.getAnnotation(Subscriber.class);
if (annotation != null) {
// 获取方法参数
Class<?>[] paramsTypeClass = method.getParameterTypes();
// 订阅函数只支持一个参数
if (paramsTypeClass != null && paramsTypeClass.length <= 1) {
Class<?> paramType = (paramsTypeClass.length == 0 ? null : convertType(paramsTypeClass[0]));
EventType eventType = new EventType(paramType, annotation.tag());
TargetMethod subscribeMethod = new TargetMethod(method, eventType,
annotation.mode());
subscibe(eventType, subscribeMethod, subscriber);
}
}
} // end for
// 获取父类,以继续查找父类中符合要求的方法
clazz = clazz.getSuperclass();
}
}
/**
* 按照EventType存储订阅者列表,这里的EventType就是事件类型,一个事件对应0到多个订阅者.
*
* @param event 事件
* @param method 订阅方法对象
* @param subscriber 订阅者
*/
private void subscibe(EventType event, TargetMethod method, Object subscriber) {
CopyOnWriteArrayList<Subscription> subscriptionLists = mSubcriberMap.get(event);
if (subscriptionLists == null) {
subscriptionLists = new CopyOnWriteArrayList<Subscription>();
}
Subscription newSubscription = new Subscription(subscriber, method);
if (subscriptionLists.contains(newSubscription)) {
return;
}
subscriptionLists.add(newSubscription);
// 将事件类型key和订阅者信息存储到map中
mSubcriberMap.put(event, subscriptionLists);
}
/**
* remove subscriber methods from map
*
* @param subscriber
*/
public void removeMethodsFromMap(Object subscriber) {
Iterator<CopyOnWriteArrayList<Subscription>> iterator = mSubcriberMap
.values().iterator();
while (iterator.hasNext()) {
CopyOnWriteArrayList<Subscription> subscriptions = iterator.next();
if (subscriptions != null) {
List<Subscription> foundSubscriptions = new
LinkedList<Subscription>();
Iterator<Subscription> subIterator = subscriptions.iterator();
while (subIterator.hasNext()) {
Subscription subscription = subIterator.next();
// 获取引用
Object cacheObject = subscription.subscriber.get();
if (isObjectsEqual(cacheObject, subscriber)
|| cacheObject == null) {
foundSubscriptions.add(subscription);
}
}
// 移除该subscriber的相关的Subscription
subscriptions.removeAll(foundSubscriptions);
}
// 如果针对某个Event的订阅者数量为空了,那么需要从map中清除
if (subscriptions == null || subscriptions.size() == 0) {
iterator.remove();
}
}
}
private boolean isObjectsEqual(Object cachedObj, Object subscriber) {
return cachedObj != null
&& cachedObj.equals(subscriber);
}
/**
* if the subscriber method's type is primitive, convert it to corresponding
* Object type. for example, int to Integer.
*
* @param eventType origin Event Type
* @return
*/
private Class<?> convertType(Class<?> eventType) {
Class<?> returnClass = eventType;
if (eventType.equals(boolean.class)) {
returnClass = Boolean.class;
} else if (eventType.equals(int.class)) {
returnClass = Integer.class;
} else if (eventType.equals(float.class)) {
returnClass = Float.class;
} else if (eventType.equals(double.class)) {
returnClass = Double.class;
}
return returnClass;
}
private boolean isSystemCalss(String name) {
return name.startsWith("java.") || name.startsWith("javax.") || name.startsWith("android.");
}
}