mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-03 22:48:25 +08:00
Prevent class loading for lazy native modules
Summary: When native modules use `LazyReactPackage`, the modules themselves are not initialized. However, they still use the class names, causing the classes to load. This diff removes the need to perform any class loads. Any properties of the classes that are required are now populated in the `ReactModuleInfo` of that class. Note that this diff itself does not prevent class loading since any references to `*.class` in `LazyReactpackage` needs to be removed in a consequent diff Reviewed By: achen1 Differential Revision: D8950025 fbshipit-source-id: 80ddf7e1f33bf2af0db1bd262069795de77ec611
This commit is contained in:
committed by
Facebook Github Bot
parent
d891ee1dee
commit
c8e000b19a
@@ -32,8 +32,8 @@ public class NativeModuleRegistryBuilder {
|
||||
private final ReactInstanceManager mReactInstanceManager;
|
||||
private final boolean mLazyNativeModulesEnabled;
|
||||
|
||||
private final Map<Class<? extends NativeModule>, ModuleHolder> mModules = new HashMap<>();
|
||||
private final Map<String, Class<? extends NativeModule>> namesToType = new HashMap<>();
|
||||
private final Map<String, ModuleHolder> mModules = new HashMap<>();
|
||||
private final Map<String,String> namesToType = new HashMap<>();
|
||||
|
||||
public NativeModuleRegistryBuilder(
|
||||
ReactApplicationContext reactApplicationContext,
|
||||
@@ -53,16 +53,10 @@ public class NativeModuleRegistryBuilder {
|
||||
lazyReactPackage.getReactModuleInfoProvider().getReactModuleInfos();
|
||||
|
||||
for (ModuleSpec moduleSpec : moduleSpecs) {
|
||||
Class<? extends NativeModule> type = moduleSpec.getType();
|
||||
ReactModuleInfo reactModuleInfo = reactModuleInfoMap.get(type.getCanonicalName());
|
||||
String className = moduleSpec.getClassName();
|
||||
ReactModuleInfo reactModuleInfo = reactModuleInfoMap.get(className);
|
||||
ModuleHolder moduleHolder;
|
||||
if (reactModuleInfo == null) {
|
||||
if (BaseJavaModule.class.isAssignableFrom(type)) {
|
||||
throw new IllegalStateException(
|
||||
"Native Java module "
|
||||
+ type.getSimpleName()
|
||||
+ " should be annotated with @ReactModule and added to a @ReactModuleList.");
|
||||
}
|
||||
NativeModule module;
|
||||
ReactMarker.logMarker(
|
||||
ReactMarkerConstants.CREATE_MODULE_START,
|
||||
@@ -78,7 +72,7 @@ public class NativeModuleRegistryBuilder {
|
||||
}
|
||||
|
||||
String name = moduleHolder.getName();
|
||||
putModuleTypeAndHolderToModuleMaps(type, name, moduleHolder);
|
||||
putModuleTypeAndHolderToModuleMaps(className, name, moduleHolder);
|
||||
}
|
||||
} else {
|
||||
FLog.d(
|
||||
@@ -103,20 +97,20 @@ public class NativeModuleRegistryBuilder {
|
||||
public void addNativeModule(NativeModule nativeModule) {
|
||||
String name = nativeModule.getName();
|
||||
Class<? extends NativeModule> type = nativeModule.getClass();
|
||||
putModuleTypeAndHolderToModuleMaps(type, name, new ModuleHolder(nativeModule));
|
||||
putModuleTypeAndHolderToModuleMaps(type.getName(), name, new ModuleHolder(nativeModule));
|
||||
}
|
||||
|
||||
private void putModuleTypeAndHolderToModuleMaps(
|
||||
Class<? extends NativeModule> type, String underName, ModuleHolder moduleHolder)
|
||||
String className, String underName, ModuleHolder moduleHolder)
|
||||
throws IllegalStateException {
|
||||
if (namesToType.containsKey(underName)) {
|
||||
Class<? extends NativeModule> existingNativeModule = namesToType.get(underName);
|
||||
String existingNativeModule = namesToType.get(underName);
|
||||
if (!moduleHolder.getCanOverrideExistingModule()) {
|
||||
throw new IllegalStateException(
|
||||
"Native module "
|
||||
+ type.getSimpleName()
|
||||
+ className
|
||||
+ " tried to override "
|
||||
+ existingNativeModule.getSimpleName()
|
||||
+ existingNativeModule
|
||||
+ " for module name "
|
||||
+ underName
|
||||
+ ". Check the getPackages() method in MainApplication.java, it might be "
|
||||
@@ -127,14 +121,14 @@ public class NativeModuleRegistryBuilder {
|
||||
mModules.remove(existingNativeModule);
|
||||
}
|
||||
|
||||
namesToType.put(underName, type);
|
||||
mModules.put(type, moduleHolder);
|
||||
namesToType.put(underName, className);
|
||||
mModules.put(className, moduleHolder);
|
||||
}
|
||||
|
||||
public NativeModuleRegistry build() {
|
||||
ArrayList<ModuleHolder> batchCompleteListenerModules = new ArrayList<>();
|
||||
for (Map.Entry<Class<? extends NativeModule>, ModuleHolder> entry : mModules.entrySet()) {
|
||||
if (OnBatchCompleteListener.class.isAssignableFrom(entry.getKey())) {
|
||||
for (Map.Entry<String, ModuleHolder> entry : mModules.entrySet()) {
|
||||
if (entry.getValue().hasOnBatchCompleteListener()) {
|
||||
batchCompleteListenerModules.add(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user