-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
232 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Class<?>, List<SubscriberMethod>> METHOD_CACHE = new ConcurrentHashMap<>(); | ||
private static final int MODIFIERS_IGNORE = Modifier.ABSTRACT | Modifier.STATIC | BRIDGE | SYNTHETIC; | ||
|
||
public List<SubscriberMethod> findMehod(Class<?> subscriberClass) { | ||
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass); | ||
if (subscriberMethods != null) { | ||
return subscriberMethods; | ||
} | ||
subscriberMethods = findByReflect(subscriberClass); | ||
return subscriberMethods; | ||
} | ||
|
||
private List<SubscriberMethod> findByReflect(Class<?> subscriberClass) { | ||
List<SubscriberMethod> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.kingja.rxbus2; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Description:TODO | ||
* Create Time:2017/5/26 14:40 | ||
* Author:KingJA | ||
* Email:kingjavip@gmail.com | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD}) | ||
public @interface Subscribe { | ||
ThreadMode threadMode() default ThreadMode.MAIN; | ||
} |
33 changes: 33 additions & 0 deletions
33
rxbus2/src/main/java/com/kingja/rxbus2/SubscriberMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.kingja.rxbus2; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* Description:TODO | ||
* Create Time:2017/4/13 15:28 | ||
* Author:KingJA | ||
* Email:kingjavip@gmail.com | ||
*/ | ||
public class SubscriberMethod { | ||
public ThreadMode getThreadMode() { | ||
return threadMode; | ||
} | ||
|
||
final ThreadMode threadMode; | ||
final Method method; | ||
final Class<?> eventType; | ||
|
||
public SubscriberMethod(Method method, Class<?> eventType,ThreadMode threadMode) { | ||
this.method = method; | ||
this.eventType = eventType; | ||
this.threadMode = threadMode; | ||
} | ||
|
||
public Method getMethod() { | ||
return method; | ||
} | ||
|
||
public Class<?> getEventType() { | ||
return eventType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.kingja.rxbus2; | ||
|
||
/** | ||
* Description:TODO | ||
* Create Time:2017/4/13 15:56 | ||
* Author:KingJA | ||
* Email:kingjavip@gmail.com | ||
*/ | ||
public class Subscription { | ||
final Object subscriber; | ||
final SubscriberMethod subscriberMethod; | ||
|
||
Subscription(Object subscriber, SubscriberMethod subscriberMethod) { | ||
this.subscriber = subscriber; | ||
this.subscriberMethod = subscriberMethod; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.kingja.rxbus2; | ||
|
||
import io.reactivex.Scheduler; | ||
|
||
/** | ||
* Description:TODO | ||
* Create Time:2017/5/26 14:47 | ||
* Author:KingJA | ||
* Email:kingjavip@gmail.com | ||
*/ | ||
public enum ThreadMode { | ||
SINGLE, COMPUTATION, IO, TRAMPOLINE, NEW_THREAD,MAIN | ||
} |