Channge interface to getNativeModule to use strings instead of classes

Summary: Now that NativeModules are stored based on String keys instead of classnames, the old innterface to getNativeModules(Class moduleInterface) is deprecated. This interface is also incorrect since a native module with the same name may be overridden, causing issues. Getting native modules by name is also similar to what JavaScript does

Reviewed By: achen1

Differential Revision: D9697827

fbshipit-source-id: ff832bd2ea5e1c7cfe7d8c0c3a66f0d755b2c354
This commit is contained in:
Ram N
2018-09-19 19:53:00 -07:00
committed by Facebook Github Bot
parent 5f31a1082b
commit 0cd3994f1a
3 changed files with 57 additions and 34 deletions
@@ -23,8 +23,11 @@ import com.facebook.react.bridge.queue.ReactQueueConfigurationImpl;
import com.facebook.react.bridge.queue.ReactQueueConfigurationSpec;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.systrace.Systrace;
import com.facebook.systrace.TraceListener;
import java.lang.annotation.Annotation;
import java.lang.annotation.Native;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
@@ -422,13 +425,25 @@ public class CatalystInstanceImpl implements CatalystInstance {
@Override
public <T extends NativeModule> boolean hasNativeModule(Class<T> nativeModuleInterface) {
return mNativeModuleRegistry.hasModule(nativeModuleInterface);
return mNativeModuleRegistry.hasModule(getNameFromAnnotation(nativeModuleInterface));
}
// This is only ever called with UIManagerModule or CurrentViewerModule.
@Override
public <T extends NativeModule> T getNativeModule(Class<T> nativeModuleInterface) {
return mNativeModuleRegistry.getModule(nativeModuleInterface);
return (T) mNativeModuleRegistry.getModule(getNameFromAnnotation(nativeModuleInterface));
}
@Override
public NativeModule getNativeModule(String moduleName) {
return mNativeModuleRegistry.getModule(moduleName);
}
private <T extends NativeModule> String getNameFromAnnotation(Class<T> nativeModuleInterface){
ReactModule annotation = nativeModuleInterface.getAnnotation(ReactModule.class);
if (annotation == null) {
throw new IllegalArgumentException("Could not find @ReactModule annotation in " + nativeModuleInterface.getCanonicalName());
}
return annotation.name();
}
// This is only used by com.facebook.react.modules.common.ModuleDataCleaner