-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from alibaba/develop
Fix autowired performance issues
- Loading branch information
Showing
9 changed files
with
93 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
arouter-api/src/main/java/com/alibaba/android/arouter/core/AutowiredServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
arouter-api/src/main/java/com/alibaba/android/arouter/facade/service/AutowiredService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters