Skip to content

Commit

Permalink
Part 2: Make CatalystInstanceImpl.getNativeModule Nullable
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RSNara authored and facebook-github-bot committed Apr 28, 2020
1 parent 1cef72a commit 9263eb5
Show file tree
Hide file tree
Showing 20 changed files with 153 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public void run() {
public void onReactContextInitialized(ReactContext reactContext) {
final UIManagerModule uiManagerModule =
reactContext.getCatalystInstance().getNativeModule(UIManagerModule.class);

if (uiManagerModule == null) {
return;
}

uiManagerModule
.getUIImplementation()
.setLayoutUpdateListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,9 @@ public void onBackPressed() {
} else {
DeviceEventManagerModule deviceEventManagerModule =
reactContext.getNativeModule(DeviceEventManagerModule.class);
deviceEventManagerModule.emitHardwareBackPressed();
if (deviceEventManagerModule != null) {
deviceEventManagerModule.emitHardwareBackPressed();
}
}
}

Expand All @@ -497,7 +499,9 @@ public void onNewIntent(Intent intent) {
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))) {
DeviceEventManagerModule deviceEventManagerModule =
currentContext.getNativeModule(DeviceEventManagerModule.class);
deviceEventManagerModule.emitNewIntentReceived(uri);
if (deviceEventManagerModule != null) {
deviceEventManagerModule.emitNewIntentReceived(uri);
}
}
currentContext.onNewIntent(mCurrentActivity, intent);
}
Expand Down Expand Up @@ -775,9 +779,12 @@ public void onConfigurationChanged(Context updatedContext, @Nullable Configurati

ReactContext currentReactContext = getCurrentReactContext();
if (currentReactContext != null) {
currentReactContext
.getNativeModule(AppearanceModule.class)
.onConfigurationChanged(updatedContext);
AppearanceModule appearanceModule =
currentReactContext.getNativeModule(AppearanceModule.class);

if (appearanceModule != null) {
appearanceModule.onConfigurationChanged(updatedContext);
}
}
}

Expand Down
28 changes: 18 additions & 10 deletions ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,12 @@ public void onChildStartedNativeGesture(MotionEvent androidEvent) {
return;
}
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
EventDispatcher eventDispatcher =
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
mJSTouchDispatcher.onChildStartedNativeGesture(androidEvent, eventDispatcher);
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);

if (uiManager != null) {
EventDispatcher eventDispatcher = uiManager.getEventDispatcher();
mJSTouchDispatcher.onChildStartedNativeGesture(androidEvent, eventDispatcher);
}
}

@Override
Expand Down Expand Up @@ -285,9 +288,12 @@ private void dispatchJSTouchEvent(MotionEvent event) {
return;
}
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
EventDispatcher eventDispatcher =
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
mJSTouchDispatcher.handleTouchEvent(event, eventDispatcher);
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);

if (uiManager != null) {
EventDispatcher eventDispatcher = uiManager.getEventDispatcher();
mJSTouchDispatcher.handleTouchEvent(event, eventDispatcher);
}
}

@Override
Expand Down Expand Up @@ -749,10 +755,12 @@ private void emitOrientationChanged(final int newRotation) {
}

private void emitUpdateDimensionsEvent() {
mReactInstanceManager
.getCurrentReactContext()
.getNativeModule(DeviceInfoModule.class)
.emitUpdateDimensionsEvent();
DeviceInfoModule deviceInfo =
mReactInstanceManager.getCurrentReactContext().getNativeModule(DeviceInfoModule.class);

if (deviceInfo != null) {
deviceInfo.emitUpdateDimensionsEvent();
}
}

private WritableMap createKeyboardEventPayload(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public void initialize() {
// TODO T59412313 Implement this API on FabricUIManager to use in bridgeless mode
if (reactApplicationContext != null && !reactApplicationContext.isBridgeless()) {
reactApplicationContext.addLifecycleEventListener(this);
UIManagerModule uiManager = reactApplicationContext.getNativeModule(UIManagerModule.class);
UIManagerModule uiManager =
Assertions.assertNotNull(reactApplicationContext.getNativeModule(UIManagerModule.class));
uiManager.addUIManagerListener(this);
}
}
Expand Down Expand Up @@ -191,7 +192,9 @@ private NativeAnimatedNodesManager getNodesManager() {
ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn();

if (reactApplicationContext != null) {
UIManagerModule uiManager = reactApplicationContext.getNativeModule(UIManagerModule.class);
UIManagerModule uiManager =
Assertions.assertNotNull(
reactApplicationContext.getNativeModule(UIManagerModule.class));
mNodesManager = new NativeAnimatedNodesManager(uiManager);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -962,19 +962,22 @@ private void handleCaptureHeap(final Responder responder) {
return;
}
JSCHeapCapture heapCapture = mCurrentContext.getNativeModule(JSCHeapCapture.class);
heapCapture.captureHeap(
mApplicationContext.getCacheDir().getPath(),
new JSCHeapCapture.CaptureCallback() {
@Override
public void onSuccess(File capture) {
responder.respond(capture.toString());
}

@Override
public void onFailure(JSCHeapCapture.CaptureException error) {
responder.error(error.toString());
}
});
if (heapCapture != null) {
heapCapture.captureHeap(
mApplicationContext.getCacheDir().getPath(),
new JSCHeapCapture.CaptureCallback() {
@Override
public void onSuccess(File capture) {
responder.respond(capture.toString());
}

@Override
public void onFailure(JSCHeapCapture.CaptureException error) {
responder.error(error.toString());
}
});
}
}

private void updateLastErrorInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.facebook.react.fabric;

import androidx.annotation.NonNull;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.JSIModuleProvider;
import com.facebook.react.bridge.JavaScriptContextHolder;
import com.facebook.react.bridge.ReactApplicationContext;
Expand Down Expand Up @@ -83,7 +84,8 @@ public UIManager get() {
private FabricUIManager createUIManager(@NonNull EventBeatManager eventBeatManager) {
Systrace.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "FabricJSIModuleProvider.createUIManager");
UIManagerModule nativeModule = mReactApplicationContext.getNativeModule(UIManagerModule.class);
UIManagerModule nativeModule =
Assertions.assertNotNull(mReactApplicationContext.getNativeModule(UIManagerModule.class));
EventDispatcher eventDispatcher = nativeModule.getEventDispatcher();
FabricUIManager fabricUIManager =
new FabricUIManager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public FpsInfo(

public FpsDebugFrameCallback(ReactContext reactContext) {
mReactContext = reactContext;
mUIManagerModule = reactContext.getNativeModule(UIManagerModule.class);
mUIManagerModule =
Assertions.assertNotNull(reactContext.getNativeModule(UIManagerModule.class));
mDidJSUpdateUiDuringFrameDetector = new DidJSUpdateUiDuringFrameDetector();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,14 @@ public EmitOnLayoutEventOperation(

@Override
public void execute() {
mReactApplicationContext
.getNativeModule(UIManagerModule.class)
.getEventDispatcher()
.dispatchEvent(
OnLayoutEvent.obtain(mTag, mScreenX, mScreenY, mScreenWidth, mScreenHeight));
UIManagerModule uiManager = mReactApplicationContext.getNativeModule(UIManagerModule.class);

if (uiManager != null) {
uiManager
.getEventDispatcher()
.dispatchEvent(
OnLayoutEvent.obtain(mTag, mScreenX, mScreenY, mScreenWidth, mScreenHeight));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ public ReactDrawerLayoutManager() {

@Override
protected void addEventEmitters(ThemedReactContext reactContext, ReactDrawerLayout view) {
view.addDrawerListener(
new DrawerEventEmitter(
view, reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()));
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
if (uiManager == null) {
return;
}

view.addDrawerListener(new DrawerEventEmitter(view, uiManager.getEventDispatcher()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ public void setIdentifier(ReactModalHostView view, int value) {}

@Override
protected void addEventEmitters(ThemedReactContext reactContext, final ReactModalHostView view) {
final EventDispatcher dispatcher =
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
if (uiManager == null) {
return;
}

final EventDispatcher dispatcher = uiManager.getEventDispatcher();
view.setOnRequestCloseListener(
new ReactModalHostView.OnRequestCloseListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,14 @@ private void updateFirstChildView() {
new GuardedRunnable(reactContext) {
@Override
public void runGuarded() {
(getReactContext())
.getNativeModule(UIManagerModule.class)
.updateNodeSize(viewTag, viewWidth, viewHeight);
UIManagerModule uiManager =
(getReactContext()).getNativeModule(UIManagerModule.class);

if (uiManager == null) {
return;
}

uiManager.updateNodeSize(viewTag, viewWidth, viewHeight);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ protected void onAfterUpdateTransaction(ReactPicker view) {

@Override
protected void addEventEmitters(final ThemedReactContext reactContext, final ReactPicker picker) {
picker.setOnSelectListener(
new PickerEventEmitter(
picker, reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()));
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);

if (uiManager == null) {
return;
}

picker.setOnSelectListener(new PickerEventEmitter(picker, uiManager.getEventDispatcher()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,17 @@ public long measure(
@Override
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
ReactContext reactContext = (ReactContext) seekbar.getContext();
reactContext
.getNativeModule(UIManagerModule.class)
.getEventDispatcher()
.dispatchEvent(
new ReactSliderEvent(
seekbar.getId(), ((ReactSlider) seekbar).toRealProgress(progress), fromUser));
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);

if (uiManager != null) {
uiManager
.getEventDispatcher()
.dispatchEvent(
new ReactSliderEvent(
seekbar.getId(),
((ReactSlider) seekbar).toRealProgress(progress),
fromUser));
}
}

@Override
Expand All @@ -105,13 +110,16 @@ public void onStartTrackingTouch(SeekBar seekbar) {}
@Override
public void onStopTrackingTouch(SeekBar seekbar) {
ReactContext reactContext = (ReactContext) seekbar.getContext();
reactContext
.getNativeModule(UIManagerModule.class)
.getEventDispatcher()
.dispatchEvent(
new ReactSlidingCompleteEvent(
seekbar.getId(),
((ReactSlider) seekbar).toRealProgress(seekbar.getProgress())));
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);

if (uiManager != null) {
uiManager
.getEventDispatcher()
.dispatchEvent(
new ReactSlidingCompleteEvent(
seekbar.getId(),
((ReactSlider) seekbar).toRealProgress(seekbar.getProgress())));
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ public long measure(
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ReactContext reactContext = (ReactContext) buttonView.getContext();
reactContext
.getNativeModule(UIManagerModule.class)

UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);

if (uiManager == null) {
return;
}

uiManager
.getEventDispatcher()
.dispatchEvent(new ReactSwitchEvent(buttonView.getId(), isChecked));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.TintContextWrapper;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableArray;
Expand Down Expand Up @@ -118,7 +119,8 @@ protected void onLayout(
}

ReactContext reactContext = getReactContext();
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
UIManagerModule uiManager =
Assertions.assertNotNull(reactContext.getNativeModule(UIManagerModule.class));

Spanned text = (Spanned) getText();
Layout layout = getLayout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,9 @@ private void setIntrinsicContentSize() {
ReactContext reactContext = getReactContext(this);
final ReactTextInputLocalData localData = new ReactTextInputLocalData(this);
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
uiManager.setViewLocalData(getId(), localData);
if (uiManager != null) {
uiManager.setViewLocalData(getId(), localData);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher;
Expand Down Expand Up @@ -62,7 +63,9 @@ class ReactEditTextInputConnectionWrapper extends InputConnectionWrapper {
public ReactEditTextInputConnectionWrapper(
InputConnection target, final ReactContext reactContext, final ReactEditText editText) {
super(target, false);
mEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
mEventDispatcher =
Assertions.assertNotNull(reactContext.getNativeModule(UIManagerModule.class))
.getEventDispatcher();
mEditText = editText;
}

Expand Down
Loading

0 comments on commit 9263eb5

Please sign in to comment.