Better error message when using getNativeModule(Class)

Summary:
Some of the native modules are accessed using `getNativeModule()` may not have an `ReactModule` annotation. As a result, such native modules cannot be found.

This diff adds a better error message, and also adds the annotation to Mobile Home's `PushNotificationManager` that is accessed using the `getNativeModule()`

Differential Revision: D9697256

fbshipit-source-id: 2e2f7d2f1eb705c9c515ff40d9acbc53055c26d8
This commit is contained in:
Ram N 2018-09-06 23:45:05 -07:00 committed by Facebook Github Bot
parent 4b15eb53ec
commit 6da93a302e
1 changed files with 6 additions and 2 deletions

View File

@ -10,6 +10,7 @@ package com.facebook.react.bridge;
import com.facebook.infer.annotation.Assertions; import com.facebook.infer.annotation.Assertions;
import com.facebook.react.module.annotations.ReactModule; import com.facebook.react.module.annotations.ReactModule;
import com.facebook.systrace.Systrace; import com.facebook.systrace.Systrace;
import java.lang.annotation.Annotation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -132,9 +133,12 @@ public class NativeModuleRegistry {
} }
public <T extends NativeModule> T getModule(Class<T> moduleInterface) { public <T extends NativeModule> T getModule(Class<T> moduleInterface) {
String name = moduleInterface.getAnnotation(ReactModule.class).name(); ReactModule annotation = moduleInterface.getAnnotation(ReactModule.class);
if (annotation == null) {
throw new IllegalArgumentException("Could not find @ReactModule annotation in class " + moduleInterface.getName());
}
return (T) Assertions.assertNotNull( return (T) Assertions.assertNotNull(
mModules.get(name), moduleInterface.getSimpleName()).getModule(); mModules.get(annotation.name()), annotation.name() + " could not be found. Is it defined in " + moduleInterface.getName()).getModule();
} }
public List<NativeModule> getAllModules() { public List<NativeModule> getAllModules() {