don't allow fallback implementation when Lazy Native modules is enabled

Reviewed By: achen1

Differential Revision: D4019360

fbshipit-source-id: af5fffd1e80cdf99ff9af743eafff1412cac8e58
This commit is contained in:
Aaron Chiu
2016-10-20 05:31:49 -07:00
committed by Facebook Github Bot
parent fb4f34bc9b
commit e16251b46d
5 changed files with 59 additions and 55 deletions

View File

@@ -940,15 +940,21 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
.flush();
if (mLazyNativeModulesEnabled && reactPackage instanceof LazyReactPackage) {
LazyReactPackage lazyReactPackage = (LazyReactPackage) reactPackage;
if (addReactModuleInfos(lazyReactPackage, reactContext, moduleSpecs, reactModuleInfoMap)) {
moduleSpecs.addAll(lazyReactPackage.getNativeModules(reactContext));
ReactModuleInfoProvider instance = lazyReactPackage.getReactModuleInfoProvider();
Map<Class, ReactModuleInfo> map = instance.getReactModuleInfos();
if (!map.isEmpty()) {
reactModuleInfoMap.putAll(map);
}
moduleSpecs.addAll(lazyReactPackage.getNativeModules(reactContext));
} else {
FLog.d(
ReactConstants.TAG,
reactPackage.getClass().getSimpleName() +
" is not a LazyReactPackage, falling back to old version");
addEagerModuleProviders(reactPackage, reactContext, moduleSpecs);
" is not a LazyReactPackage, falling back to old version.");
for (NativeModule nativeModule : reactPackage.createNativeModules(reactContext)) {
moduleSpecs.add(
new ModuleSpec(nativeModule.getClass(), new EagerModuleProvider(nativeModule)));
}
}
for (Class<? extends JavaScriptModule> jsModuleClass : reactPackage.createJSModules()) {
@@ -957,55 +963,6 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
private boolean addReactModuleInfos(
LazyReactPackage lazyReactPackage,
ReactApplicationContext reactApplicationContext,
List<ModuleSpec> moduleSpecs,
Map<Class, ReactModuleInfo> reactModuleInfoMap) {
Class<?> reactModuleInfoProviderClass = null;
try {
reactModuleInfoProviderClass = Class.forName(
lazyReactPackage.getClass().getCanonicalName() + "$$ReactModuleInfoProvider");
} catch (ClassNotFoundException e) {
FLog.w(
TAG,
"Could not find generated ReactModuleInfoProvider for " + lazyReactPackage.getClass());
// Fallback to non-lazy method.
addEagerModuleProviders(lazyReactPackage, reactApplicationContext, moduleSpecs);
return false;
}
if (reactModuleInfoProviderClass != null) {
ReactModuleInfoProvider instance;
try {
instance = (ReactModuleInfoProvider) reactModuleInfoProviderClass.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(
"Unable to instantiate ReactModuleInfoProvider for " + lazyReactPackage.getClass(),
e);
} catch (IllegalAccessException e) {
throw new RuntimeException(
"Unable to instantiate ReactModuleInfoProvider for " + lazyReactPackage.getClass(),
e);
}
Map<Class, ReactModuleInfo> map = instance.getReactModuleInfos();
if (!map.isEmpty()) {
reactModuleInfoMap.putAll(map);
}
}
return true;
}
private void addEagerModuleProviders(
ReactPackage reactPackage,
ReactApplicationContext reactApplicationContext,
List<ModuleSpec> moduleSpecs) {
for (NativeModule nativeModule : reactPackage.createNativeModules(reactApplicationContext)) {
moduleSpecs.add(
new ModuleSpec(nativeModule.getClass(), new EagerModuleProvider(nativeModule)));
}
}
private void moveReactContextToCurrentLifecycleState() {
if (mLifecycleState == LifecycleState.RESUMED) {
moveToResumedLifecycleState(true);