Skip to content

Commit a6d3e4e

Browse files
committed
see 03/26 log
1 parent 4bed20f commit a6d3e4e

35 files changed

+464
-432
lines changed

buildSrc/src/main/groovy/Config.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ class Config {
5353
// 本地第一次上传插件新的版本需设置 isApply = false, useLocal = true
5454
// 本地上传成功之后 isApply = true 即可应用插件来调试,后续版本更新无需设置 isApply = false
5555
// 发布版本的话把 isApply = false, useLocal = false,更新版本号,发布成功后 isApply = true 即可使用远程库版本
56-
plugin_api : new DepConfig(isApply: false, useLocal: true, pluginPath: "com.blankj:api-gradle-plugin:1.4-r1", pluginId: "com.blankj.api"),
56+
plugin_api : new DepConfig(isApply: true, useLocal: false, pluginPath: "com.blankj:api-gradle-plugin:1.2", pluginId: "com.blankj.api"),
5757
//./gradlew plugin:api-gradle-plugin:uploadArchives // 上传到本地 maven
5858
//./gradlew plugin:api-gradle-plugin:bintrayUpload // 上传到 jcenter
59-
plugin_bus : new DepConfig(isApply: true, useLocal: false, pluginPath: "com.blankj:bus-gradle-plugin:2.6-r2", pluginId: "com.blankj.bus"),
59+
plugin_bus : new DepConfig(isApply: true, useLocal: false, pluginPath: "com.blankj:bus-gradle-plugin:2.4", pluginId: "com.blankj.bus"),
6060
//./gradlew plugin:bus-gradle-plugin:uploadArchives // 上传到本地 maven
6161
//./gradlew plugin:bus-gradle-plugin:bintrayUpload // 上传到 jcenter
6262

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0-all.zip
Lines changed: 37 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.blankj.utilcode.util;
22

33
import android.support.annotation.NonNull;
4-
import android.text.TextUtils;
54
import android.util.Log;
65

7-
import java.io.IOException;
86
import java.lang.annotation.ElementType;
97
import java.lang.annotation.Retention;
108
import java.lang.annotation.RetentionPolicy;
@@ -25,12 +23,21 @@ public final class ApiUtils {
2523

2624
private static final String TAG = "ApiUtils";
2725

28-
private static final String PREFIX = "blankj.api/";
29-
30-
private Map<Class, Object> apiClass_apiInstance_map = new ConcurrentHashMap<>();
31-
private Map<Class, Class> apiClass_apiImplClass_map = new HashMap<>();
26+
private Map<Class, BaseApi> mApiMap = new ConcurrentHashMap<>();
27+
private Map<Class, Class> mInjectApiImplMap = new HashMap<>();
3228

3329
private ApiUtils() {
30+
init();
31+
}
32+
33+
/**
34+
* It'll be injected the implClasses who have {@link ApiUtils.Api} annotation
35+
* by function of {@link ApiUtils#registerImpl} when execute transform task.
36+
*/
37+
private void init() {/*inject*/}
38+
39+
private void registerImpl(Class implClass) {
40+
mInjectApiImplMap.put(implClass.getSuperclass(), implClass);
3441
}
3542

3643
/**
@@ -44,132 +51,47 @@ public static <T extends BaseApi> T getApi(@NonNull final Class<T> apiClass) {
4451
return getInstance().getApiInner(apiClass);
4552
}
4653

54+
public static void register(Class<? extends BaseApi> implClass) {
55+
getInstance().registerImpl(implClass);
56+
}
57+
4758
public static String toString_() {
4859
return getInstance().toString();
4960
}
5061

5162
@Override
5263
public String toString() {
53-
getAllApis();
54-
StringBuilder sb = new StringBuilder();
55-
sb.append("ApiUtils {");
56-
for (Map.Entry<Class, Class> entry : apiClass_apiImplClass_map.entrySet()) {
57-
sb.append("\n ")
58-
.append(entry.getKey().getName())
59-
.append(": ")
60-
.append(entry.getValue().getName());
61-
}
62-
sb.append("\n}");
63-
return sb.toString();
64+
return "ApiUtils: " + mInjectApiImplMap;
6465
}
6566

6667
private static ApiUtils getInstance() {
6768
return LazyHolder.INSTANCE;
6869
}
6970

7071
private <Result> Result getApiInner(Class apiClass) {
71-
Object apiInstance = apiClass_apiInstance_map.get(apiClass);
72-
if (apiInstance == null) {
73-
synchronized (this) {
74-
apiInstance = apiClass_apiInstance_map.get(apiClass);
75-
if (apiInstance == null) {
76-
Class implClass = getApiImplClass(apiClass);
77-
if (implClass != null) {
78-
try {
79-
apiInstance = implClass.newInstance();
80-
apiClass_apiInstance_map.put(apiClass, apiInstance);
81-
} catch (Exception ignore) {
82-
Log.e(TAG, "The api of <" + implClass + "> has no parameterless constructor.");
83-
return null;
84-
}
85-
} else {
86-
Log.e(TAG, "The api of <" + apiClass + "> doesn't implement.");
87-
return null;
88-
}
89-
}
90-
}
72+
BaseApi api = mApiMap.get(apiClass);
73+
if (api != null) {
74+
return (Result) api;
9175
}
92-
//noinspection unchecked
93-
return (Result) apiInstance;
94-
}
95-
96-
private Class getApiImplClass(Class apiClass) {
97-
Class apiImplClass = apiClass_apiImplClass_map.get(apiClass);
98-
if (apiImplClass != null) return apiImplClass;
99-
try {
100-
String[] apiImpls = Utils.getApp().getAssets().list(PREFIX + apiClass.getName());
101-
if (apiImpls == null) {
102-
return null;
103-
}
104-
if (apiImpls.length != 1) {
105-
Log.e(TAG, "The api of <" + apiClass + "> has more than one implement.");
106-
return null;
107-
}
108-
String apiImpl = apiImpls[0];
109-
if (TextUtils.isEmpty(apiImpl)) {
110-
Log.e(TAG, "The api of <" + apiClass + ">'s name is empty.");
111-
return null;
76+
synchronized (this) {
77+
api = mApiMap.get(apiClass);
78+
if (api != null) {
79+
return (Result) api;
11280
}
113-
String[] apiImpl_isMock = apiImpl.split("-");
114-
if (apiImpl_isMock.length != 2) {
115-
Log.e(TAG, "The api of <" + apiClass + ">'s implement <" + apiImpl
116-
+ "> which format of name is wrong.");
117-
return null;
118-
}
119-
String className = apiImpl_isMock[0];
120-
boolean isMock = Boolean.parseBoolean(apiImpl_isMock[1]);
121-
if (TextUtils.isEmpty(className)) {
122-
return null;
123-
}
124-
apiImplClass = Class.forName(className);
125-
return registerApiInner(apiClass, apiImplClass);
126-
} catch (ClassNotFoundException e) {
127-
e.printStackTrace();
128-
} catch (IOException e) {
129-
e.printStackTrace();
130-
}
131-
return null;
132-
}
133-
134-
private Class registerApiInner(Class apiClass, Class apiImplClass) {
135-
if (apiImplClass == null) return null;
136-
if (apiClass == null) {
137-
Class superclass = apiImplClass.getSuperclass();
138-
if (superclass == null) {
139-
Log.e(TAG, "<" + apiImplClass.getName() + ">'s superClass is null");
140-
return null;
141-
}
142-
apiClass = superclass;
143-
}
144-
//noinspection unchecked
145-
if (apiClass.isAssignableFrom(apiImplClass)) {
146-
apiClass_apiImplClass_map.put(apiClass, apiImplClass);
147-
return apiImplClass;
148-
} else {
149-
Log.e(TAG, "<" + apiImplClass.getName() + ">'s superClass is <"
150-
+ apiClass.getName() + ">, not <" + apiClass.getName() + ">");
151-
return null;
152-
}
153-
154-
}
155-
156-
static void registerApi(Class<? extends BaseApi> implClass) {
157-
getInstance().registerApiInner(null, implClass);
158-
}
159-
160-
private void getAllApis() {
161-
try {
162-
String[] apis = Utils.getApp().getAssets().list(PREFIX);
163-
if (apis == null) return;
164-
for (String api : apis) {
81+
Class implClass = mInjectApiImplMap.get(apiClass);
82+
if (implClass != null) {
16583
try {
166-
getApiImplClass(Class.forName(api));
167-
} catch (ClassNotFoundException e) {
168-
e.printStackTrace();
84+
api = (BaseApi) implClass.newInstance();
85+
mApiMap.put(apiClass, api);
86+
return (Result) api;
87+
} catch (Exception ignore) {
88+
Log.e(TAG, "The <" + implClass + "> has no parameterless constructor.");
89+
return null;
16990
}
91+
} else {
92+
Log.e(TAG, "The <" + apiClass + "> doesn't implement.");
93+
return null;
17094
}
171-
} catch (IOException e) {
172-
e.printStackTrace();
17395
}
17496
}
17597

@@ -183,6 +105,6 @@ private static class LazyHolder {
183105
boolean isMock() default false;
184106
}
185107

186-
public abstract static class BaseApi {
108+
public static class BaseApi {
187109
}
188110
}

lib/utilcode/src/main/java/com/blankj/utilcode/util/BusUtils.java

Lines changed: 22 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
import android.util.Log;
44

5-
import java.io.IOException;
65
import java.lang.annotation.ElementType;
76
import java.lang.annotation.Retention;
87
import java.lang.annotation.RetentionPolicy;
98
import java.lang.annotation.Target;
109
import java.lang.reflect.InvocationTargetException;
1110
import java.lang.reflect.Method;
1211
import java.util.ArrayList;
13-
import java.util.Collections;
14-
import java.util.Comparator;
1512
import java.util.HashMap;
1613
import java.util.HashSet;
1714
import java.util.List;
@@ -31,9 +28,8 @@
3128
*/
3229
public final class BusUtils {
3330

34-
private static final Object NULL = "nULl";
35-
private static final String TAG = "BusUtils";
36-
private static final String PREFIX = "blankj.bus/";
31+
private static final Object NULL = "nULl";
32+
private static final String TAG = "BusUtils";
3733

3834
private final Map<String, List<BusInfo>> mTag_BusInfoListMap = new HashMap<>();
3935

@@ -42,65 +38,24 @@ public final class BusUtils {
4238
private final Map<String, Map<String, Object>> mClassName_Tag_Arg4StickyMap = new ConcurrentHashMap<>();
4339

4440
private BusUtils() {
45-
try {
46-
String[] tags = Utils.getApp().getAssets().list(PREFIX);
47-
if (tags == null || tags.length == 0) {
48-
Log.w(TAG, "no bus");
49-
return;
50-
}
51-
for (String tag : tags) {
52-
String[] busInfos = Utils.getApp().getAssets().list(PREFIX + tag);
53-
if (busInfos == null || busInfos.length == 0) {
54-
Log.w(TAG, "The tag of <" + tag + "> no bus.");
55-
continue;
56-
}
57-
for (String busInfo : busInfos) {
58-
parseBusInfo(tag, busInfo);
59-
}
60-
sortBusByPriority(tag);
61-
}
62-
} catch (IOException e) {
63-
e.printStackTrace();
64-
}
41+
init();
6542
}
6643

67-
private void parseBusInfo(String tag, String busInfo) {
68-
String[] split = busInfo.split("-");
69-
if (split.length != 7) {
70-
Log.e(TAG, "The tag of <" + tag + ">'s bus <" + busInfo + "> which format is wrong.");
71-
return;
72-
}
73-
String className = split[0];
74-
String funName = split[1];
75-
String paramType = split[2];
76-
String paramName = split[3];
77-
boolean sticky = Boolean.parseBoolean(split[4]);
78-
String threadMode = split[5];
79-
int priority;
80-
try {
81-
priority = Integer.parseInt(split[6]);
82-
} catch (NumberFormatException e) {
83-
Log.e(TAG, "The tag of <" + tag + ">'s bus <" + busInfo + "> which format is wrong.");
84-
return;
85-
}
86-
registerBusInner(tag, className, funName, paramType, paramName, sticky, threadMode, priority);
87-
}
44+
/**
45+
* It'll be injected the bus who have {@link Bus} annotation
46+
* by function of {@link BusUtils#registerBus} when execute transform task.
47+
*/
48+
private void init() {/*inject*/}
8849

89-
private void sortBusByPriority(String tag) {
90-
List<BusInfo> busInfoList = mTag_BusInfoListMap.get(tag);
91-
if (busInfoList != null && busInfoList.size() > 1) {
92-
Collections.sort(busInfoList, new Comparator<BusInfo>() {
93-
@Override
94-
public int compare(BusInfo o0, BusInfo o1) {
95-
return o1.priority - o0.priority;
96-
}
97-
});
98-
}
50+
private void registerBus(String tag,
51+
String className, String funName, String paramType, String paramName,
52+
boolean sticky, String threadMode) {
53+
registerBus(tag, className, funName, paramType, paramName, sticky, threadMode, 0);
9954
}
10055

101-
private void registerBusInner(String tag,
102-
String className, String funName, String paramType, String paramName,
103-
boolean sticky, String threadMode, int priority) {
56+
private void registerBus(String tag,
57+
String className, String funName, String paramType, String paramName,
58+
boolean sticky, String threadMode, int priority) {
10459
List<BusInfo> busInfoList = mTag_BusInfoListMap.get(tag);
10560
if (busInfoList == null) {
10661
busInfoList = new ArrayList<>();
@@ -109,12 +64,6 @@ private void registerBusInner(String tag,
10964
busInfoList.add(new BusInfo(className, funName, paramType, paramName, sticky, threadMode, priority));
11065
}
11166

112-
public static void registerBus(String tag,
113-
String className, String funName, String paramType, String paramName,
114-
boolean sticky, String threadMode, int priority) {
115-
getInstance().registerBusInner(tag, className, funName, paramType, paramName, sticky, threadMode, priority);
116-
}
117-
11867
public static void register(final Object bus) {
11968
getInstance().registerInner(bus);
12069
}
@@ -152,20 +101,6 @@ public String toString() {
152101
return "BusUtils: " + mTag_BusInfoListMap;
153102
}
154103

155-
static Runnable getPreLoadRunnable() {
156-
return new Runnable() {
157-
@Override
158-
public void run() {
159-
preLoad();
160-
}
161-
};
162-
}
163-
164-
private static void preLoad() {
165-
//noinspection ResultOfMethodCallIgnored
166-
getInstance();
167-
}
168-
169104
private static BusUtils getInstance() {
170105
return LazyHolder.INSTANCE;
171106
}
@@ -244,9 +179,6 @@ private void postInner(final String tag, final Object arg, final boolean sticky)
244179
if (busInfo.method == null) {
245180
Method method = getMethodByBusInfo(busInfo);
246181
if (method == null) {
247-
Log.e(TAG, "The bus of tag <" + tag + ">'s method <" + busInfo.funName +
248-
("".equals(busInfo.paramType) ? "()" : ("(" + busInfo.paramType + " " + busInfo.paramName + ")"))
249-
+ "> is not exists.");
250182
return;
251183
}
252184
busInfo.method = method;
@@ -302,7 +234,7 @@ public void run() {
302234
};
303235
switch (busInfo.threadMode) {
304236
case "MAIN":
305-
UtilsBridge.runOnUiThread(runnable);
237+
ThreadUtils.runOnUiThread(runnable);
306238
return;
307239
case "IO":
308240
ThreadUtils.getIoPool().execute(runnable);
@@ -399,6 +331,12 @@ private void removeStickyInner(final String tag) {
399331
}
400332
}
401333

334+
static void registerBus4Test(String tag,
335+
String className, String funName, String paramType, String paramName,
336+
boolean sticky, String threadMode, int priority) {
337+
getInstance().registerBus(tag, className, funName, paramType, paramName, sticky, threadMode, priority);
338+
}
339+
402340
private static final class BusInfo {
403341

404342
String className;

0 commit comments

Comments
 (0)