Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nullable annotations to some ViewManager methods #23610

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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}.
Expand Down Expand Up @@ -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 {
Expand All @@ -72,20 +74,17 @@ public void setTransform(T view, ReadableArray matrix) {
}

@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();
Expand All @@ -95,41 +94,41 @@ 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
view.setTag(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;
}
Expand All @@ -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) {
Expand All @@ -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")) {
Expand All @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Expand Up @@ -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;

/**
Expand All @@ -31,16 +33,16 @@
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);
}

/**
* 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);
Expand All @@ -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
Expand All @@ -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();
}

Expand All @@ -85,21 +87,21 @@ 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) {
}

/**
* Subclasses can override this method to install custom event emitters on the given View. You
* 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) {
}

/**
Expand All @@ -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) {
}

/**
Expand All @@ -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
Expand All @@ -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) {
}

/**
Expand Down Expand Up @@ -209,7 +211,7 @@ public Map<String, String> getNativeProps() {
/**
*
*/
public @Nullable Object updateLocalData(T view, ReactStylesDiffMap props, ReactStylesDiffMap localData) {
public @Nullable Object updateLocalData(@Nonnull T view, ReactStylesDiffMap props, ReactStylesDiffMap localData) {
return null;
}

Expand Down