diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml
new file mode 100644
index 0000000..9d5b48d
--- /dev/null
+++ b/.idea/checkstyle-idea.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/java/example/kingja/rxbus/FragmentA.java b/app/src/main/java/example/kingja/rxbus/FragmentA.java
index 906a072..9bf666e 100644
--- a/app/src/main/java/example/kingja/rxbus/FragmentA.java
+++ b/app/src/main/java/example/kingja/rxbus/FragmentA.java
@@ -3,6 +3,7 @@
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
+import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -11,6 +12,7 @@
import com.kingja.rxbus2.Callback;
import com.kingja.rxbus2.RxBus;
+import com.kingja.rxbus2.Subscribe;
/**
* Description:TODO
@@ -27,7 +29,7 @@ public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
savedInstanceState) {
-
+ RxBus.getDefault().register(this);
rootView = inflater.inflate(R.layout.fragment_a, container, false);
return rootView;
}
@@ -70,17 +72,14 @@ public void onClick(View v) {
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
- RxBus.getDefault().register(this, EventA.class, new Callback() {
- @Override
- public void onReceive(EventA event) {
- ((TextView) rootView.findViewById(R.id.tv_eventMsg)).setText(event.getMsg());
- }
- });
- RxBus.getDefault().register(this, EventC.class, new Callback() {
- @Override
- public void onReceive(EventC event) {
- ((TextView) rootView.findViewById(R.id.tv_eventMsg)).setText(event.getMsg());
- }
- });
+
+ }
+ @Subscribe
+ public void receiveEventA(EventA event) {
+ ((TextView) rootView.findViewById(R.id.tv_eventMsg)).setText(event.getMsg());
+ }
+ @Subscribe
+ public void receiveEventC(EventC event) {
+ ((TextView) rootView.findViewById(R.id.tv_eventMsg)).setText(event.getMsg());
}
}
diff --git a/app/src/main/java/example/kingja/rxbus/FragmentB.java b/app/src/main/java/example/kingja/rxbus/FragmentB.java
index 957f623..47e936e 100644
--- a/app/src/main/java/example/kingja/rxbus/FragmentB.java
+++ b/app/src/main/java/example/kingja/rxbus/FragmentB.java
@@ -11,6 +11,7 @@
import com.kingja.rxbus2.Callback;
import com.kingja.rxbus2.RxBus;
+import com.kingja.rxbus2.Subscribe;
/**
* Description:TODO
@@ -50,17 +51,14 @@ public void onClick(View v) {
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
- RxBus.getDefault().register(this, EventB.class, new Callback() {
- @Override
- public void onReceive(EventB event) {
- ((TextView) rootView.findViewById(R.id.tv_eventMsg)).setText(event.getMsg());
- }
- });
- RxBus.getDefault().register(this, EventC.class, new Callback() {
- @Override
- public void onReceive(EventC event) {
- ((TextView) rootView.findViewById(R.id.tv_eventMsg)).setText(event.getMsg());
- }
- });
+ RxBus.getDefault().register(this);
+ }
+ @Subscribe
+ public void receiveEventB(EventB event) {
+ ((TextView) rootView.findViewById(R.id.tv_eventMsg)).setText(event.getMsg());
+ }
+ @Subscribe
+ public void receiveEventC(EventC event) {
+ ((TextView) rootView.findViewById(R.id.tv_eventMsg)).setText(event.getMsg());
}
}
diff --git a/app/src/main/java/example/kingja/rxbus/MainActivity.java b/app/src/main/java/example/kingja/rxbus/MainActivity.java
index 783e980..4c9c49b 100644
--- a/app/src/main/java/example/kingja/rxbus/MainActivity.java
+++ b/app/src/main/java/example/kingja/rxbus/MainActivity.java
@@ -11,6 +11,7 @@
import com.kingja.rxbus2.Callback;
import com.kingja.rxbus2.RxBus;
+import com.kingja.rxbus2.Subscribe;
public class MainActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();
@@ -24,13 +25,7 @@ protected void onCreate(Bundle savedInstanceState) {
getSupportFragmentManager().beginTransaction().add(R.id.fl_fragmentA, new FragmentA()).commit();
getSupportFragmentManager().beginTransaction().add(R.id.fl_fragmentB, new FragmentB()).commit();
-
- RxBus.getDefault().register(this, EventMain.class, new Callback() {
- @Override
- public void onReceive(EventMain event) {
- ((TextView) findViewById(R.id.tv_main_eventMsg)).setText(event.getMsg());
- }
- });
+ RxBus.getDefault().register(this);
}
public void sendEventA(View view) {
@@ -45,4 +40,9 @@ public void sendEventC(View view) {
RxBus.getDefault().post(new EventC(this));
}
+ @Subscribe
+ public void receiveEventMain(EventMain event) {
+ ((TextView) findViewById(R.id.tv_main_eventMsg)).setText(event.getMsg());
+ }
+
}
diff --git a/rxbus2/src/main/java/com/kingja/rxbus2/MethodFinder.java b/rxbus2/src/main/java/com/kingja/rxbus2/MethodFinder.java
new file mode 100644
index 0000000..a93deb2
--- /dev/null
+++ b/rxbus2/src/main/java/com/kingja/rxbus2/MethodFinder.java
@@ -0,0 +1,52 @@
+package com.kingja.rxbus2;
+
+import android.util.Log;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Description:TODO
+ * Create Time:2017/4/13 15:27
+ * Author:KingJA
+ * Email:kingjavip@gmail.com
+ */
+public class MethodFinder {
+ private static final int BRIDGE = 0x40;
+ private static final int SYNTHETIC = 0x1000;
+ private static final Map, List> METHOD_CACHE = new ConcurrentHashMap<>();
+ private static final int MODIFIERS_IGNORE = Modifier.ABSTRACT | Modifier.STATIC | BRIDGE | SYNTHETIC;
+
+ public List findMehod(Class> subscriberClass) {
+ List subscriberMethods = METHOD_CACHE.get(subscriberClass);
+ if (subscriberMethods != null) {
+ return subscriberMethods;
+ }
+ subscriberMethods = findByReflect(subscriberClass);
+ return subscriberMethods;
+ }
+
+ private List findByReflect(Class> subscriberClass) {
+ List subscriberMethods = new ArrayList<>();
+ Method[] methods = subscriberClass.getDeclaredMethods();
+ for (Method method : methods) {
+ int modifiers = method.getModifiers();
+ if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
+ Class>[] parameterTypes = method.getParameterTypes();
+ if (parameterTypes.length == 1) {
+ Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
+ if (subscribeAnnotation != null) {
+ ThreadMode threadMode = subscribeAnnotation.threadMode();
+ Class> eventType = parameterTypes[0];
+ subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode));
+ }
+ }
+ }
+ }
+ return subscriberMethods;
+ }
+}
diff --git a/rxbus2/src/main/java/com/kingja/rxbus2/RxBus.java b/rxbus2/src/main/java/com/kingja/rxbus2/RxBus.java
index e865437..295a1fc 100644
--- a/rxbus2/src/main/java/com/kingja/rxbus2/RxBus.java
+++ b/rxbus2/src/main/java/com/kingja/rxbus2/RxBus.java
@@ -1,12 +1,11 @@
package com.kingja.rxbus2;
-import android.content.Context;
-import android.util.Log;
-
+import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
@@ -14,6 +13,7 @@
import io.reactivex.functions.Consumer;
import io.reactivex.processors.FlowableProcessor;
import io.reactivex.processors.PublishProcessor;
+import io.reactivex.schedulers.Schedulers;
/**
* Description:TODO
@@ -26,6 +26,7 @@ public class RxBus {
private final FlowableProcessor