Log SoftExceptions when the legacy NativeModule system is used

Summary:
When ReactFeatureFlags.warnOnLegacyNativeModuleSystemUse is true, we will log a SoftException, whenever:
1. A Java/Cxx NativeModule is created by the legacy system.
2. Any method on the Java NativeModule is executed, including getConstants().

NOTE: Logs to CXXModule use incoming.

Changelog: [Internal]

Reviewed By: JoshuaGross

Differential Revision: D30252953

fbshipit-source-id: 570929624d0114bb298c593ba909e5cdbd54bd6c
This commit is contained in:
Ramanpreet Nara
2021-08-13 13:53:44 -07:00
committed by Facebook GitHub Bot
parent 2b427f8692
commit f93d95e079
2 changed files with 59 additions and 0 deletions
@@ -15,6 +15,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
import androidx.annotation.Nullable;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.config.ReactFeatureFlags;
import com.facebook.systrace.Systrace;
import com.facebook.systrace.SystraceMessage;
import java.lang.reflect.Method;
@@ -43,6 +44,7 @@ public class JavaModuleWrapper {
private final ModuleHolder mModuleHolder;
private final ArrayList<NativeModule.NativeMethod> mMethods;
private final ArrayList<MethodDescriptor> mDescs;
private static final String TAG = JavaModuleWrapper.class.getSimpleName();
public JavaModuleWrapper(JSInstance jsInstance, ModuleHolder moduleHolder) {
mJSInstance = jsInstance;
@@ -113,6 +115,17 @@ public class JavaModuleWrapper {
@DoNotStrip
public @Nullable NativeMap getConstants() {
if (ReactFeatureFlags.warnOnLegacyNativeModuleSystemUse) {
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException(
"Calling getConstants() on Java NativeModule (name = \""
+ mModuleHolder.getName()
+ "\", className = "
+ mModuleHolder.getClassName()
+ ")."));
}
if (!mModuleHolder.getHasConstants()) {
return null;
}
@@ -144,10 +157,34 @@ public class JavaModuleWrapper {
@DoNotStrip
public void invoke(int methodId, ReadableNativeArray parameters) {
if (ReactFeatureFlags.warnOnLegacyNativeModuleSystemUse) {
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException(
"Calling method on Java NativeModule (name = \""
+ mModuleHolder.getName()
+ "\", className = "
+ mModuleHolder.getClassName()
+ ")."));
}
if (mMethods == null || methodId >= mMethods.size()) {
return;
}
if (ReactFeatureFlags.warnOnLegacyNativeModuleSystemUse) {
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException(
"Calling "
+ mDescs.get(methodId).name
+ "() on Java NativeModule (name = \""
+ mModuleHolder.getName()
+ "\", className = "
+ mModuleHolder.getClassName()
+ ")."));
}
mMethods.get(methodId).invoke(mJSInstance, parameters);
}
}
@@ -21,6 +21,7 @@ public class NativeModuleRegistry {
private final ReactApplicationContext mReactApplicationContext;
private final Map<String, ModuleHolder> mModules;
private final String TAG = NativeModuleRegistry.class.getSimpleName();
public NativeModuleRegistry(
ReactApplicationContext reactApplicationContext, Map<String, ModuleHolder> modules) {
@@ -41,6 +42,17 @@ public class NativeModuleRegistry {
ArrayList<JavaModuleWrapper> javaModules = new ArrayList<>();
for (Map.Entry<String, ModuleHolder> entry : mModules.entrySet()) {
if (!entry.getValue().isCxxModule()) {
if (ReactFeatureFlags.warnOnLegacyNativeModuleSystemUse) {
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException(
"Registering legacy NativeModule: Java NativeModule (name = \""
+ entry.getValue().getName()
+ "\", className = "
+ entry.getValue().getClassName()
+ ")."));
}
javaModules.add(new JavaModuleWrapper(jsInstance, entry.getValue()));
}
}
@@ -51,6 +63,16 @@ public class NativeModuleRegistry {
ArrayList<ModuleHolder> cxxModules = new ArrayList<>();
for (Map.Entry<String, ModuleHolder> entry : mModules.entrySet()) {
if (entry.getValue().isCxxModule()) {
if (ReactFeatureFlags.warnOnLegacyNativeModuleSystemUse) {
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException(
"Registering legacy NativeModule: Cxx NativeModule (name = \""
+ entry.getValue().getName()
+ "\", className = "
+ entry.getValue().getClassName()
+ ")."));
}
cxxModules.add(entry.getValue());
}
}