Skip to content

Commit cb6fbe8

Browse files
mrsimplemrsimple
authored andcommitted
fixed not get super methods bug
1 parent 77ab854 commit cb6fbe8

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2015 Umeng, Inc
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.simple.eventbus.demo.fragment;
26+
27+
import android.support.v4.app.Fragment;
28+
import android.util.Log;
29+
30+
import org.simple.eventbus.Subcriber;
31+
import org.simple.eventbus.demo.bean.User;
32+
33+
/**
34+
* @author mrsimple
35+
*/
36+
public class BaseFragment extends Fragment {
37+
38+
@Subcriber
39+
private void protectedMethodInSuper(User user) {
40+
Log.e(getTag(), "### supper protectedMethodInSuper invoked ( default tag )");
41+
}
42+
43+
@Subcriber
44+
private void privateMethodInSuper(User user) {
45+
Log.e(getTag(), "### supper privateMethodInSuper invoked ( default tag ) ");
46+
}
47+
}

Simple_eventbus_demo/src/org/simple/eventbus/demo/fragment/ConstactFragment.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
package org.simple.eventbus.demo.fragment;
1818

1919
import android.os.Bundle;
20-
import android.support.v4.app.Fragment;
2120
import android.view.LayoutInflater;
2221
import android.view.View;
2322
import android.view.ViewGroup;
24-
import android.widget.AdapterView.OnItemClickListener;
2523
import android.widget.AdapterView;
24+
import android.widget.AdapterView.OnItemClickListener;
2625
import android.widget.ArrayAdapter;
2726
import android.widget.BaseAdapter;
2827
import android.widget.ListView;
@@ -40,7 +39,7 @@
4039
/**
4140
* @author mrsimple
4241
*/
43-
public class ConstactFragment extends Fragment {
42+
public class ConstactFragment extends BaseFragment {
4443

4544
BaseAdapter mAdapter;
4645
List<User> mConstacts = new LinkedList<User>();

Simple_eventbus_demo/src/org/simple/eventbus/demo/fragment/MenuFragment.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.simple.eventbus.demo.fragment;
1818

1919
import android.os.Bundle;
20-
import android.support.v4.app.Fragment;
2120
import android.util.Log;
2221
import android.view.LayoutInflater;
2322
import android.view.View;
@@ -36,7 +35,7 @@
3635
/**
3736
* @author mrsimple
3837
*/
39-
public class MenuFragment extends Fragment {
38+
public class MenuFragment extends BaseFragment {
4039

4140
public static final String CLICK_TAG = "click_user";
4241
/**

src/org/simple/eventbus/EventBus.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,38 @@ public void register(Object subscriber) {
127127
if (subscriber == null) {
128128
return;
129129
}
130-
final Method[] allMethods = subscriber.getClass().getDeclaredMethods();
130+
// final Method[] allMethods =
131+
// subscriber.getClass().getDeclaredMethods();
132+
// for (int i = 0; i < allMethods.length; i++) {
133+
// Method method = allMethods[i];
134+
// // 根据注解来解析函数
135+
// Subcriber annotation = method.getAnnotation(Subcriber.class);
136+
// if (annotation != null) {
137+
// // 获取方法参数
138+
// Class<?>[] paramsTypeClass = method.getParameterTypes();
139+
// // just only one param
140+
// if (paramsTypeClass != null && paramsTypeClass.length == 1) {
141+
// EventType event = new EventType(paramsTypeClass[0],
142+
// annotation.tag());
143+
// TargetMethod subscribeMethod = new TargetMethod(method,
144+
// paramsTypeClass[0], annotation.mode());
145+
// // 订阅事件
146+
// subscibe(event, subscribeMethod, subscriber);
147+
// }
148+
// }
149+
// } // end for
150+
151+
Class<?> clazz = subscriber.getClass();
152+
while (!isObjectClass(clazz)) {
153+
// 查找使用注解标注了的目标函数
154+
findSubcribeMethods(clazz, subscriber);
155+
// 查找完subscriber中复合要求的方法再查找父类中的方法,直至最顶层的object类
156+
clazz = clazz.getSuperclass();
157+
}
158+
}
159+
160+
private void findSubcribeMethods(Class<?> clazz, Object subscriber) {
161+
final Method[] allMethods = clazz.getDeclaredMethods();
131162
for (int i = 0; i < allMethods.length; i++) {
132163
Method method = allMethods[i];
133164
// 根据注解来解析函数
@@ -147,6 +178,10 @@ public void register(Object subscriber) {
147178
} // end for
148179
}
149180

181+
private boolean isObjectClass(Class<?> clazz) {
182+
return clazz.getName().equals(Object.class.getName());
183+
}
184+
150185
/**
151186
* @param event
152187
* @param method

0 commit comments

Comments
 (0)