最新黑马C/C++全栈培训第24期项目实战(附带源码+笔记)

//WXSDKEngine的init方法已经被弃用,weex的初始化需要使用下面这个initialize方法
 
public static void initialize(Application application,InitConfig config){
 
  synchronized (mLock) {
 
    //如果已经初始化过那么直接return
 
    if (mIsInit) {
 
      return;
 
    }
 
    long start = System.currentTimeMillis();
 
    WXEnvironment.sSDKInitStart = start;
 
    if(WXEnvironment.isApkDebugable()){
 
      WXEnvironment.sLogLevel = LogLevel.DEBUG;
 
    }else{
 
if(WXEnvironment.sApplication != null){
 
  WXEnvironment.sLogLevel = LogLevel.WARN;
 
}else {
 
  WXLogUtils.e(TAG,"WXEnvironment.sApplication is " + WXEnvironment.sApplication);
 
}
 
    }
 
    doInitInternal(application,config);
 
    WXEnvironment.sSDKInitInvokeTime = System.currentTimeMillis()-start;
 
    WXLogUtils.renderPerformanceLog("SDKInitInvokeTime", WXEnvironment.sSDKInitInvokeTime);
 
    //监测weex表现的的一个检测器的初始化
 
    WXPerformance.init();
 
 
 
    mIsInit = true;
 
  }
 
}
private static void doInitInternal(final Application application,final InitConfig config){
 
  //这里给WXEnvironment中的application赋值,方便后续使用
 
   WXEnvironment.sApplication = application;
 
if(application == null){//application为空的话报错
 
  WXLogUtils.e(TAG, " doInitInternal application is null");
 
  WXExceptionUtils.commitCriticalExceptionRT(null,
 
        WXErrorCode.WX_KEY_EXCEPTION_SDK_INIT,
 
        "doInitInternal",
 
        WXErrorCode.WX_KEY_EXCEPTION_SDK_INIT.getErrorMsg() + "WXEnvironment sApplication is null",
 
        null);
 
}
 
   //weex环境初始化标识设置为false
 
   WXEnvironment.JsFrameworkInit = false;
 
 
 
   //通过单例模式获取一个WXBridgeManager的唯一实例,使用WXBridgeManager开启一个安全的线程初始化weex框架。
 
   //开启安全线程的细节暂时不深究,这里主要看weex框架的初始化流程
 
   WXBridgeManager.getInstance().post(new Runnable() {
 
     @Override
 
     public void run() {
 
        //开始初始化时间
 
       long start = System.currentTimeMillis();
 
       //获取WXSDKManager实例
 
       WXSDKManager sm = WXSDKManager.getInstance();
 
       //使用sm监听weex框架初始化,我们可以通过WXSDKManager.registerStatisticsListener()
 
       //注册一个监听器
 
       sm.onSDKEngineInitialize();
 
       //设置weex框架配置项
 
       if(config != null ) {
 
         sm.setInitConfig(config);
 
       }
 
       //初始化so文件加载器,后两个参数分别为初始化时的加载器和监听器。
 
       WXSoInstallMgrSdk.init(application,
 
                             sm.getIWXSoLoaderAdapter(),
 
                             sm.getWXStatisticsListener());
 
       //加载so库文件,如不设置自定义的IWXSoLoaderAdapter,那么采用系统默认方式加载
 
       boolean isSoInitSuccess = WXSoInstallMgrSdk.initSo(V8_SO_NAME, 1, config!=null?config.getUtAdapter():null);
 
       if (!isSoInitSuccess) {//加载失败报错
 
     WXExceptionUtils.commitCriticalExceptionRT(null,
 
           WXErrorCode.WX_KEY_EXCEPTION_SDK_INIT,
 
           "doInitInternal",
 
           WXErrorCode.WX_KEY_EXCEPTION_SDK_INIT.getErrorMsg() + "isSoInit false",
 
           null);
 
 
 
 
 
         return;
 
       }
 
       //使用WXSDKManager初始化jsFramwork,就是讲weexSdk中assets下的js文件拷贝到缓存目录中,使用so的native方法执行
 
       sm.initScriptsFramework(config!=null?config.getFramework():null);
 
      
 
       //计算初始化所用时间
 
       WXEnvironment.sSDKInitExecuteTime = System.currentTimeMillis() - start;
 
       WXLogUtils.renderPerformanceLog("SDKInitExecuteTime", WXEnvironment.sSDKInitExecuteTime);
 
     }
 
   });
 
   //注册weexSDK内置的一系列Component和Module
 
   register();
 
}
public static synchronized boolean registerComponent(final String type, final IFComponentHolder holder, final Map<String, Object> componentInfo) throws WXException {
 
  if (holder == null || TextUtils.isEmpty(type)) {
 
    return false;
 
  }
 
  //execute task in js thread to make sure register order is same as the order invoke register method.
 
  //在js线程执行这个注册组件的任务,以确保我们注册组件的顺序与调用注册方法的顺序一致。
 
  WXBridgeManager.getInstance()
 
      .post(new Runnable() {
 
    @Override
 
    public void run() {
 
      try {
 
        Map<String, Object> registerInfo = componentInfo;
 
        if (registerInfo == null){
 
          registerInfo = new HashMap<>();
 
        }
 
 
 
        //设置注册信息 
 
        registerInfo.put("type",type);
 
        //holder是之前让大家留意的SimpleComponentHolder类
 
        registerInfo.put("methods",holder.getMethods());
 
        //生成原生组件映射
 
        registerNativeComponent(type, holder);
 
        //将组件注册信息传递到jsFramwork,生成js中解析vue中原生component是所需的Json映射
 
        registerJSComponent(registerInfo);
 
        //sComponentInfos是js解析需要使用的类,包含所有组件的注册信息
 
        sComponentInfos.add(registerInfo);
 
      } catch (WXException e) {
 
        WXLogUtils.e("register component error:", e);
 
      }
 
    }
 
  });
 
  return true;
 
}
public static Pair<Map<String,Invoker>,Map<String,Invoker>> getMethods(Class clz){
 
  Map<String, Invoker> methods = new HashMap<>();
 
  Map<String, Invoker> mInvokers = new HashMap<>();
 
 
  Annotation[] annotations;
 
  Annotation anno;
 
  try {
 
    for (Method method : clz.getMethods()) {
 
      try {
 
        annotations = method.getDeclaredAnnotations();
 
        for (int i = 0, annotationsCount = annotations.length;
 
             i < annotationsCount; ++i) {
 
          anno = annotations[i];
 
          if(anno == null){
 
            continue;
 
          }
 
          if (anno instanceof WXComponentProp) {
 
            String name = ((WXComponentProp) anno).name();
 
            methods.put(name, new MethodInvoker(method,true));
 
            break;
 
          }else if(anno instanceof JSMethod){
 
            JSMethod methodAnno = (JSMethod)anno;
 
            String name = methodAnno.alias();
 
            if(JSMethod.NOT_SET.equals(name)){
 
              name = method.getName();
 
            }
 
            mInvokers.put(name, new MethodInvoker(method,methodAnno.uiThread()));
 
            break;
 
          }
 
        }
 
      } catch (ArrayIndexOutOfBoundsException | IncompatibleClassChangeError e) {
 
        //ignore: getDeclaredAnnotations may throw this
 
      }
 
    }
 
  }catch (IndexOutOfBoundsException e){
 
    e.printStackTrace();
 
    //ignore: getMethods may throw this
 
  }
 
  return new Pair<>(methods,mInvokers);
 
}
//SimpleComponentHolder.java
 
@Override
 
public void loadIfNonLazy() {
 
//获取类中所有注解
 
  Annotation[] annotations = mClz.getDeclaredAnnotations();
 
  for (Annotation annotation :
 
    annotations) {
 
    if (annotation instanceof Component){
 
      if(!((Component) annotation).lazyload() && mMethodInvokers == null){
 
        //通过此方法获得组件类的方法和属性,并保存
 
        generate();
 
      }
 
      return;
 
    }
 
  }
 
}
 
private synchronized void generate(){
 
  if(WXEnvironment.isApkDebugable()) {
 
    WXLogUtils.d(TAG, "Generate Component:" + mClz.getSimpleName());
 
  }
 
  //通过getMethods()方法获取属性和方法信息
 
  Pair<Map<String, Invoker>, Map<String, Invoker>> methodPair = getMethods(mClz);
 
  //保存属性和方法信息到自身
 
  mPropertyInvokers = methodPair.first;
 
  mMethodInvokers = methodPair.second;
 
}
 
 

猜你喜欢

转载自blog.csdn.net/yhhh5468156/article/details/89785492
今日推荐