Skip to content

Commit

Permalink
Merge pull request #38 from alibaba/develop
Browse files Browse the repository at this point in the history
Fix autowired performance issues
  • Loading branch information
zhi1ong authored Feb 28, 2017
2 parents 974b6da + 071b48d commit 8780ba5
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.alibaba.android.arouter.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
Expand Down Expand Up @@ -79,7 +77,7 @@ public void onClick(View v) {
ARouter.getInstance().build("/test/activity1")
.withString("name", "老王")
.withInt("age", 18)
.withBoolean("girl", true)
.withBoolean("boy", true)
.withLong("high", 180)
.withString("url", "https://a.b.c")
.navigation();
Expand Down
2 changes: 1 addition & 1 deletion arouter-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ext {
artifact = bintrayName
libraryName = 'ARouter sdk'
libraryDescription = 'A router for android'
libraryVersion = '1.0.6'
libraryVersion = '1.0.7'
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.alibaba.android.arouter.core;

import android.content.Context;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.service.AutowiredService;
import com.alibaba.android.arouter.facade.template.ISyringe;

import org.apache.commons.collections4.map.LRUMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.alibaba.android.arouter.utils.Consts.SUFFIX_AUTOWIRED;

/**
* Autowired service impl.
*
* @author zhilong <a href="mailto:zhilong.lzl@alibaba-inc.com">Contact me.</a>
* @version 1.0
* @since 2017/2/28 下午6:08
*/
@Route(path = "/arouter/service/autowired")
public class AutowiredServiceImpl implements AutowiredService {
private Map<String, ISyringe> classCache;
private List<String> blackList;

@Override
public void init(Context context) {
classCache = new LRUMap<>();
blackList = new ArrayList<>();
}

@Override
public void autowire(Object instance) {
String className = instance.getClass().getName();
try {
if (!blackList.contains(className)) {
ISyringe autowiredHelper = classCache.get(className);
if (null == autowiredHelper) { // No cache.
autowiredHelper = (ISyringe) Class.forName(instance.getClass().getName() + SUFFIX_AUTOWIRED).getConstructor().newInstance();
}
autowiredHelper.inject(instance);
classCache.put(className, autowiredHelper);
}
} catch (Exception ex) {
// ARouter.logger.error(TAG, "Autowired made exception, in class [" + className + "]");
blackList.add(className); // This instance need not autowired.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public synchronized static void completion(Postcard postcard) {
setValue(postcard,
params.getValue(),
params.getKey(),
resultMap.get(TextUtils.getRight(params.getKey())));
resultMap.get(params.getKey()));
}

// Save params name which need autoinject.
Expand Down Expand Up @@ -200,37 +200,35 @@ public synchronized static void completion(Postcard postcard) {
*/
private static void setValue(Postcard postcard, Integer typeDef, String key, String value) {
try {
String currentKey = TextUtils.getLeft(key);

if (null != typeDef) {
switch (typeDef) {
case Consts.DEF_BOOLEAN:
postcard.withBoolean(currentKey, Boolean.parseBoolean(value));
postcard.withBoolean(key, Boolean.parseBoolean(value));
break;
case Consts.DEF_BYTE:
postcard.withByte(currentKey, Byte.valueOf(value));
postcard.withByte(key, Byte.valueOf(value));
break;
case Consts.DEF_SHORT:
postcard.withShort(currentKey, Short.valueOf(value));
postcard.withShort(key, Short.valueOf(value));
break;
case Consts.DEF_INT:
postcard.withInt(currentKey, Integer.valueOf(value));
postcard.withInt(key, Integer.valueOf(value));
break;
case Consts.DEF_LONG:
postcard.withLong(currentKey, Long.valueOf(value));
postcard.withLong(key, Long.valueOf(value));
break;
case Consts.DEF_FLOAT:
postcard.withFloat(currentKey, Float.valueOf(value));
postcard.withFloat(key, Float.valueOf(value));
break;
case Consts.DEF_DOUBLE:
postcard.withDouble(currentKey, Double.valueOf(value));
postcard.withDouble(key, Double.valueOf(value));
break;
case Consts.DEF_STRING:
default:
postcard.withString(currentKey, value);
postcard.withString(key, value);
}
} else {
postcard.withString(currentKey, value);
postcard.withString(key, value);
}
} catch (Throwable ex) {
logger.warning(Consts.TAG, "LogisticsCenter setValue failed! " + ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.alibaba.android.arouter.facade.service;

import com.alibaba.android.arouter.facade.template.IProvider;

/**
* Service for autowired.
*
* @author zhilong <a href="mailto:zhilong.lzl@alibaba-inc.com">Contact me.</a>
* @version 1.0
* @since 2017/2/28 下午6:06
*/
public interface AutowiredService extends IProvider {

/**
* Autowired core.
* @param instance the instance who need autowired.
*/
void autowire(Object instance);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.facade.service.AutowiredService;
import com.alibaba.android.arouter.facade.service.DegradeService;
import com.alibaba.android.arouter.facade.service.InterceptorService;
import com.alibaba.android.arouter.facade.service.PathReplaceService;
import com.alibaba.android.arouter.facade.template.ILogger;
import com.alibaba.android.arouter.facade.template.ISyringe;
import com.alibaba.android.arouter.thread.DefaultPoolExecutor;
import com.alibaba.android.arouter.utils.Consts;
import com.alibaba.android.arouter.utils.DefaultLogger;
Expand All @@ -31,9 +31,6 @@
import java.lang.reflect.Method;
import java.util.concurrent.ThreadPoolExecutor;

import static com.alibaba.android.arouter.utils.Consts.SUFFIX_AUTOWIRED;
import static com.alibaba.android.arouter.utils.Consts.TAG;

/**
* ARouter core
*
Expand Down Expand Up @@ -169,12 +166,9 @@ static void setLogger(ILogger userLogger) {
}

static void inject(Object thiz) {
try {
Class autowiredClass = Class.forName(thiz.getClass().getName() + SUFFIX_AUTOWIRED);
ISyringe iSyringe = (ISyringe) autowiredClass.getConstructor().newInstance();
iSyringe.inject(thiz);
} catch (Exception ex) {
logger.error(TAG, "Autowired made exception, message [" + ex.getMessage() + "]");
AutowiredService autowiredService = ((AutowiredService) ARouter.getInstance().build("/arouter/service/autowired").navigation());
if (null != autowiredService) {
autowiredService.autowire(thiz);
}
}

Expand Down Expand Up @@ -245,7 +239,8 @@ private String extractGroup(String path) {
}

static void afterInit() {
interceptorService = ARouter.getInstance().navigation(InterceptorService.class); // Trigger interceptor init.
// Trigger interceptor init, use byName.
interceptorService = (InterceptorService) ARouter.getInstance().build("/arouter/service/interceptor").navigation();
}

protected <T> T navigation(Class<? extends T> service) {
Expand Down
2 changes: 1 addition & 1 deletion arouter-compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ext {
artifact = bintrayName
libraryName = 'ARouter compiler'
libraryDescription = 'A compiler for ARouter to find route'
libraryVersion = '1.0.2'
libraryVersion = '1.0.3'
}

compileJava {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -167,7 +168,7 @@ private void generateHelper() throws IOException, IllegalAccessException {
}

statment = buildStatement(statment, TypeUtils.typeExchange(element.asType()), isActivity);
injectMethodBuilder.addStatement(statment, fieldName);
injectMethodBuilder.addStatement(statment, StringUtils.isEmpty(fieldConfig.name()) ? fieldName : fieldConfig.name());

// Validater
if (fieldConfig.required() && !element.asType().getKind().isPrimitive()) { // Primitive wont be check.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private void parseRoutes(Set<? extends Element> routeElements) throws IOExceptio
if (field.getKind().isField() && field.getAnnotation(Autowired.class) != null && !typeUtil.isSubtype(field.asType(), iProvider)) {
// It must be field, then it has annotation, but it not be provider.
Autowired paramConfig = field.getAnnotation(Autowired.class);
paramsType.put(StringUtils.isEmpty(paramConfig.name()) ? field.getSimpleName().toString() : field.getSimpleName().toString() + "|" + paramConfig.name(), TypeUtils.typeExchange(field.asType()));
paramsType.put(StringUtils.isEmpty(paramConfig.name()) ? field.getSimpleName().toString() : paramConfig.name(), TypeUtils.typeExchange(field.asType()));
}
}
routeMete = new RouteMeta(route, element, RouteType.ACTIVITY, paramsType);
Expand Down

0 comments on commit 8780ba5

Please sign in to comment.