Skip to content

Commit 8780ba5

Browse files
authored
Merge pull request #38 from alibaba/develop
Fix autowired performance issues
2 parents 974b6da + 071b48d commit 8780ba5

File tree

9 files changed

+93
-30
lines changed

9 files changed

+93
-30
lines changed

app/src/main/java/com/alibaba/android/arouter/demo/MainActivity.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.alibaba.android.arouter.demo;
22

33
import android.app.Activity;
4-
import android.app.AlertDialog;
5-
import android.content.DialogInterface;
64
import android.content.Intent;
75
import android.os.Bundle;
86
import android.support.v7.app.AppCompatActivity;
@@ -79,7 +77,7 @@ public void onClick(View v) {
7977
ARouter.getInstance().build("/test/activity1")
8078
.withString("name", "老王")
8179
.withInt("age", 18)
82-
.withBoolean("girl", true)
80+
.withBoolean("boy", true)
8381
.withLong("high", 180)
8482
.withString("url", "https://a.b.c")
8583
.navigation();

arouter-api/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ext {
55
artifact = bintrayName
66
libraryName = 'ARouter sdk'
77
libraryDescription = 'A router for android'
8-
libraryVersion = '1.0.6'
8+
libraryVersion = '1.0.7'
99
}
1010

1111
android {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.alibaba.android.arouter.core;
2+
3+
import android.content.Context;
4+
5+
import com.alibaba.android.arouter.facade.annotation.Route;
6+
import com.alibaba.android.arouter.facade.service.AutowiredService;
7+
import com.alibaba.android.arouter.facade.template.ISyringe;
8+
9+
import org.apache.commons.collections4.map.LRUMap;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
import static com.alibaba.android.arouter.utils.Consts.SUFFIX_AUTOWIRED;
16+
17+
/**
18+
* Autowired service impl.
19+
*
20+
* @author zhilong <a href="mailto:zhilong.lzl@alibaba-inc.com">Contact me.</a>
21+
* @version 1.0
22+
* @since 2017/2/28 下午6:08
23+
*/
24+
@Route(path = "/arouter/service/autowired")
25+
public class AutowiredServiceImpl implements AutowiredService {
26+
private Map<String, ISyringe> classCache;
27+
private List<String> blackList;
28+
29+
@Override
30+
public void init(Context context) {
31+
classCache = new LRUMap<>();
32+
blackList = new ArrayList<>();
33+
}
34+
35+
@Override
36+
public void autowire(Object instance) {
37+
String className = instance.getClass().getName();
38+
try {
39+
if (!blackList.contains(className)) {
40+
ISyringe autowiredHelper = classCache.get(className);
41+
if (null == autowiredHelper) { // No cache.
42+
autowiredHelper = (ISyringe) Class.forName(instance.getClass().getName() + SUFFIX_AUTOWIRED).getConstructor().newInstance();
43+
}
44+
autowiredHelper.inject(instance);
45+
classCache.put(className, autowiredHelper);
46+
}
47+
} catch (Exception ex) {
48+
// ARouter.logger.error(TAG, "Autowired made exception, in class [" + className + "]");
49+
blackList.add(className); // This instance need not autowired.
50+
}
51+
}
52+
}

arouter-api/src/main/java/com/alibaba/android/arouter/core/LogisticsCenter.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public synchronized static void completion(Postcard postcard) {
154154
setValue(postcard,
155155
params.getValue(),
156156
params.getKey(),
157-
resultMap.get(TextUtils.getRight(params.getKey())));
157+
resultMap.get(params.getKey()));
158158
}
159159

160160
// Save params name which need autoinject.
@@ -200,37 +200,35 @@ public synchronized static void completion(Postcard postcard) {
200200
*/
201201
private static void setValue(Postcard postcard, Integer typeDef, String key, String value) {
202202
try {
203-
String currentKey = TextUtils.getLeft(key);
204-
205203
if (null != typeDef) {
206204
switch (typeDef) {
207205
case Consts.DEF_BOOLEAN:
208-
postcard.withBoolean(currentKey, Boolean.parseBoolean(value));
206+
postcard.withBoolean(key, Boolean.parseBoolean(value));
209207
break;
210208
case Consts.DEF_BYTE:
211-
postcard.withByte(currentKey, Byte.valueOf(value));
209+
postcard.withByte(key, Byte.valueOf(value));
212210
break;
213211
case Consts.DEF_SHORT:
214-
postcard.withShort(currentKey, Short.valueOf(value));
212+
postcard.withShort(key, Short.valueOf(value));
215213
break;
216214
case Consts.DEF_INT:
217-
postcard.withInt(currentKey, Integer.valueOf(value));
215+
postcard.withInt(key, Integer.valueOf(value));
218216
break;
219217
case Consts.DEF_LONG:
220-
postcard.withLong(currentKey, Long.valueOf(value));
218+
postcard.withLong(key, Long.valueOf(value));
221219
break;
222220
case Consts.DEF_FLOAT:
223-
postcard.withFloat(currentKey, Float.valueOf(value));
221+
postcard.withFloat(key, Float.valueOf(value));
224222
break;
225223
case Consts.DEF_DOUBLE:
226-
postcard.withDouble(currentKey, Double.valueOf(value));
224+
postcard.withDouble(key, Double.valueOf(value));
227225
break;
228226
case Consts.DEF_STRING:
229227
default:
230-
postcard.withString(currentKey, value);
228+
postcard.withString(key, value);
231229
}
232230
} else {
233-
postcard.withString(currentKey, value);
231+
postcard.withString(key, value);
234232
}
235233
} catch (Throwable ex) {
236234
logger.warning(Consts.TAG, "LogisticsCenter setValue failed! " + ex.getMessage());
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.alibaba.android.arouter.facade.service;
2+
3+
import com.alibaba.android.arouter.facade.template.IProvider;
4+
5+
/**
6+
* Service for autowired.
7+
*
8+
* @author zhilong <a href="mailto:zhilong.lzl@alibaba-inc.com">Contact me.</a>
9+
* @version 1.0
10+
* @since 2017/2/28 下午6:06
11+
*/
12+
public interface AutowiredService extends IProvider {
13+
14+
/**
15+
* Autowired core.
16+
* @param instance the instance who need autowired.
17+
*/
18+
void autowire(Object instance);
19+
}

arouter-api/src/main/java/com/alibaba/android/arouter/launcher/_ARouter.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
import com.alibaba.android.arouter.facade.Postcard;
1717
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
1818
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
19+
import com.alibaba.android.arouter.facade.service.AutowiredService;
1920
import com.alibaba.android.arouter.facade.service.DegradeService;
2021
import com.alibaba.android.arouter.facade.service.InterceptorService;
2122
import com.alibaba.android.arouter.facade.service.PathReplaceService;
2223
import com.alibaba.android.arouter.facade.template.ILogger;
23-
import com.alibaba.android.arouter.facade.template.ISyringe;
2424
import com.alibaba.android.arouter.thread.DefaultPoolExecutor;
2525
import com.alibaba.android.arouter.utils.Consts;
2626
import com.alibaba.android.arouter.utils.DefaultLogger;
@@ -31,9 +31,6 @@
3131
import java.lang.reflect.Method;
3232
import java.util.concurrent.ThreadPoolExecutor;
3333

34-
import static com.alibaba.android.arouter.utils.Consts.SUFFIX_AUTOWIRED;
35-
import static com.alibaba.android.arouter.utils.Consts.TAG;
36-
3734
/**
3835
* ARouter core
3936
*
@@ -169,12 +166,9 @@ static void setLogger(ILogger userLogger) {
169166
}
170167

171168
static void inject(Object thiz) {
172-
try {
173-
Class autowiredClass = Class.forName(thiz.getClass().getName() + SUFFIX_AUTOWIRED);
174-
ISyringe iSyringe = (ISyringe) autowiredClass.getConstructor().newInstance();
175-
iSyringe.inject(thiz);
176-
} catch (Exception ex) {
177-
logger.error(TAG, "Autowired made exception, message [" + ex.getMessage() + "]");
169+
AutowiredService autowiredService = ((AutowiredService) ARouter.getInstance().build("/arouter/service/autowired").navigation());
170+
if (null != autowiredService) {
171+
autowiredService.autowire(thiz);
178172
}
179173
}
180174

@@ -245,7 +239,8 @@ private String extractGroup(String path) {
245239
}
246240

247241
static void afterInit() {
248-
interceptorService = ARouter.getInstance().navigation(InterceptorService.class); // Trigger interceptor init.
242+
// Trigger interceptor init, use byName.
243+
interceptorService = (InterceptorService) ARouter.getInstance().build("/arouter/service/interceptor").navigation();
249244
}
250245

251246
protected <T> T navigation(Class<? extends T> service) {

arouter-compiler/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ext {
55
artifact = bintrayName
66
libraryName = 'ARouter compiler'
77
libraryDescription = 'A compiler for ARouter to find route'
8-
libraryVersion = '1.0.2'
8+
libraryVersion = '1.0.3'
99
}
1010

1111
compileJava {

arouter-compiler/src/main/java/com/alibaba/android/arouter/compiler/processor/AutowiredProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import org.apache.commons.collections4.CollectionUtils;
1616
import org.apache.commons.collections4.MapUtils;
17+
import org.apache.commons.lang3.StringUtils;
1718

1819
import java.io.IOException;
1920
import java.util.ArrayList;
@@ -167,7 +168,7 @@ private void generateHelper() throws IOException, IllegalAccessException {
167168
}
168169

169170
statment = buildStatement(statment, TypeUtils.typeExchange(element.asType()), isActivity);
170-
injectMethodBuilder.addStatement(statment, fieldName);
171+
injectMethodBuilder.addStatement(statment, StringUtils.isEmpty(fieldConfig.name()) ? fieldName : fieldConfig.name());
171172

172173
// Validater
173174
if (fieldConfig.required() && !element.asType().getKind().isPrimitive()) { // Primitive wont be check.

arouter-compiler/src/main/java/com/alibaba/android/arouter/compiler/processor/RouteProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private void parseRoutes(Set<? extends Element> routeElements) throws IOExceptio
222222
if (field.getKind().isField() && field.getAnnotation(Autowired.class) != null && !typeUtil.isSubtype(field.asType(), iProvider)) {
223223
// It must be field, then it has annotation, but it not be provider.
224224
Autowired paramConfig = field.getAnnotation(Autowired.class);
225-
paramsType.put(StringUtils.isEmpty(paramConfig.name()) ? field.getSimpleName().toString() : field.getSimpleName().toString() + "|" + paramConfig.name(), TypeUtils.typeExchange(field.asType()));
225+
paramsType.put(StringUtils.isEmpty(paramConfig.name()) ? field.getSimpleName().toString() : paramConfig.name(), TypeUtils.typeExchange(field.asType()));
226226
}
227227
}
228228
routeMete = new RouteMeta(route, element, RouteType.ACTIVITY, paramsType);

0 commit comments

Comments
 (0)