Skip to content

Commit 9263eb5

Browse files
RSNarafacebook-github-bot
authored andcommitted
Part 2: Make CatalystInstanceImpl.getNativeModule Nullable
Summary: This is the codemod portion of the parent diff. I modified all call-sites to `ReactContext.getNativeModule` to do a null check on the returned NativeModule. Changelog: [Android][Fixed] - Check if NativeModules returned from CatalystInstanceImpl.getNativeModule are null before using them. Reviewed By: JoshuaGross Differential Revision: D21272028 fbshipit-source-id: 6bd16c6bf30605f2dfdf4c481352063712965342
1 parent 1cef72a commit 9263eb5

20 files changed

+153
-72
lines changed

ReactAndroid/src/androidTest/java/com/facebook/react/testing/rule/ReactNativeTestRule.java

+5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public void run() {
9595
public void onReactContextInitialized(ReactContext reactContext) {
9696
final UIManagerModule uiManagerModule =
9797
reactContext.getCatalystInstance().getNativeModule(UIManagerModule.class);
98+
99+
if (uiManagerModule == null) {
100+
return;
101+
}
102+
98103
uiManagerModule
99104
.getUIImplementation()
100105
.setLayoutUpdateListener(

ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,9 @@ public void onBackPressed() {
470470
} else {
471471
DeviceEventManagerModule deviceEventManagerModule =
472472
reactContext.getNativeModule(DeviceEventManagerModule.class);
473-
deviceEventManagerModule.emitHardwareBackPressed();
473+
if (deviceEventManagerModule != null) {
474+
deviceEventManagerModule.emitHardwareBackPressed();
475+
}
474476
}
475477
}
476478

@@ -497,7 +499,9 @@ public void onNewIntent(Intent intent) {
497499
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))) {
498500
DeviceEventManagerModule deviceEventManagerModule =
499501
currentContext.getNativeModule(DeviceEventManagerModule.class);
500-
deviceEventManagerModule.emitNewIntentReceived(uri);
502+
if (deviceEventManagerModule != null) {
503+
deviceEventManagerModule.emitNewIntentReceived(uri);
504+
}
501505
}
502506
currentContext.onNewIntent(mCurrentActivity, intent);
503507
}
@@ -775,9 +779,12 @@ public void onConfigurationChanged(Context updatedContext, @Nullable Configurati
775779

776780
ReactContext currentReactContext = getCurrentReactContext();
777781
if (currentReactContext != null) {
778-
currentReactContext
779-
.getNativeModule(AppearanceModule.class)
780-
.onConfigurationChanged(updatedContext);
782+
AppearanceModule appearanceModule =
783+
currentReactContext.getNativeModule(AppearanceModule.class);
784+
785+
if (appearanceModule != null) {
786+
appearanceModule.onConfigurationChanged(updatedContext);
787+
}
781788
}
782789
}
783790

ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java

+18-10
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,12 @@ public void onChildStartedNativeGesture(MotionEvent androidEvent) {
200200
return;
201201
}
202202
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
203-
EventDispatcher eventDispatcher =
204-
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
205-
mJSTouchDispatcher.onChildStartedNativeGesture(androidEvent, eventDispatcher);
203+
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
204+
205+
if (uiManager != null) {
206+
EventDispatcher eventDispatcher = uiManager.getEventDispatcher();
207+
mJSTouchDispatcher.onChildStartedNativeGesture(androidEvent, eventDispatcher);
208+
}
206209
}
207210

208211
@Override
@@ -285,9 +288,12 @@ private void dispatchJSTouchEvent(MotionEvent event) {
285288
return;
286289
}
287290
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
288-
EventDispatcher eventDispatcher =
289-
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
290-
mJSTouchDispatcher.handleTouchEvent(event, eventDispatcher);
291+
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
292+
293+
if (uiManager != null) {
294+
EventDispatcher eventDispatcher = uiManager.getEventDispatcher();
295+
mJSTouchDispatcher.handleTouchEvent(event, eventDispatcher);
296+
}
291297
}
292298

293299
@Override
@@ -749,10 +755,12 @@ private void emitOrientationChanged(final int newRotation) {
749755
}
750756

751757
private void emitUpdateDimensionsEvent() {
752-
mReactInstanceManager
753-
.getCurrentReactContext()
754-
.getNativeModule(DeviceInfoModule.class)
755-
.emitUpdateDimensionsEvent();
758+
DeviceInfoModule deviceInfo =
759+
mReactInstanceManager.getCurrentReactContext().getNativeModule(DeviceInfoModule.class);
760+
761+
if (deviceInfo != null) {
762+
deviceInfo.emitUpdateDimensionsEvent();
763+
}
756764
}
757765

758766
private WritableMap createKeyboardEventPayload(

ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedModule.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ public void initialize() {
130130
// TODO T59412313 Implement this API on FabricUIManager to use in bridgeless mode
131131
if (reactApplicationContext != null && !reactApplicationContext.isBridgeless()) {
132132
reactApplicationContext.addLifecycleEventListener(this);
133-
UIManagerModule uiManager = reactApplicationContext.getNativeModule(UIManagerModule.class);
133+
UIManagerModule uiManager =
134+
Assertions.assertNotNull(reactApplicationContext.getNativeModule(UIManagerModule.class));
134135
uiManager.addUIManagerListener(this);
135136
}
136137
}
@@ -191,7 +192,9 @@ private NativeAnimatedNodesManager getNodesManager() {
191192
ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn();
192193

193194
if (reactApplicationContext != null) {
194-
UIManagerModule uiManager = reactApplicationContext.getNativeModule(UIManagerModule.class);
195+
UIManagerModule uiManager =
196+
Assertions.assertNotNull(
197+
reactApplicationContext.getNativeModule(UIManagerModule.class));
195198
mNodesManager = new NativeAnimatedNodesManager(uiManager);
196199
}
197200
}

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java

+15-12
Original file line numberDiff line numberDiff line change
@@ -962,19 +962,22 @@ private void handleCaptureHeap(final Responder responder) {
962962
return;
963963
}
964964
JSCHeapCapture heapCapture = mCurrentContext.getNativeModule(JSCHeapCapture.class);
965-
heapCapture.captureHeap(
966-
mApplicationContext.getCacheDir().getPath(),
967-
new JSCHeapCapture.CaptureCallback() {
968-
@Override
969-
public void onSuccess(File capture) {
970-
responder.respond(capture.toString());
971-
}
972965

973-
@Override
974-
public void onFailure(JSCHeapCapture.CaptureException error) {
975-
responder.error(error.toString());
976-
}
977-
});
966+
if (heapCapture != null) {
967+
heapCapture.captureHeap(
968+
mApplicationContext.getCacheDir().getPath(),
969+
new JSCHeapCapture.CaptureCallback() {
970+
@Override
971+
public void onSuccess(File capture) {
972+
responder.respond(capture.toString());
973+
}
974+
975+
@Override
976+
public void onFailure(JSCHeapCapture.CaptureException error) {
977+
responder.error(error.toString());
978+
}
979+
});
980+
}
978981
}
979982

980983
private void updateLastErrorInfo(

ReactAndroid/src/main/java/com/facebook/react/fabric/FabricJSIModuleProvider.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.facebook.react.fabric;
99

1010
import androidx.annotation.NonNull;
11+
import com.facebook.infer.annotation.Assertions;
1112
import com.facebook.react.bridge.JSIModuleProvider;
1213
import com.facebook.react.bridge.JavaScriptContextHolder;
1314
import com.facebook.react.bridge.ReactApplicationContext;
@@ -83,7 +84,8 @@ public UIManager get() {
8384
private FabricUIManager createUIManager(@NonNull EventBeatManager eventBeatManager) {
8485
Systrace.beginSection(
8586
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "FabricJSIModuleProvider.createUIManager");
86-
UIManagerModule nativeModule = mReactApplicationContext.getNativeModule(UIManagerModule.class);
87+
UIManagerModule nativeModule =
88+
Assertions.assertNotNull(mReactApplicationContext.getNativeModule(UIManagerModule.class));
8789
EventDispatcher eventDispatcher = nativeModule.getEventDispatcher();
8890
FabricUIManager fabricUIManager =
8991
new FabricUIManager(

ReactAndroid/src/main/java/com/facebook/react/modules/debug/FpsDebugFrameCallback.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public FpsInfo(
7575

7676
public FpsDebugFrameCallback(ReactContext reactContext) {
7777
mReactContext = reactContext;
78-
mUIManagerModule = reactContext.getNativeModule(UIManagerModule.class);
78+
mUIManagerModule =
79+
Assertions.assertNotNull(reactContext.getNativeModule(UIManagerModule.class));
7980
mDidJSUpdateUiDuringFrameDetector = new DidJSUpdateUiDuringFrameDetector();
8081
}
8182

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIViewOperationQueue.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,14 @@ public EmitOnLayoutEventOperation(
113113

114114
@Override
115115
public void execute() {
116-
mReactApplicationContext
117-
.getNativeModule(UIManagerModule.class)
118-
.getEventDispatcher()
119-
.dispatchEvent(
120-
OnLayoutEvent.obtain(mTag, mScreenX, mScreenY, mScreenWidth, mScreenHeight));
116+
UIManagerModule uiManager = mReactApplicationContext.getNativeModule(UIManagerModule.class);
117+
118+
if (uiManager != null) {
119+
uiManager
120+
.getEventDispatcher()
121+
.dispatchEvent(
122+
OnLayoutEvent.obtain(mTag, mScreenX, mScreenY, mScreenWidth, mScreenHeight));
123+
}
121124
}
122125
}
123126

ReactAndroid/src/main/java/com/facebook/react/views/drawer/ReactDrawerLayoutManager.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ public ReactDrawerLayoutManager() {
5656

5757
@Override
5858
protected void addEventEmitters(ThemedReactContext reactContext, ReactDrawerLayout view) {
59-
view.addDrawerListener(
60-
new DrawerEventEmitter(
61-
view, reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()));
59+
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
60+
if (uiManager == null) {
61+
return;
62+
}
63+
64+
view.addDrawerListener(new DrawerEventEmitter(view, uiManager.getEventDispatcher()));
6265
}
6366

6467
@Override

ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,12 @@ public void setIdentifier(ReactModalHostView view, int value) {}
105105

106106
@Override
107107
protected void addEventEmitters(ThemedReactContext reactContext, final ReactModalHostView view) {
108-
final EventDispatcher dispatcher =
109-
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
108+
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
109+
if (uiManager == null) {
110+
return;
111+
}
112+
113+
final EventDispatcher dispatcher = uiManager.getEventDispatcher();
110114
view.setOnRequestCloseListener(
111115
new ReactModalHostView.OnRequestCloseListener() {
112116
@Override

ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,14 @@ private void updateFirstChildView() {
414414
new GuardedRunnable(reactContext) {
415415
@Override
416416
public void runGuarded() {
417-
(getReactContext())
418-
.getNativeModule(UIManagerModule.class)
419-
.updateNodeSize(viewTag, viewWidth, viewHeight);
417+
UIManagerModule uiManager =
418+
(getReactContext()).getNativeModule(UIManagerModule.class);
419+
420+
if (uiManager == null) {
421+
return;
422+
}
423+
424+
uiManager.updateNodeSize(viewTag, viewWidth, viewHeight);
420425
}
421426
});
422427
}

ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPickerManager.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@ protected void onAfterUpdateTransaction(ReactPicker view) {
6262

6363
@Override
6464
protected void addEventEmitters(final ThemedReactContext reactContext, final ReactPicker picker) {
65-
picker.setOnSelectListener(
66-
new PickerEventEmitter(
67-
picker, reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()));
65+
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
66+
67+
if (uiManager == null) {
68+
return;
69+
}
70+
71+
picker.setOnSelectListener(new PickerEventEmitter(picker, uiManager.getEventDispatcher()));
6872
}
6973

7074
@Override

ReactAndroid/src/main/java/com/facebook/react/views/slider/ReactSliderManager.java

+21-13
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,17 @@ public long measure(
9191
@Override
9292
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
9393
ReactContext reactContext = (ReactContext) seekbar.getContext();
94-
reactContext
95-
.getNativeModule(UIManagerModule.class)
96-
.getEventDispatcher()
97-
.dispatchEvent(
98-
new ReactSliderEvent(
99-
seekbar.getId(), ((ReactSlider) seekbar).toRealProgress(progress), fromUser));
94+
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
95+
96+
if (uiManager != null) {
97+
uiManager
98+
.getEventDispatcher()
99+
.dispatchEvent(
100+
new ReactSliderEvent(
101+
seekbar.getId(),
102+
((ReactSlider) seekbar).toRealProgress(progress),
103+
fromUser));
104+
}
100105
}
101106

102107
@Override
@@ -105,13 +110,16 @@ public void onStartTrackingTouch(SeekBar seekbar) {}
105110
@Override
106111
public void onStopTrackingTouch(SeekBar seekbar) {
107112
ReactContext reactContext = (ReactContext) seekbar.getContext();
108-
reactContext
109-
.getNativeModule(UIManagerModule.class)
110-
.getEventDispatcher()
111-
.dispatchEvent(
112-
new ReactSlidingCompleteEvent(
113-
seekbar.getId(),
114-
((ReactSlider) seekbar).toRealProgress(seekbar.getProgress())));
113+
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
114+
115+
if (uiManager != null) {
116+
uiManager
117+
.getEventDispatcher()
118+
.dispatchEvent(
119+
new ReactSlidingCompleteEvent(
120+
seekbar.getId(),
121+
((ReactSlider) seekbar).toRealProgress(seekbar.getProgress())));
122+
}
115123
}
116124
};
117125

ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,14 @@ public long measure(
8080
@Override
8181
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
8282
ReactContext reactContext = (ReactContext) buttonView.getContext();
83-
reactContext
84-
.getNativeModule(UIManagerModule.class)
83+
84+
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
85+
86+
if (uiManager == null) {
87+
return;
88+
}
89+
90+
uiManager
8591
.getEventDispatcher()
8692
.dispatchEvent(new ReactSwitchEvent(buttonView.getId(), isChecked));
8793
}

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import androidx.appcompat.widget.AppCompatTextView;
2626
import androidx.appcompat.widget.TintContextWrapper;
2727
import com.facebook.common.logging.FLog;
28+
import com.facebook.infer.annotation.Assertions;
2829
import com.facebook.react.bridge.Arguments;
2930
import com.facebook.react.bridge.ReactContext;
3031
import com.facebook.react.bridge.WritableArray;
@@ -118,7 +119,8 @@ protected void onLayout(
118119
}
119120

120121
ReactContext reactContext = getReactContext();
121-
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
122+
UIManagerModule uiManager =
123+
Assertions.assertNotNull(reactContext.getNativeModule(UIManagerModule.class));
122124

123125
Spanned text = (Spanned) getText();
124126
Layout layout = getLayout();

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,9 @@ private void setIntrinsicContentSize() {
609609
ReactContext reactContext = getReactContext(this);
610610
final ReactTextInputLocalData localData = new ReactTextInputLocalData(this);
611611
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
612-
uiManager.setViewLocalData(getId(), localData);
612+
if (uiManager != null) {
613+
uiManager.setViewLocalData(getId(), localData);
614+
}
613615
}
614616
}
615617

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.view.inputmethod.InputConnection;
1313
import android.view.inputmethod.InputConnectionWrapper;
1414
import androidx.annotation.Nullable;
15+
import com.facebook.infer.annotation.Assertions;
1516
import com.facebook.react.bridge.ReactContext;
1617
import com.facebook.react.uimanager.UIManagerModule;
1718
import com.facebook.react.uimanager.events.EventDispatcher;
@@ -62,7 +63,9 @@ class ReactEditTextInputConnectionWrapper extends InputConnectionWrapper {
6263
public ReactEditTextInputConnectionWrapper(
6364
InputConnection target, final ReactContext reactContext, final ReactEditText editText) {
6465
super(target, false);
65-
mEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
66+
mEventDispatcher =
67+
Assertions.assertNotNull(reactContext.getNativeModule(UIManagerModule.class))
68+
.getEventDispatcher();
6669
mEditText = editText;
6770
}
6871

0 commit comments

Comments
 (0)