From a58fcbff0bfd27a442884010aae48957a9990de1 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Fri, 25 Oct 2019 15:07:18 -0700 Subject: [PATCH] Add NonNull annotation to Fabric Event classes Summary: This diff annotates Fabric MountingManager and Events classes with NonNull annotations, this will help analysis of nullability plus improving integration with Kotlin clients Changelog: Add NonNull annotation to Fabric Event classes Reviewed By: shergin Differential Revision: D18010923 fbshipit-source-id: fb9d5683bbd51fa25dda9b2023f9c411c3ff541d --- .../react/fabric/events/EventBeatManager.java | 3 +- .../fabric/events/EventEmitterWrapper.java | 5 +- .../fabric/events/FabricEventEmitter.java | 25 +++++---- .../fabric/mounting/MountingManager.java | 51 ++++++++++--------- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventBeatManager.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventBeatManager.java index 361f3544f32..61e763f1eb5 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventBeatManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventBeatManager.java @@ -8,6 +8,7 @@ package com.facebook.react.fabric.events; import android.annotation.SuppressLint; +import androidx.annotation.NonNull; import com.facebook.jni.HybridData; import com.facebook.proguard.annotations.DoNotStrip; import com.facebook.react.bridge.ReactApplicationContext; @@ -31,7 +32,7 @@ public class EventBeatManager implements BatchEventDispatchedListener { private native void beat(); - public EventBeatManager(ReactApplicationContext reactApplicationContext) { + public EventBeatManager(@NonNull ReactApplicationContext reactApplicationContext) { mHybridData = initHybrid(); mReactApplicationContext = reactApplicationContext; } diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventEmitterWrapper.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventEmitterWrapper.java index 22ec357f48e..a1cd86518c4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventEmitterWrapper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventEmitterWrapper.java @@ -8,6 +8,7 @@ package com.facebook.react.fabric.events; import android.annotation.SuppressLint; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.facebook.jni.HybridData; import com.facebook.proguard.annotations.DoNotStrip; @@ -35,7 +36,7 @@ public class EventEmitterWrapper { mHybridData = initHybrid(); } - private native void invokeEvent(String eventName, NativeMap params); + private native void invokeEvent(@NonNull String eventName, @NonNull NativeMap params); /** * Invokes the execution of the C++ EventEmitter. @@ -43,7 +44,7 @@ public class EventEmitterWrapper { * @param eventName {@link String} name of the event to execute. * @param params {@link WritableMap} payload of the event */ - public void invoke(String eventName, @Nullable WritableMap params) { + public void invoke(@NonNull String eventName, @Nullable WritableMap params) { NativeMap payload = params == null ? new WritableNativeMap() : (NativeMap) params; invokeEvent(eventName, payload); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/events/FabricEventEmitter.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/events/FabricEventEmitter.java index 075ef2b23d2..6b9578b442c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/events/FabricEventEmitter.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/events/FabricEventEmitter.java @@ -14,6 +14,7 @@ import static com.facebook.react.uimanager.events.TouchesHelper.TOP_TOUCH_END_KE import static com.facebook.react.uimanager.events.TouchesHelper.TOUCHES_KEY; import android.util.Pair; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.facebook.common.logging.FLog; import com.facebook.react.bridge.ReadableMap; @@ -29,16 +30,16 @@ import java.util.Set; public class FabricEventEmitter implements RCTEventEmitter { - private static final String TAG = FabricEventEmitter.class.getSimpleName(); + private static final String TAG = "FabricEventEmitter"; - private final FabricUIManager mUIManager; + @NonNull private final FabricUIManager mUIManager; - public FabricEventEmitter(FabricUIManager uiManager) { + public FabricEventEmitter(@NonNull FabricUIManager uiManager) { mUIManager = uiManager; } @Override - public void receiveEvent(int reactTag, String eventName, @Nullable WritableMap params) { + public void receiveEvent(int reactTag, @NonNull String eventName, @Nullable WritableMap params) { Systrace.beginSection( Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "FabricEventEmitter.receiveEvent('" + eventName + "')"); @@ -48,7 +49,9 @@ public class FabricEventEmitter implements RCTEventEmitter { @Override public void receiveTouches( - String eventTopLevelType, WritableArray touches, WritableArray changedIndices) { + @NonNull String eventTopLevelType, + @NonNull WritableArray touches, + @NonNull WritableArray changedIndices) { Pair result = TOP_TOUCH_END_KEY.equalsIgnoreCase(eventTopLevelType) || TOP_TOUCH_CANCEL_KEY.equalsIgnoreCase(eventTopLevelType) @@ -79,7 +82,7 @@ public class FabricEventEmitter implements RCTEventEmitter { } /** TODO T31905686 optimize this to avoid copying arrays */ - private WritableArray copyWritableArray(WritableArray array) { + private WritableArray copyWritableArray(@NonNull WritableArray array) { WritableNativeArray ret = new WritableNativeArray(); for (int i = 0; i < array.size(); i++) { ret.pushMap(getWritableMap(array.getMap(i))); @@ -101,8 +104,8 @@ public class FabricEventEmitter implements RCTEventEmitter { * @param indices {WritableArray} Indices to remove from `touches`. * @return {Array} Subsequence of removed touch objects. */ - private Pair removeTouchesAtIndices( - WritableArray touches, WritableArray indices) { + private @NonNull Pair removeTouchesAtIndices( + @NonNull WritableArray touches, @NonNull WritableArray indices) { WritableArray rippedOut = new WritableNativeArray(); // use an unsafe downcast to alias to nullable elements, // so we can delete and then compact. @@ -131,8 +134,8 @@ public class FabricEventEmitter implements RCTEventEmitter { * @param changedIndices {@link WritableArray} Indices by which to pull subsequence. * @return {Array} Subsequence of touch objects. */ - private Pair touchSubsequence( - WritableArray touches, WritableArray changedIndices) { + private @NonNull Pair touchSubsequence( + @NonNull WritableArray touches, @NonNull WritableArray changedIndices) { WritableArray result = new WritableNativeArray(); for (int i = 0; i < changedIndices.size(); i++) { result.pushMap(getWritableMap(touches.getMap(changedIndices.getInt(i)))); @@ -146,7 +149,7 @@ public class FabricEventEmitter implements RCTEventEmitter { * * @param readableMap {@link ReadableMap} source map */ - private WritableMap getWritableMap(ReadableMap readableMap) { + private @NonNull WritableMap getWritableMap(@NonNull ReadableMap readableMap) { WritableNativeMap map = new WritableNativeMap(); map.merge(readableMap); return map; diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java index e7556d66753..381b58a81f0 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java @@ -12,6 +12,7 @@ import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import androidx.annotation.AnyThread; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.UiThread; import com.facebook.infer.annotation.Assertions; @@ -44,17 +45,17 @@ import java.util.concurrent.ConcurrentHashMap; public class MountingManager { public static final String TAG = MountingManager.class.getSimpleName(); - private final ConcurrentHashMap mTagToViewState; - private final JSResponderHandler mJSResponderHandler = new JSResponderHandler(); - private final ViewManagerRegistry mViewManagerRegistry; - private final RootViewManager mRootViewManager = new RootViewManager(); + @NonNull private final ConcurrentHashMap mTagToViewState; + @NonNull private final JSResponderHandler mJSResponderHandler = new JSResponderHandler(); + @NonNull private final ViewManagerRegistry mViewManagerRegistry; + @NonNull private final RootViewManager mRootViewManager = new RootViewManager(); - public MountingManager(ViewManagerRegistry viewManagerRegistry) { + public MountingManager(@NonNull ViewManagerRegistry viewManagerRegistry) { mTagToViewState = new ConcurrentHashMap<>(); mViewManagerRegistry = viewManagerRegistry; } - public void addRootView(int reactRootTag, View rootView) { + public void addRootView(int reactRootTag, @NonNull View rootView) { if (rootView.getId() != View.NO_ID) { throw new IllegalViewOperationException( "Trying to add a root view with an explicit id already set. React Native uses " @@ -69,7 +70,7 @@ public class MountingManager { /** Releases all references to given native View. */ @UiThread - private void dropView(View view) { + private void dropView(@NonNull View view) { UiThreadUtil.assertOnUiThread(); int reactTag = view.getId(); @@ -109,7 +110,7 @@ public class MountingManager { getViewGroupManager(parentViewState).addView(parentView, view, index); } - private ViewState getViewState(int tag) { + private @NonNull ViewState getViewState(int tag) { ViewState viewState = mTagToViewState.get(tag); if (viewState == null) { throw new IllegalStateException("Unable to find viewState view for tag " + tag); @@ -134,7 +135,7 @@ public class MountingManager { } if (viewState.mViewManager == null) { - throw new IllegalStateException("Unable to find viewState manager for tag " + reactTag); + throw new IllegalStateException("Unable to find viewManager for tag " + reactTag); } if (viewState.mView == null) { @@ -144,7 +145,8 @@ public class MountingManager { viewState.mViewManager.receiveCommand(viewState.mView, commandId, commandArgs); } - public void receiveCommand(int reactTag, String commandId, @Nullable ReadableArray commandArgs) { + public void receiveCommand( + int reactTag, @NonNull String commandId, @Nullable ReadableArray commandArgs) { ViewState viewState = getNullableViewState(reactTag); if (viewState == null) { @@ -181,7 +183,8 @@ public class MountingManager { } @SuppressWarnings("unchecked") // prevents unchecked conversion warn of the type - private static ViewGroupManager getViewGroupManager(ViewState viewState) { + private static @NonNull ViewGroupManager getViewGroupManager( + @NonNull ViewState viewState) { if (viewState.mViewManager == null) { throw new IllegalStateException("Unable to find ViewManager for view: " + viewState); } @@ -212,8 +215,8 @@ public class MountingManager { @UiThread public void createView( - ThemedReactContext themedReactContext, - String componentName, + @NonNull ThemedReactContext themedReactContext, + @NonNull String componentName, int reactTag, @Nullable ReadableMap props, @Nullable StateWrapper stateWrapper, @@ -247,7 +250,7 @@ public class MountingManager { } @UiThread - public void updateProps(int reactTag, ReadableMap props) { + public void updateProps(int reactTag, @Nullable ReadableMap props) { if (props == null) { return; } @@ -339,7 +342,7 @@ public class MountingManager { } @UiThread - public void updateLocalData(int reactTag, ReadableMap newLocalData) { + public void updateLocalData(int reactTag, @NonNull ReadableMap newLocalData) { UiThreadUtil.assertOnUiThread(); ViewState viewState = getViewState(reactTag); if (viewState.mCurrentProps == null) { @@ -394,7 +397,7 @@ public class MountingManager { @UiThread public void preallocateView( - ThemedReactContext reactContext, + @NonNull ThemedReactContext reactContext, String componentName, int reactTag, @Nullable ReadableMap props, @@ -410,7 +413,7 @@ public class MountingManager { } @UiThread - public void updateEventEmitter(int reactTag, EventEmitterWrapper eventEmitter) { + public void updateEventEmitter(int reactTag, @NonNull EventEmitterWrapper eventEmitter) { UiThreadUtil.assertOnUiThread(); ViewState viewState = getViewState(reactTag); viewState.mEventEmitter = eventEmitter; @@ -471,15 +474,15 @@ public class MountingManager { @AnyThread public long measure( - Context context, - String componentName, - ReadableMap localData, - ReadableMap props, - ReadableMap state, + @NonNull Context context, + @NonNull String componentName, + @NonNull ReadableMap localData, + @NonNull ReadableMap props, + @NonNull ReadableMap state, float width, - YogaMeasureMode widthMode, + @NonNull YogaMeasureMode widthMode, float height, - YogaMeasureMode heightMode) { + @NonNull YogaMeasureMode heightMode) { return mViewManagerRegistry .get(componentName)