Skip to content

Commit bc8b569

Browse files
committed
增加EventBus Subscribe注解方法判断
1 parent 6639727 commit bc8b569

File tree

5 files changed

+72
-17
lines changed

5 files changed

+72
-17
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ dependencies {
9292
//页面事件交互-androideventbus-停止维护-按需导入
9393
// implementation 'org.simple:androideventbus:1.0.5.1'
9494
//页面事件交互-eventbus-按需导入
95-
// compileOnly 'org.greenrobot:eventbus:3.1.1'
95+
// implementation 'org.greenrobot:eventbus:3.1.1'
9696
//万能适配器--一般都需要吧
9797
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.49-androidx'
9898
//webView库 继承FastWebActivity需要,不需要内置WebView可以不implementation

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ ext{
3232
targetSdkVersion = 28
3333
supportVersion = "1.0.0"
3434
widgetVersion = "3.2.24"
35-
versionCode = 246
36-
versionName = "2.3.2"
35+
versionCode = 247
36+
versionName = "2.3.5"
3737
}

library/src/main/java/com/aries/library/fast/basis/BasisActivity.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import android.view.accessibility.AccessibilityEvent;
1111
import android.widget.FrameLayout;
1212

13-
import androidx.fragment.app.Fragment;
14-
1513
import com.aries.library.fast.FastConstant;
1614
import com.aries.library.fast.FastManager;
1715
import com.aries.library.fast.i.ActivityDispatchEventControl;
@@ -31,6 +29,7 @@
3129

3230
import java.util.List;
3331

32+
import androidx.fragment.app.Fragment;
3433
import butterknife.ButterKnife;
3534
import butterknife.Unbinder;
3635

@@ -46,6 +45,7 @@
4645
* 5、2018-6-25 13:25:30 增加解决StatusLayoutManager与SmartRefreshLayout冲突解决方案
4746
* 6、2018-9-25 10:04:31 新增onActivityResult统一处理逻辑
4847
* 7、2018-9-26 16:59:59 新增按键监听统一处理
48+
* 8、2019-12-19 11:53:28 增加EventBus Subscribe注解方法判断
4949
*/
5050
public abstract class BasisActivity extends RxAppCompatActivity implements IBasisView {
5151

@@ -65,7 +65,9 @@ public abstract class BasisActivity extends RxAppCompatActivity implements IBasi
6565
protected void onCreate(Bundle savedInstanceState) {
6666
if (isEventBusEnable()) {
6767
if (FastUtil.isClassExist(FastConstant.EVENT_BUS_CLASS)) {
68-
org.greenrobot.eventbus.EventBus.getDefault().register(this);
68+
if (FastUtil.haveEventBusAnnotation(this)) {
69+
org.greenrobot.eventbus.EventBus.getDefault().register(this);
70+
}
6971
}
7072
if (FastUtil.isClassExist(FastConstant.ANDROID_EVENT_BUS_CLASS)) {
7173
EventBus.getDefault().register(this);
@@ -106,7 +108,9 @@ protected void onResume() {
106108
protected void onDestroy() {
107109
if (isEventBusEnable()) {
108110
if (FastUtil.isClassExist(FastConstant.EVENT_BUS_CLASS)) {
109-
org.greenrobot.eventbus.EventBus.getDefault().unregister(this);
111+
if (FastUtil.haveEventBusAnnotation(this)) {
112+
org.greenrobot.eventbus.EventBus.getDefault().unregister(this);
113+
}
110114
}
111115
if (FastUtil.isClassExist(FastConstant.ANDROID_EVENT_BUS_CLASS)) {
112116
EventBus.getDefault().unregister(this);

library/src/main/java/com/aries/library/fast/basis/BasisFragment.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import android.view.ViewGroup;
1010
import android.widget.FrameLayout;
1111

12-
import androidx.fragment.app.FragmentManager;
13-
1412
import com.aries.library.fast.FastConstant;
1513
import com.aries.library.fast.FastManager;
1614
import com.aries.library.fast.i.IBasisView;
@@ -25,6 +23,7 @@
2523

2624
import org.simple.eventbus.EventBus;
2725

26+
import androidx.fragment.app.FragmentManager;
2827
import butterknife.ButterKnife;
2928
import butterknife.Unbinder;
3029

@@ -37,6 +36,7 @@
3736
* 2、增加解决StatusLayoutManager与SmartRefreshLayout冲突解决方案
3837
* 3、2018-7-6 17:12:16 删除IBasisFragment 控制是否单Fragment 通过另一种方式实现
3938
* 4、2019-1-29 18:33:10 修改对用户可以见回调{@link #setUserVisibleHint(boolean)}{@link #onHiddenChanged(boolean)} (boolean)}
39+
* 5、2019-12-19 11:54:20 增加EventBus Subscribe注解方法判断
4040
*/
4141
public abstract class BasisFragment extends RxFragment implements IBasisView {
4242

@@ -94,7 +94,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
9494
mIsViewLoaded = true;
9595
if (isEventBusEnable()) {
9696
if (FastUtil.isClassExist(FastConstant.EVENT_BUS_CLASS)) {
97-
org.greenrobot.eventbus.EventBus.getDefault().register(this);
97+
if (FastUtil.haveEventBusAnnotation(this)) {
98+
org.greenrobot.eventbus.EventBus.getDefault().register(this);
99+
}
98100
}
99101
if (FastUtil.isClassExist(FastConstant.ANDROID_EVENT_BUS_CLASS)) {
100102
EventBus.getDefault().register(this);
@@ -138,7 +140,9 @@ public void onDestroyView() {
138140
public void onDestroy() {
139141
if (isEventBusEnable()) {
140142
if (FastUtil.isClassExist(FastConstant.EVENT_BUS_CLASS)) {
141-
org.greenrobot.eventbus.EventBus.getDefault().unregister(this);
143+
if (FastUtil.haveEventBusAnnotation(this)) {
144+
org.greenrobot.eventbus.EventBus.getDefault().unregister(this);
145+
}
142146
}
143147
if (FastUtil.isClassExist(FastConstant.ANDROID_EVENT_BUS_CLASS)) {
144148
EventBus.getDefault().unregister(this);

library/src/main/java/com/aries/library/fast/util/FastUtil.java

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@
2222
import android.view.View;
2323
import android.view.ViewGroup;
2424

25-
import androidx.annotation.ColorInt;
26-
import androidx.annotation.Nullable;
27-
import androidx.core.graphics.drawable.DrawableCompat;
28-
25+
import com.aries.library.fast.FastConstant;
2926
import com.aries.library.fast.manager.LoggerManager;
3027
import com.aries.ui.util.DrawableUtil;
3128

29+
import java.lang.reflect.Method;
3230
import java.util.List;
3331
import java.util.Random;
3432

33+
import androidx.annotation.ColorInt;
34+
import androidx.annotation.Nullable;
35+
import androidx.core.graphics.drawable.DrawableCompat;
36+
3537
/**
3638
* @Author: AriesHoo on 2018/7/23 9:29
3739
* @E-Mail: AriesHoo@126.com
@@ -57,7 +59,7 @@ public static Application getApplication() {
5759
try {
5860
//兼容android P,直接调用@hide注解的方法来获取application对象
5961
Application app = ActivityThread.currentApplication();
60-
LoggerManager.e("getApplication0:"+app);
62+
LoggerManager.e("getApplication0:" + app);
6163
if (app != null) {
6264
return app;
6365
}
@@ -66,7 +68,7 @@ public static Application getApplication() {
6668
try {
6769
//兼容android P,直接调用@hide注解的方法来获取application对象
6870
Application app = AppGlobals.getInitialApplication();
69-
LoggerManager.e("getApplication1:"+app);
71+
LoggerManager.e("getApplication1:" + app);
7072
if (app != null) {
7173
return app;
7274
}
@@ -222,6 +224,51 @@ public static boolean isClassExist(String className) {
222224
return isExit;
223225
}
224226

227+
/**
228+
* {@link org.greenrobot.eventbus.EventBus} 要求注册之前, 订阅者必须含有一个或以上声明 {@link org.greenrobot.eventbus.Subscribe}
229+
* 注解的方法, 否则会报错, 所以如果要想完成在基类中自动注册, 避免报错就要先检查是否符合注册资格
230+
*
231+
* @param subscriber 订阅者
232+
* @return 返回 {@code true} 则表示含有 {@link org.greenrobot.eventbus.Subscribe} 注解, {@code false} 为不含有
233+
*/
234+
public static boolean haveEventBusAnnotation(Object subscriber) {
235+
if (!FastUtil.isClassExist(FastConstant.EVENT_BUS_CLASS)) {
236+
return false;
237+
}
238+
boolean skipSuperClasses = false;
239+
Class<?> clazz = subscriber.getClass();
240+
//查找类中符合注册要求的方法, 直到Object类
241+
while (clazz != null && !isSystemClass(clazz.getName()) && !skipSuperClasses) {
242+
Method[] allMethods;
243+
try {
244+
allMethods = clazz.getDeclaredMethods();
245+
} catch (Throwable th) {
246+
try {
247+
allMethods = clazz.getMethods();
248+
} catch (Throwable th2) {
249+
continue;
250+
} finally {
251+
skipSuperClasses = true;
252+
}
253+
}
254+
for (int i = 0; i < allMethods.length; i++) {
255+
Method method = allMethods[i];
256+
Class<?>[] parameterTypes = method.getParameterTypes();
257+
//查看该方法是否含有 Subscribe 注解
258+
if (method.isAnnotationPresent(org.greenrobot.eventbus.Subscribe.class) && parameterTypes.length == 1) {
259+
return true;
260+
}
261+
} //end for
262+
//获取父类, 以继续查找父类中符合要求的方法
263+
clazz = clazz.getSuperclass();
264+
}
265+
return false;
266+
}
267+
268+
private static boolean isSystemClass(String name) {
269+
return name.startsWith("java.") || name.startsWith("javax.") || name.startsWith("android.");
270+
}
271+
225272
public static boolean isRunningForeground(Context context) {
226273
return isRunningForeground(context, null);
227274
}

0 commit comments

Comments
 (0)