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
This commit is contained in:
Dulmandakh 2019-02-23 10:03:33 -08:00 committed by Mike Grabowski
parent f9097017de
commit fbf039b126
2 changed files with 45 additions and 46 deletions

View File

@ -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<T extends View, C extends LayoutShadowNode
private static double[] sTransformDecompositionArray = new double[16];
@ReactProp(name = PROP_BACKGROUND_COLOR, defaultInt = Color.TRANSPARENT, customType = "Color")
public void setBackgroundColor(T view, int backgroundColor) {
public void setBackgroundColor(@Nonnull T view, int backgroundColor) {
view.setBackgroundColor(backgroundColor);
}
@ReactProp(name = PROP_TRANSFORM)
public void setTransform(T view, ReadableArray matrix) {
public void setTransform(@Nonnull T view, @Nullable ReadableArray matrix) {
if (matrix == null) {
resetTransformProperty(view);
} else {
@ -72,20 +74,17 @@ public abstract class BaseViewManager<T extends View, C extends LayoutShadowNode
}
@ReactProp(name = ViewProps.OPACITY, defaultFloat = 1.f)
public void setOpacity(T view, float opacity) {
public void setOpacity(@Nonnull T view, float opacity) {
view.setAlpha(opacity);
}
@ReactProp(name = PROP_ELEVATION)
public void setElevation(T view, float elevation) {
if (Build.VERSION.SDK_INT >= 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<T extends View, C extends LayoutShadowNode
}
@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 abstract class BaseViewManager<T extends View, C extends LayoutShadowNode
}
@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 abstract class BaseViewManager<T extends View, C extends LayoutShadowNode
}
@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 abstract class BaseViewManager<T extends View, C extends LayoutShadowNode
}
@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 abstract class BaseViewManager<T extends View, C extends LayoutShadowNode
@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 @@ public abstract class BaseViewManager<T extends View, C extends LayoutShadowNode
}
}
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 @@ public abstract class BaseViewManager<T extends View, C extends LayoutShadowNode
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);
}

View File

@ -20,6 +20,8 @@ import com.facebook.react.uimanager.annotations.ReactPropGroup;
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 @@ import javax.annotation.Nullable;
public abstract class ViewManager<T extends View, C extends ReactShadowNode>
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<T extends View, C extends ReactShadowNode>
/**
* 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<T extends View, C extends ReactShadowNode>
* @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<T extends View, C extends ReactShadowNode>
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<T extends View, C extends ReactShadowNode>
* 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<T extends View, C extends ReactShadowNode>
* 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<T extends View, C extends ReactShadowNode>
* 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<T extends View, C extends ReactShadowNode>
*
* 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<T extends View, C extends ReactShadowNode>
* @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<T extends View, C extends ReactShadowNode>
/**
*
*/
public @Nullable Object updateLocalData(T view, ReactStylesDiffMap props, ReactStylesDiffMap localData) {
public @Nullable Object updateLocalData(@Nonnull T view, ReactStylesDiffMap props, ReactStylesDiffMap localData) {
return null;
}