Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[WEEX-210][android] Weex Auto Scan Component And Module Discover Mech…
Browse files Browse the repository at this point in the history
…anism
  • Loading branch information
jianbai.gbj committed Feb 11, 2018
1 parent de40fe1 commit b1ca035
Show file tree
Hide file tree
Showing 12 changed files with 568 additions and 7 deletions.
36 changes: 36 additions & 0 deletions android/playground/app/src/main/assets/weex_config_search.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"modules":[
{
"name" : "tool",
"className":"com.taobao.weex.ui.module.WXModalUIModule",
"methods":[
"toast",
"alert",
"confirm",
"prompt"
]
},
{
"name" : "toolError",
"className":"com.taobao.weex.ui.module.WXModalUIModuleError",
"methods":[
"toast",
"alert"
]
}
],
"components":[
{
"name":"diva",
"className":"com.taobao.weex.ui.component.WXDiv",
"appendTree":false,
"methods":[]
},
{
"name":"divnot",
"className":"com.taobao.weex.ui.component.WXDivNot",
"appendTree":false,
"methods":[]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public void onCreate() {
WXSDKEngine.registerModule("titleBar", WXTitleBar.class);

WXSDKEngine.registerModule("wsonTest", WXWsonTestModule.class);


/**
* override default image tag
* WXSDKEngine.registerComponent("image", FrescoImageComponent.class);
Expand Down
21 changes: 20 additions & 1 deletion android/sdk/src/main/java/com/taobao/weex/InitConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.taobao.weex;

import com.taobao.weex.adapter.ClassLoaderAdapter;
import com.taobao.weex.adapter.IDrawableLoader;
import com.taobao.weex.adapter.IWXHttpAdapter;
import com.taobao.weex.adapter.IWXImgLoaderAdapter;
Expand All @@ -42,6 +43,7 @@ public class InitConfig {
private IWebSocketAdapterFactory webSocketAdapterFactory;
private IWXJSExceptionAdapter mJSExceptionAdapter;
private String framework;
private ClassLoaderAdapter classLoaderAdapter;

public IWXHttpAdapter getHttpAdapter() {
return httpAdapter;
Expand Down Expand Up @@ -79,6 +81,15 @@ public IWebSocketAdapterFactory getWebSocketAdapterFactory() {
return webSocketAdapterFactory;
}

public ClassLoaderAdapter getClassLoaderAdapter() {
return classLoaderAdapter;
}

public InitConfig setClassLoaderAdapter(ClassLoaderAdapter classLoaderAdapter) {
this.classLoaderAdapter = classLoaderAdapter;
return this;
}

public IWXJSExceptionAdapter getJSExceptionAdapter() {
return mJSExceptionAdapter;
}
Expand All @@ -97,6 +108,8 @@ public static class Builder{
IWXJSExceptionAdapter mJSExceptionAdapter;
String framework;
IWebSocketAdapterFactory webSocketAdapterFactory;
ClassLoaderAdapter classLoaderAdapter;

public Builder(){

}
Expand Down Expand Up @@ -151,6 +164,11 @@ public Builder setWebSocketAdapterFactory(IWebSocketAdapterFactory factory) {
return this;
}

public Builder setClassLoaderAdapter(ClassLoaderAdapter classLoaderAdapter) {
this.classLoaderAdapter = classLoaderAdapter;
return this;
}

public InitConfig build(){
InitConfig config = new InitConfig();
config.httpAdapter = this.httpAdapter;
Expand All @@ -162,7 +180,8 @@ public InitConfig build(){
config.framework=this.framework;
config.mURIAdapter = this.mURIAdapter;
config.webSocketAdapterFactory = this.webSocketAdapterFactory;
config.mJSExceptionAdapter=this.mJSExceptionAdapter;
config.mJSExceptionAdapter= this.mJSExceptionAdapter;
config.classLoaderAdapter = this.classLoaderAdapter;
return config;
}
}
Expand Down
5 changes: 4 additions & 1 deletion android/sdk/src/main/java/com/taobao/weex/WXSDKEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import com.taobao.weex.ui.component.list.WXCell;
import com.taobao.weex.ui.component.list.WXListComponent;
import com.taobao.weex.ui.component.list.template.WXRecyclerTemplateList;
import com.taobao.weex.ui.config.AutoScanConfigRegister;
import com.taobao.weex.ui.module.WXLocaleModule;
import com.taobao.weex.ui.module.WXMetaModule;
import com.taobao.weex.ui.module.WXModalUIModule;
Expand Down Expand Up @@ -345,9 +346,11 @@ private static void register() {
} catch (WXException e) {
WXLogUtils.e("[WXSDKEngine] register:", e);
}
AutoScanConfigRegister.doScanConfig();
batchHelper.flush();
}


/**
*
* Register component. The registration is singleton in {@link WXSDKEngine} level
Expand Down Expand Up @@ -426,7 +429,7 @@ public static <T extends WXModule> boolean registerModuleWithFactory(String modu
return registerModule(moduleName, factory.getExternalModuleClass(moduleName,WXEnvironment.getApplication()),global);
}

private static <T extends WXModule> boolean registerModule(String moduleName, ModuleFactory factory, boolean global) throws WXException {
public static <T extends WXModule> boolean registerModule(String moduleName, ModuleFactory factory, boolean global) throws WXException {
return WXModuleManager.registerModule(moduleName, factory,global);
}

Expand Down
10 changes: 10 additions & 0 deletions android/sdk/src/main/java/com/taobao/weex/WXSDKManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.taobao.weex.adapter.ClassLoaderAdapter;
import com.taobao.weex.adapter.DefaultUriAdapter;
import com.taobao.weex.adapter.DefaultWXHttpAdapter;
import com.taobao.weex.adapter.ICrashInfoReporter;
Expand Down Expand Up @@ -86,6 +87,7 @@ public class WXSDKManager {
private IWXStorageAdapter mIWXStorageAdapter;
private IWXStatisticsListener mStatisticsListener;
private URIAdapter mURIAdapter;
private ClassLoaderAdapter mClassLoaderAdapter;
private IWebSocketAdapterFactory mIWebSocketAdapterFactory;
private ITracingAdapter mTracingAdapter;
private WXValidateProcessor mWXValidateProcessor;
Expand Down Expand Up @@ -345,6 +347,13 @@ public void setIWXJSExceptionAdapter(IWXJSExceptionAdapter IWXJSExceptionAdapter
return mURIAdapter;
}

public ClassLoaderAdapter getClassLoaderAdapter() {
if(mClassLoaderAdapter == null){
mClassLoaderAdapter = new ClassLoaderAdapter();
}
return mClassLoaderAdapter;
}

public IWXSoLoaderAdapter getIWXSoLoaderAdapter() {
return mIWXSoLoaderAdapter;
}
Expand All @@ -359,6 +368,7 @@ void setInitConfig(InitConfig config){
this.mIWebSocketAdapterFactory = config.getWebSocketAdapterFactory();
this.mIWXJSExceptionAdapter = config.getJSExceptionAdapter();
this.mIWXSoLoaderAdapter = config.getIWXSoLoaderAdapter();
this.mClassLoaderAdapter = config.getClassLoaderAdapter();
}

public IWXStorageAdapter getIWXStorageAdapter(){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.taobao.weex.adapter;

import android.content.Context;

import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.common.WXModule;
import com.taobao.weex.ui.component.WXComponent;
import com.taobao.weex.utils.WXLogUtils;

/**
* Created by furture on 2018/2/7.
* class loader adapter for load auto config class.
*/
public class ClassLoaderAdapter {

/**
* context is module class
* */
public Class<? extends WXModule> getModuleClass(String name, String className, Context context){
try {
return (Class<? extends WXModule>) context.getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

/**
* context is instance context
*/
public Class<? extends WXComponent> getComponentClass(String name, String className, WXSDKInstance instance) {
try {
return (Class<? extends WXComponent>) instance.getContext().getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.taobao.weex.dom.DOMAction;
import com.taobao.weex.dom.WXDomModule;
import com.taobao.weex.dom.action.Actions;
import com.taobao.weex.ui.config.ConfigModuleFactory;
import com.taobao.weex.ui.module.WXTimerModule;
import com.taobao.weex.utils.WXExceptionUtils;
import com.taobao.weex.utils.WXLogUtils;
Expand Down Expand Up @@ -217,7 +218,12 @@ private static WXModule findModule(String instanceId, String moduleStr,ModuleFac
wxModule = moduleMap.get(moduleStr);
if (wxModule == null) {
try {
wxModule = factory.buildInstance();
if(factory instanceof ConfigModuleFactory){
WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId);
wxModule = ((ConfigModuleFactory) factory).buildInstance(instance);
}else{
wxModule = factory.buildInstance();
}
wxModule.setModuleName(moduleStr);
} catch (Exception e) {
WXLogUtils.e(moduleStr + " module build instace failed.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ExternalLoaderComponentHolder implements IFComponentHolder {
private Class mClass;


public ExternalLoaderComponentHolder(String type,IExternalComponentGetter clzGetter) {
public ExternalLoaderComponentHolder(String type, IExternalComponentGetter clzGetter) {
this.mClzGetter = clzGetter;
mType = type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public class SimpleComponentHolder implements IFComponentHolder{
private Map<String, Invoker> mMethodInvokers;
private ComponentCreator mCreator;

static class ClazzComponentCreator implements ComponentCreator{
public static class ClazzComponentCreator implements ComponentCreator{

private Constructor<? extends WXComponent> mConstructor;
private final Class<? extends WXComponent> mCompClz;

ClazzComponentCreator(Class<? extends WXComponent> c){
public ClazzComponentCreator(Class<? extends WXComponent> c){
mCompClz = c;
}

Expand Down Expand Up @@ -135,7 +135,7 @@ private synchronized void generate(){
mMethodInvokers = methodPair.second;
}

static Pair<Map<String,Invoker>,Map<String,Invoker>> getMethods(Class clz){
public static Pair<Map<String,Invoker>,Map<String,Invoker>> getMethods(Class clz){
Map<String, Invoker> methods = new HashMap<>();
Map<String, Invoker> mInvokers = new HashMap<>();

Expand Down
Loading

0 comments on commit b1ca035

Please sign in to comment.