From 0b4f74176c9a2d21d0bcf9c497b7ce7440f6a920 Mon Sep 17 00:00:00 2001 From: Dulmandakh Date: Sat, 23 Feb 2019 10:03:33 -0800 Subject: [PATCH] add nullable annotations to some ViewManager methods (#23610) Summary: Add nullable annotations to BaseViewManager and ViewManager methods. This will improve Kotlin developer experience and help Android Studio to offer better autocomplete. [Android] [Changed] - add nullable annotations to BaseViewManager and ViewManager methods. Might break ViewManagers in Kotlin. Pull Request resolved: https://github.com/facebook/react-native/pull/23610 Differential Revision: D14198630 Pulled By: mdvacca fbshipit-source-id: c596c88254e1d02f0af233a466f685200fac8917 --- .../react/uimanager/BaseViewManager.java | 65 +++++++++---------- .../facebook/react/uimanager/ViewManager.java | 26 ++++---- 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java index e7d003854..88fcf0566 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java @@ -15,7 +15,9 @@ import com.facebook.react.bridge.ReadableArray; import com.facebook.react.uimanager.AccessibilityDelegateUtil.AccessibilityRole; import com.facebook.react.uimanager.annotations.ReactProp; import com.facebook.react.uimanager.util.ReactFindViewUtil; -import java.util.Locale; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; /** * Base class that should be suitable for the majority of subclasses of {@link ViewManager}. @@ -58,12 +60,12 @@ public abstract class BaseViewManager= Build.VERSION_CODES.LOLLIPOP) { - view.setElevation(PixelUtil.toPixelFromDIP(elevation)); - } - // Do nothing on API < 21 + public void setElevation(@Nonnull T view, float elevation) { + ViewCompat.setElevation(view, PixelUtil.toPixelFromDIP(elevation)); } @ReactProp(name = PROP_Z_INDEX) - public void setZIndex(T view, float zIndex) { + public void setZIndex(@Nonnull T view, float zIndex) { int integerZIndex = Math.round(zIndex); ViewGroupManager.setViewZIndex(view, integerZIndex); ViewParent parent = view.getParent(); @@ -95,12 +94,12 @@ public abstract class BaseViewManager= Build.VERSION_CODES.KITKAT) { + public void setAccessibilityLiveRegion(@Nonnull T view, @Nullable String liveRegion) { if (liveRegion == null || liveRegion.equals("none")) { - view.setAccessibilityLiveRegion(View.ACCESSIBILITY_LIVE_REGION_NONE); + ViewCompat.setAccessibilityLiveRegion(view, ViewCompat.ACCESSIBILITY_LIVE_REGION_NONE); } else if (liveRegion.equals("polite")) { - view.setAccessibilityLiveRegion(View.ACCESSIBILITY_LIVE_REGION_POLITE); + ViewCompat.setAccessibilityLiveRegion(view, ViewCompat.ACCESSIBILITY_LIVE_REGION_POLITE); } else if (liveRegion.equals("assertive")) { - view.setAccessibilityLiveRegion(View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE); + ViewCompat.setAccessibilityLiveRegion(view, ViewCompat.ACCESSIBILITY_LIVE_REGION_ASSERTIVE); } - } } - private static void setTransformProperty(View view, ReadableArray transforms) { + private static void setTransformProperty(@Nonnull View view, ReadableArray transforms) { TransformHelper.processTransform(transforms, sTransformDecompositionArray); MatrixMathHelper.decomposeMatrix(sTransformDecompositionArray, sMatrixDecompositionContext); view.setTranslationX( @@ -246,7 +243,7 @@ public abstract class BaseViewManager extends BaseJavaModule { - public final void updateProperties(T viewToUpdate, ReactStylesDiffMap props) { + public final void updateProperties(@Nonnull T viewToUpdate, ReactStylesDiffMap props) { ViewManagerPropertyUpdater.updateProps(this, viewToUpdate, props); onAfterUpdateTransaction(viewToUpdate); } @@ -39,8 +41,8 @@ public abstract class ViewManager /** * Creates a view and installs event emitters on it. */ - public final T createView( - ThemedReactContext reactContext, + public final @Nonnull T createView( + @Nonnull ThemedReactContext reactContext, JSResponderHandler jsResponderHandler) { T view = createViewInstance(reactContext); addEventEmitters(reactContext, view); @@ -54,7 +56,7 @@ public abstract class ViewManager * @return the name of this view manager. This will be the name used to reference this view * manager from JavaScript in createReactNativeComponentClass. */ - public abstract String getName(); + public abstract @Nonnull String getName(); /** * This method should return a subclass of {@link ReactShadowNode} which will be then used for @@ -65,7 +67,7 @@ public abstract class ViewManager throw new RuntimeException("ViewManager subclasses must implement createShadowNodeInstance()"); } - public C createShadowNodeInstance(ReactApplicationContext context) { + public @Nonnull C createShadowNodeInstance(@Nonnull ReactApplicationContext context) { return createShadowNodeInstance(); } @@ -85,13 +87,13 @@ public abstract class ViewManager * Subclasses should return a new View instance of the proper type. * @param reactContext */ - protected abstract T createViewInstance(ThemedReactContext reactContext); + protected abstract @Nonnull T createViewInstance(@Nonnull ThemedReactContext reactContext); /** * Called when view is detached from view hierarchy and allows for some additional cleanup by * the {@link ViewManager} subclass. */ - public void onDropViewInstance(T view) { + public void onDropViewInstance(@Nonnull T view) { } /** @@ -99,7 +101,7 @@ public abstract class ViewManager * might want to override this method if your view needs to emit events besides basic touch events * to JS (e.g. scroll events). */ - protected void addEventEmitters(ThemedReactContext reactContext, T view) { + protected void addEventEmitters(@Nonnull ThemedReactContext reactContext, @Nonnull T view) { } /** @@ -108,7 +110,7 @@ public abstract class ViewManager * you want to override this method you should call super.onAfterUpdateTransaction from it as * the parent class of the ViewManager may rely on callback being executed. */ - protected void onAfterUpdateTransaction(T view) { + protected void onAfterUpdateTransaction(@Nonnull T view) { } /** @@ -122,7 +124,7 @@ public abstract class ViewManager * * TODO(7247021): Replace updateExtraData with generic update props mechanism after D2086999 */ - public abstract void updateExtraData(T root, Object extraData); + public abstract void updateExtraData(@Nonnull T root, Object extraData); /** * Subclasses may use this method to receive events/commands directly from JS through the @@ -133,7 +135,7 @@ public abstract class ViewManager * @param commandId code of the command * @param args optional arguments for the command */ - public void receiveCommand(T root, int commandId, @Nullable ReadableArray args) { + public void receiveCommand(@Nonnull T root, int commandId, @Nullable ReadableArray args) { } /** @@ -209,7 +211,7 @@ public abstract class ViewManager /** * */ - public @Nullable Object updateLocalData(T view, ReactStylesDiffMap props, ReactStylesDiffMap localData) { + public @Nullable Object updateLocalData(@Nonnull T view, ReactStylesDiffMap props, ReactStylesDiffMap localData) { return null; }