Fix LazyReactPackage in OSS

Summary:
In OSS, during gradle build, the ReactModuleSpecProcess annotation-processor does not run. As a result, the `ReactModuleInfo` that we need for CoreReactPackage is not generated, resulting in a runtime error.

The fix is to make LazyReactPackage revert to what it was doing earlier. In `NativeModuleRegistryBuilder`, if we dont find `ReactModuleInfo` for any `ModuleSpec`, we eagerly instantiate the module and get all the `ReactModuleInfo` from it. By returning an emoy collection if the class is not available, we force the modules in `CoreReactPackage` to use this codepath instead.

The alternate fix would be to ensure that the annotation processor runs in gradle/OSS. However, the annotation processor will be removed eventually in the future, and we will also be move to generating them for JS, so that work will soon be irrelevant.

Reviewed By: fkgozali, achen1

Differential Revision: D9130517

fbshipit-source-id: 469cf0e32a2f3650f098547667b3cd09a63eb1a0
This commit is contained in:
Ram N 2018-08-02 12:02:36 -07:00 committed by Facebook Github Bot
parent 3f89dd2db6
commit 42146a7a4a
2 changed files with 17 additions and 7 deletions

View File

@ -7,20 +7,21 @@
package com.facebook.react;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
import com.facebook.react.bridge.ModuleSpec;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMarker;
import com.facebook.react.bridge.ReactMarkerConstants;
import com.facebook.react.module.model.ReactModuleInfo;
import com.facebook.react.module.model.ReactModuleInfoProvider;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.systrace.SystraceMessage;
import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* React package supporting lazy creation of native modules.
@ -36,7 +37,16 @@ public abstract class LazyReactPackage implements ReactPackage {
reactModuleInfoProviderClass = Class.forName(
lazyReactPackage.getClass().getCanonicalName() + "$$ReactModuleInfoProvider");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
// In OSS case, when the annotation processor does not run, we fall back to non-lazy mode
// For this, we simply return an empty moduleMap.
// NativeModuleRegistryBuilder will eagerly get all the modules, and get the info from the
// modules directly
return new ReactModuleInfoProvider() {
@Override
public Map<String, ReactModuleInfo> getReactModuleInfos() {
return Collections.emptyMap();
}
};
}
if (reactModuleInfoProviderClass == null) {

View File

@ -60,7 +60,7 @@ public class NativeModuleRegistryBuilder {
NativeModule module;
ReactMarker.logMarker(
ReactMarkerConstants.CREATE_MODULE_START,
moduleSpec.getType().getName());
moduleSpec.getClassName());
try {
module = moduleSpec.getProvider().get();
} finally {