Skip to content

Commit f93d95e

Browse files
RSNarafacebook-github-bot
authored andcommitted
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
1 parent 2b427f8 commit f93d95e

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

ReactAndroid/src/main/java/com/facebook/react/bridge/JavaModuleWrapper.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import androidx.annotation.Nullable;
1717
import com.facebook.proguard.annotations.DoNotStrip;
18+
import com.facebook.react.config.ReactFeatureFlags;
1819
import com.facebook.systrace.Systrace;
1920
import com.facebook.systrace.SystraceMessage;
2021
import java.lang.reflect.Method;
@@ -43,6 +44,7 @@ public class MethodDescriptor {
4344
private final ModuleHolder mModuleHolder;
4445
private final ArrayList<NativeModule.NativeMethod> mMethods;
4546
private final ArrayList<MethodDescriptor> mDescs;
47+
private static final String TAG = JavaModuleWrapper.class.getSimpleName();
4648

4749
public JavaModuleWrapper(JSInstance jsInstance, ModuleHolder moduleHolder) {
4850
mJSInstance = jsInstance;
@@ -113,6 +115,17 @@ public List<MethodDescriptor> getMethodDescriptors() {
113115

114116
@DoNotStrip
115117
public @Nullable NativeMap getConstants() {
118+
if (ReactFeatureFlags.warnOnLegacyNativeModuleSystemUse) {
119+
ReactSoftExceptionLogger.logSoftException(
120+
TAG,
121+
new ReactNoCrashSoftException(
122+
"Calling getConstants() on Java NativeModule (name = \""
123+
+ mModuleHolder.getName()
124+
+ "\", className = "
125+
+ mModuleHolder.getClassName()
126+
+ ")."));
127+
}
128+
116129
if (!mModuleHolder.getHasConstants()) {
117130
return null;
118131
}
@@ -144,10 +157,34 @@ public List<MethodDescriptor> getMethodDescriptors() {
144157

145158
@DoNotStrip
146159
public void invoke(int methodId, ReadableNativeArray parameters) {
160+
if (ReactFeatureFlags.warnOnLegacyNativeModuleSystemUse) {
161+
ReactSoftExceptionLogger.logSoftException(
162+
TAG,
163+
new ReactNoCrashSoftException(
164+
"Calling method on Java NativeModule (name = \""
165+
+ mModuleHolder.getName()
166+
+ "\", className = "
167+
+ mModuleHolder.getClassName()
168+
+ ")."));
169+
}
170+
147171
if (mMethods == null || methodId >= mMethods.size()) {
148172
return;
149173
}
150174

175+
if (ReactFeatureFlags.warnOnLegacyNativeModuleSystemUse) {
176+
ReactSoftExceptionLogger.logSoftException(
177+
TAG,
178+
new ReactNoCrashSoftException(
179+
"Calling "
180+
+ mDescs.get(methodId).name
181+
+ "() on Java NativeModule (name = \""
182+
+ mModuleHolder.getName()
183+
+ "\", className = "
184+
+ mModuleHolder.getClassName()
185+
+ ")."));
186+
}
187+
151188
mMethods.get(methodId).invoke(mJSInstance, parameters);
152189
}
153190
}

ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModuleRegistry.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class NativeModuleRegistry {
2121

2222
private final ReactApplicationContext mReactApplicationContext;
2323
private final Map<String, ModuleHolder> mModules;
24+
private final String TAG = NativeModuleRegistry.class.getSimpleName();
2425

2526
public NativeModuleRegistry(
2627
ReactApplicationContext reactApplicationContext, Map<String, ModuleHolder> modules) {
@@ -41,6 +42,17 @@ private ReactApplicationContext getReactApplicationContext() {
4142
ArrayList<JavaModuleWrapper> javaModules = new ArrayList<>();
4243
for (Map.Entry<String, ModuleHolder> entry : mModules.entrySet()) {
4344
if (!entry.getValue().isCxxModule()) {
45+
if (ReactFeatureFlags.warnOnLegacyNativeModuleSystemUse) {
46+
ReactSoftExceptionLogger.logSoftException(
47+
TAG,
48+
new ReactNoCrashSoftException(
49+
"Registering legacy NativeModule: Java NativeModule (name = \""
50+
+ entry.getValue().getName()
51+
+ "\", className = "
52+
+ entry.getValue().getClassName()
53+
+ ")."));
54+
}
55+
4456
javaModules.add(new JavaModuleWrapper(jsInstance, entry.getValue()));
4557
}
4658
}
@@ -51,6 +63,16 @@ private ReactApplicationContext getReactApplicationContext() {
5163
ArrayList<ModuleHolder> cxxModules = new ArrayList<>();
5264
for (Map.Entry<String, ModuleHolder> entry : mModules.entrySet()) {
5365
if (entry.getValue().isCxxModule()) {
66+
if (ReactFeatureFlags.warnOnLegacyNativeModuleSystemUse) {
67+
ReactSoftExceptionLogger.logSoftException(
68+
TAG,
69+
new ReactNoCrashSoftException(
70+
"Registering legacy NativeModule: Cxx NativeModule (name = \""
71+
+ entry.getValue().getName()
72+
+ "\", className = "
73+
+ entry.getValue().getClassName()
74+
+ ")."));
75+
}
5476
cxxModules.add(entry.getValue());
5577
}
5678
}

0 commit comments

Comments
 (0)