mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-03 22:48:25 +08:00
Summary: This change drops the year from the copyright headers and the LICENSE file. Reviewed By: yungsters Differential Revision: D9727774 fbshipit-source-id: df4fc1e4390733fe774b1a160dd41b4a3d83302a
66 lines
2.5 KiB
Java
66 lines
2.5 KiB
Java
// Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
// LICENSE file in the root directory of this source tree.
|
|
|
|
package com.facebook.react;
|
|
|
|
import com.facebook.react.bridge.ModuleHolder;
|
|
import com.facebook.react.bridge.NativeModuleRegistry;
|
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/** Helper class to build NativeModuleRegistry. */
|
|
public class NativeModuleRegistryBuilder {
|
|
|
|
private final ReactApplicationContext mReactApplicationContext;
|
|
private final ReactInstanceManager mReactInstanceManager;
|
|
|
|
private final Map<String, ModuleHolder> mModules = new HashMap<>();
|
|
|
|
public NativeModuleRegistryBuilder(
|
|
ReactApplicationContext reactApplicationContext, ReactInstanceManager reactInstanceManager) {
|
|
mReactApplicationContext = reactApplicationContext;
|
|
mReactInstanceManager = reactInstanceManager;
|
|
}
|
|
|
|
public void processPackage(ReactPackage reactPackage) {
|
|
// We use an iterable instead of an iterator here to ensure thread safety, and that this list
|
|
// cannot be modified
|
|
Iterable<ModuleHolder> moduleHolders;
|
|
if (reactPackage instanceof LazyReactPackage) {
|
|
moduleHolders =
|
|
((LazyReactPackage) reactPackage).getNativeModuleIterator(mReactApplicationContext);
|
|
} else if (reactPackage instanceof TurboReactPackage) {
|
|
moduleHolders =
|
|
((TurboReactPackage) reactPackage).getNativeModuleIterator(mReactApplicationContext);
|
|
} else {
|
|
moduleHolders =
|
|
ReactPackageHelper.getNativeModuleIterator(
|
|
reactPackage, mReactApplicationContext, mReactInstanceManager);
|
|
}
|
|
|
|
for (ModuleHolder moduleHolder : moduleHolders) {
|
|
String name = moduleHolder.getName();
|
|
if (mModules.containsKey(name)) {
|
|
ModuleHolder existingNativeModule = mModules.get(name);
|
|
if (!moduleHolder.getCanOverrideExistingModule()) {
|
|
throw new IllegalStateException(
|
|
"Native module "
|
|
+ name
|
|
+ " tried to override "
|
|
+ existingNativeModule.getClassName()
|
|
+ " for module name .Check the getPackages() method in MainApplication.java, it might be that module is being created twice. If this was your intention, set canOverrideExistingModule=true");
|
|
}
|
|
mModules.remove(existingNativeModule);
|
|
}
|
|
mModules.put(name, moduleHolder);
|
|
}
|
|
}
|
|
|
|
public NativeModuleRegistry build() {
|
|
return new NativeModuleRegistry(mReactApplicationContext, mModules);
|
|
}
|
|
}
|