From d61a03674539bec954108ab1a102e97eab00e08b Mon Sep 17 00:00:00 2001 From: Dulmandakh Date: Sat, 23 Feb 2019 18:34:28 +0800 Subject: [PATCH] add nullable annotations to some ViewManager methods --- .../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 e7d003854bec89..88fcf056601cbe 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.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 void setZIndex(T view, float zIndex) { } @ReactProp(name = PROP_RENDER_TO_HARDWARE_TEXTURE) - public void setRenderToHardwareTexture(T view, boolean useHWTexture) { + public void setRenderToHardwareTexture(@Nonnull T view, boolean useHWTexture) { view.setLayerType(useHWTexture ? View.LAYER_TYPE_HARDWARE : View.LAYER_TYPE_NONE, null); } @ReactProp(name = PROP_TEST_ID) - public void setTestId(T view, String testId) { + public void setTestId(@Nonnull T view, String testId) { view.setTag(R.id.react_test_id, testId); // temporarily set the tag and keyed tags to avoid end to end test regressions @@ -108,28 +107,28 @@ public void setTestId(T view, String testId) { } @ReactProp(name = PROP_NATIVE_ID) - public void setNativeId(T view, String nativeId) { + public void setNativeId(@Nonnull T view, String nativeId) { view.setTag(R.id.view_tag_native_id, nativeId); ReactFindViewUtil.notifyViewRendered(view); } @ReactProp(name = PROP_ACCESSIBILITY_LABEL) - public void setAccessibilityLabel(T view, String accessibilityLabel) { + public void setAccessibilityLabel(@Nonnull T view, String accessibilityLabel) { view.setContentDescription(accessibilityLabel); } @ReactProp(name = PROP_ACCESSIBILITY_COMPONENT_TYPE) - public void setAccessibilityComponentType(T view, String accessibilityComponentType) { + public void setAccessibilityComponentType(@Nonnull T view, String accessibilityComponentType) { AccessibilityHelper.updateAccessibilityComponentType(view, accessibilityComponentType); } @ReactProp(name = PROP_ACCESSIBILITY_HINT) - public void setAccessibilityHint(T view, String accessibilityHint) { + public void setAccessibilityHint(@Nonnull T view, String accessibilityHint) { view.setTag(R.id.accessibility_hint, accessibilityHint); } @ReactProp(name = PROP_ACCESSIBILITY_ROLE) - public void setAccessibilityRole(T view, String accessibilityRole) { + public void setAccessibilityRole(@Nonnull T view, @Nullable String accessibilityRole) { if (accessibilityRole == null) { return; } @@ -138,7 +137,7 @@ public void setAccessibilityRole(T view, String accessibilityRole) { } @ReactProp(name = PROP_ACCESSIBILITY_STATES) - public void setViewStates(T view, ReadableArray accessibilityStates) { + public void setViewStates(@Nonnull T view, @Nullable ReadableArray accessibilityStates) { view.setSelected(false); view.setEnabled(true); if (accessibilityStates == null) { @@ -155,7 +154,7 @@ public void setViewStates(T view, ReadableArray accessibilityStates) { } @ReactProp(name = PROP_IMPORTANT_FOR_ACCESSIBILITY) - public void setImportantForAccessibility(T view, String importantForAccessibility) { + public void setImportantForAccessibility(@Nonnull T view, @Nullable String importantForAccessibility) { if (importantForAccessibility == null || importantForAccessibility.equals("auto")) { ViewCompat.setImportantForAccessibility(view, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO); } else if (importantForAccessibility.equals("yes")) { @@ -169,48 +168,46 @@ public void setImportantForAccessibility(T view, String importantForAccessibilit @Deprecated @ReactProp(name = PROP_ROTATION) - public void setRotation(T view, float rotation) { + public void setRotation(@Nonnull T view, float rotation) { view.setRotation(rotation); } @Deprecated @ReactProp(name = PROP_SCALE_X, defaultFloat = 1f) - public void setScaleX(T view, float scaleX) { + public void setScaleX(@Nonnull T view, float scaleX) { view.setScaleX(scaleX); } @Deprecated @ReactProp(name = PROP_SCALE_Y, defaultFloat = 1f) - public void setScaleY(T view, float scaleY) { + public void setScaleY(@Nonnull T view, float scaleY) { view.setScaleY(scaleY); } @Deprecated @ReactProp(name = PROP_TRANSLATE_X, defaultFloat = 0f) - public void setTranslateX(T view, float translateX) { + public void setTranslateX(@Nonnull T view, float translateX) { view.setTranslationX(PixelUtil.toPixelFromDIP(translateX)); } @Deprecated @ReactProp(name = PROP_TRANSLATE_Y, defaultFloat = 0f) - public void setTranslateY(T view, float translateY) { + public void setTranslateY(@Nonnull T view, float translateY) { view.setTranslationY(PixelUtil.toPixelFromDIP(translateY)); } @ReactProp(name = PROP_ACCESSIBILITY_LIVE_REGION) - public void setAccessibilityLiveRegion(T view, String liveRegion) { - if (Build.VERSION.SDK_INT >= 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 @@ private static void setTransformProperty(View view, ReadableArray transforms) { } } - private static void resetTransformProperty(View view) { + private static void resetTransformProperty(@Nonnull View view) { view.setTranslationX(PixelUtil.toPixelFromDIP(0)); view.setTranslationY(PixelUtil.toPixelFromDIP(0)); view.setRotation(0); @@ -257,12 +254,12 @@ private static void resetTransformProperty(View view) { view.setCameraDistance(0); } - private void updateViewAccessibility(T view) { + private void updateViewAccessibility(@Nonnull T view) { AccessibilityDelegateUtil.setDelegate(view); } @Override - protected void onAfterUpdateTransaction(T view) { + protected void onAfterUpdateTransaction(@Nonnull T view) { super.onAfterUpdateTransaction(view); updateViewAccessibility(view); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java index 0fab09ccfbb00e..cf852a64500bef 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java @@ -20,6 +20,8 @@ import com.facebook.react.uimanager.annotations.ReactPropertyHolder; import com.facebook.yoga.YogaMeasureMode; import java.util.Map; + +import javax.annotation.Nonnull; import javax.annotation.Nullable; /** @@ -31,7 +33,7 @@ public abstract class ViewManager 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 final void updateProperties(T viewToUpdate, ReactStylesDiffMap props) { /** * 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 final T createView( * @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 C createShadowNodeInstance() { 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 C createShadowNodeInstance(ReactApplicationContext context) { * 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 void onDropViewInstance(T view) { * 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 @@ protected void addEventEmitters(ThemedReactContext reactContext, T view) { * 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 @@ protected void onAfterUpdateTransaction(T view) { * * 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 @@ protected void onAfterUpdateTransaction(T view) { * @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 Map getNativeProps() { /** * */ - public @Nullable Object updateLocalData(T view, ReactStylesDiffMap props, ReactStylesDiffMap localData) { + public @Nullable Object updateLocalData(@Nonnull T view, ReactStylesDiffMap props, ReactStylesDiffMap localData) { return null; }