在Android項(xiàng)目中告喊,相信Eventbus大家已經(jīng)很熟悉了,Eventbus是一個(gè)支持跨Module的事件發(fā)送響應(yīng)框架低零,但是隨著項(xiàng)目的不斷擴(kuò)大 以及多人團(tuán)隊(duì)的開發(fā)丙曙,漸漸的會(huì)出現(xiàn)一個(gè)不大和諧的情況,就是有一部分開發(fā)童鞋由于一些特定的業(yè)務(wù)場(chǎng)景翅萤,會(huì)經(jīng)常復(fù)用組內(nèi)其他成員的event事件恐疲,久而久之 就會(huì)出現(xiàn)多個(gè)eventbus事件發(fā)送 多個(gè)場(chǎng)景同時(shí)接收腊满,某天也許你會(huì)發(fā)現(xiàn)一個(gè)很奇怪的bug,但怎么也找不到原因培己,最后會(huì)發(fā)現(xiàn)是之前的童鞋濫用的eventbus導(dǎo)致碳蛋,這類的bug往往要浪費(fèi)很長(zhǎng)時(shí)間,而且有的event事件也找不到post源頭和handle的接受者省咨。
應(yīng)對(duì)這種情況肃弟,本人開發(fā)了點(diǎn)對(duì)點(diǎn)的開發(fā)框架,大體保持了EventBus的用法零蓉,以下是使用方式:
接收:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PointBus.getDefault().register(this);
setContentView(R.layout.activity_main);
}
public void onTestClick(View view){
Intent intent = new Intent(this, TestActivity.class);
startActivity(intent);
}
@PointSubscribe(threadMode = ThreadMode.MAIN)
public void handleEvent(TestEvent testEvent) {
Toast.makeText(this,testEvent.msg,Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
PointBus.getDefault().unRegister(this);
}
發(fā)送:
public void testPost() {
PointBus.getDefault().post(this, new TestEvent("from TestActivity"), "com.fly.ui.test.MainActivity");
}
這里的com.fly.ui.test.MainActivity就是上述接收事件的Activity 就算其他的Actvity或者Fragment 處理的相同的event 也接收不到 你可以指定你的event 往哪發(fā)松
下面是實(shí)現(xiàn)原理笤受,大體原理還是使用的APT 這里對(duì)APT不熟悉的童鞋可以找些基礎(chǔ)的文章學(xué)習(xí)一下 很容易上手 下面貼一下process部分關(guān)鍵的代碼
TypeElement classElement = (TypeElement) method.getEnclosingElement();
VariableElement param = method.getParameters().get(0);
TypeMirror typeMirror = getParamTypeMirror(param, messager);
String className = classElement.getSimpleName().toString();
if (element instanceof ExecutableElement) {
PointSubscribe bindView = element.getAnnotation(PointSubscribe.class);
String threadMode = bindView.threadMode().name();
ExecutableElement method = (ExecutableElement) element;
這樣明白了注解標(biāo)注的方法對(duì)應(yīng)的類的全名和event類全名 所有的信息都存儲(chǔ)在緩存列表中
最后創(chuàng)建一個(gè)Java文件
Writer writer = jfo.openWriter();
writer.write("package " + packName + ";");
writer.write("\n\n");
writer.write("import com.fly.bus.SubscribeImpl;");
writer.write("\n");
writer.write("import com.fly.bus.SubscribeInfo;");
writer.write("\n");
writer.write("import java.util.ArrayList;");
writer.write("\n");
writer.write("import java.util.HashMap;");
writer.write("\n");
writer.write("import java.util.List;");
writer.write("\n");
writer.write("public class " + newClassName + " implements SubscribeImpl{\n\n");
writer.write(" public static HashMap<String,List<SubscribeInfo>> subscribeInfoMap;\n");
writer.write(" public static List<SubscribeInfo> subscribeInfoList;\n");
writer.write(" static {\n");
writer.write(" subscribeInfoMap = new HashMap<>();\n");
initSubscribeInfo(writer, hashMap);
writer.write(" }\n");
writer.write(" public static void initSubscribeInfo(String methodName,String eventName,String threadMode){\n");
writer.write(" subscribeInfoList.add(new SubscribeInfo(methodName,eventName,threadMode)); \n");
writer.write(" }\n");
writer.write(" @Override\n");
writer.write(" public HashMap<String, List<SubscribeInfo>> getSubscribeInfo(){\n");
writer.write(" return subscribeInfoMap;\n");
writer.write(" }\n");
writer.write("\n\n");
writer.write("}\n");
writer.flush();
writer.close();
private void initSubscribeInfo(Writer writer, HashMap<String, List<SimpleSubscribeInfo>> hashMap) {
try {
for (String className : hashMap.keySet()) {
writer.write(" subscribeInfoList = new ArrayList<>();\n");
List<SimpleSubscribeInfo> infoList = hashMap.get(className);
if (infoList == null || infoList.size() == 0) {
continue;
}
for (SimpleSubscribeInfo info : infoList) {
String methodName = info.getMethodName();
String eventName = info.getEventName();
String threadMode = info.getThreadMode();
writer.write(" initSubscribeInfo(" + "\"" + methodName + "\"" + "," + "\"" + eventName + "\"" + "," + "\"" + threadMode + "\"" + "); \n");
}
writer.write(" subscribeInfoMap.put(" + "\"" + className + "\"" + ",subscribeInfoList);\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
總結(jié):將所有注解的方法對(duì)應(yīng)的類和參數(shù)名 在編譯器 利用AbstractProcessor保存在一個(gè)在編譯器生成的Hashmap里,當(dāng)你post的時(shí)候 以你的目標(biāo)類的全名(com.fly.ui.test.MainActivity)作為key去尋找出對(duì)應(yīng)的value敌蜂,value里保存著event類全名和方法名 以及要處理事件的線程箩兽,然后就invoke method即可
最后附上github鏈接:https://github.com/tmacfly/PointBusMaster
轉(zhuǎn)發(fā)請(qǐng)標(biāo)明出處 萬分感謝