privatevoidfindUsingReflectionInSingleClass(FindStatefindState){Method[]methods;// 优先使用getDeclareMethods方法,如注释中所说,比getMethods方法块。try{// This is faster than getMethods, especially when subscribers are fat classes like Activitiesmethods=findState.clazz.getDeclaredMethods();}catch(Throwableth){// Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149methods=findState.clazz.getMethods();findState.skipSuperClasses=true;}for(Methodmethod:methods){intmodifiers=method.getModifiers();// 通过访问符只获取publicif((modifiers&Modifier.PUBLIC)!=0&&(modifiers&MODIFIERS_IGNORE)==0){Class<?>[]parameterTypes=method.getParameterTypes();// 方法的参数(事件类型)长度只能为1if(parameterTypes.length==1){SubscribesubscribeAnnotation=method.getAnnotation(Subscribe.class);if(subscribeAnnotation!=null){Class<?>eventType=parameterTypes[0];// 获取到annotation中的内容,进行subscriberMethod的添加if(findState.checkAdd(method,eventType)){ThreadModethreadMode=subscribeAnnotation.threadMode();findState.subscriberMethods.add(newSubscriberMethod(method,eventType,threadMode,subscribeAnnotation.priority(),subscribeAnnotation.sticky()));}}}elseif(strictMethodVerification&&method.isAnnotationPresent(Subscribe.class)){//抛出方法参数只能为1的异常StringmethodName=method.getDeclaringClass().getName()+"."+method.getName();thrownewEventBusException("@Subscribe method "+methodName+"must have exactly 1 parameter but has "+parameterTypes.length);}}elseif(strictMethodVerification&&method.isAnnotationPresent(Subscribe.class)){//抛出方法访问符只能为public的异常StringmethodName=method.getDeclaringClass().getName()+"."+method.getName();thrownewEventBusException(methodName+" is a illegal @Subscribe method: must be public, non-static, and non-abstract");}}}
finalclassBackgroundPosterimplementsRunnable{privatefinalPendingPostQueuequeue;privatefinalEventBuseventBus;privatevolatilebooleanexecutorRunning;BackgroundPoster(EventBuseventBus){this.eventBus=eventBus;queue=newPendingPostQueue();}publicvoidenqueue(Subscriptionsubscription,Objectevent){PendingPostpendingPost=PendingPost.obtainPendingPost(subscription,event);synchronized(this){queue.enqueue(pendingPost);if(!executorRunning){executorRunning=true;eventBus.getExecutorService().execute(this);}}}@Overridepublicvoidrun(){try{try{while(true){PendingPostpendingPost=queue.poll(1000);if(pendingPost==null){synchronized(this){// Check again, this time in synchronizedpendingPost=queue.poll();if(pendingPost==null){executorRunning=false;return;}}}eventBus.invokeSubscriber(pendingPost);}}catch(InterruptedExceptione){Log.w("Event",Thread.currentThread().getName()+" was interruppted",e);}}finally{executorRunning=false;}}}
classAsyncPosterimplementsRunnable{privatefinalPendingPostQueuequeue;privatefinalEventBuseventBus;AsyncPoster(EventBuseventBus){this.eventBus=eventBus;queue=newPendingPostQueue();}publicvoidenqueue(Subscriptionsubscription,Objectevent){PendingPostpendingPost=PendingPost.obtainPendingPost(subscription,event);queue.enqueue(pendingPost);eventBus.getExecutorService().execute(this);}@Overridepublicvoidrun(){PendingPostpendingPost=queue.poll();if(pendingPost==null){thrownewIllegalStateException("No pending post available");}eventBus.invokeSubscriber(pendingPost);}}
finalclassPendingPostQueue{privatePendingPosthead;privatePendingPosttail;synchronizedvoidenqueue(PendingPostpendingPost){if(pendingPost==null){thrownewNullPointerException("null cannot be enqueued");}if(tail!=null){tail.next=pendingPost;tail=pendingPost;}elseif(head==null){head=tail=pendingPost;}else{thrownewIllegalStateException("Head present, but no tail");}notifyAll();}synchronizedPendingPostpoll(){PendingPostpendingPost=head;if(head!=null){head=head.next;if(head==null){tail=null;}}returnpendingPost;}synchronizedPendingPostpoll(intmaxMillisToWait)throwsInterruptedException{if(head==null){wait(maxMillisToWait);}returnpoll();}}
finalclassPendingPost{privatefinalstaticList<PendingPost>pendingPostPool=newArrayList<PendingPost>();Objectevent;Subscriptionsubscription;PendingPostnext;// 对PendingPost的获取,优先从缓存池中拿privatePendingPost(Objectevent,Subscriptionsubscription){this.event=event;this.subscription=subscription;}staticPendingPostobtainPendingPost(Subscriptionsubscription,Objectevent){synchronized(pendingPostPool){intsize=pendingPostPool.size();if(size>0){PendingPostpendingPost=pendingPostPool.remove(size-1);pendingPost.event=event;pendingPost.subscription=subscription;pendingPost.next=null;returnpendingPost;}}returnnewPendingPost(event,subscription);}// 对PendingPost释放时,将其添加到缓存池中staticvoidreleasePendingPost(PendingPostpendingPost){pendingPost.event=null;pendingPost.subscription=null;pendingPost.next=null;synchronized(pendingPostPool){// Don't let the pool grow indefinitelyif(pendingPostPool.size()<10000){pendingPostPool.add(pendingPost);}}}}