From dbb5a23cadd5cfa0c0ad4dafaa17ee4992662b05 Mon Sep 17 00:00:00 2001 From: Gijs Weterings Date: Wed, 9 Apr 2025 09:07:30 -0700 Subject: [PATCH] fix nullsafe FIXMEs for ReactBaseTextShadowNode.java and mark nullsafe Summary: Gone trough all the FIXMEs added in the previous diff by the nullsafe tool, marked the class as nullsafe and ensured no remaining violations. Changelog: [Android][Fixed] Made ReactBAseTeextShadowNode.java nullsafe Reviewed By: alanleedev Differential Revision: D72460122 fbshipit-source-id: 772b19016fa1558b549d73ad2b23f891ce2f57f6 --- .../ReactAndroid/api/ReactAndroid.api | 2 + .../views/text/ReactBaseTextShadowNode.java | 42 +++++++++---------- .../react/views/text/TextTransform.kt | 4 ++ 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 7b50ecdd4c1..f4caf8d95aa 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -6744,6 +6744,7 @@ public final class com/facebook/react/views/text/TextTransform : java/lang/Enum public static final field UNSET Lcom/facebook/react/views/text/TextTransform; public static final field UPPERCASE Lcom/facebook/react/views/text/TextTransform; public static final fun apply (Ljava/lang/String;Lcom/facebook/react/views/text/TextTransform;)Ljava/lang/String; + public static final fun applyNonNull (Ljava/lang/String;Lcom/facebook/react/views/text/TextTransform;)Ljava/lang/String; public static fun getEntries ()Lkotlin/enums/EnumEntries; public static fun valueOf (Ljava/lang/String;)Lcom/facebook/react/views/text/TextTransform; public static fun values ()[Lcom/facebook/react/views/text/TextTransform; @@ -6751,6 +6752,7 @@ public final class com/facebook/react/views/text/TextTransform : java/lang/Enum public final class com/facebook/react/views/text/TextTransform$Companion { public final fun apply (Ljava/lang/String;Lcom/facebook/react/views/text/TextTransform;)Ljava/lang/String; + public final fun applyNonNull (Ljava/lang/String;Lcom/facebook/react/views/text/TextTransform;)Ljava/lang/String; } public final class com/facebook/react/views/text/TextTransformKt { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java index e97d33943d5..5737e1611c9 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java @@ -18,6 +18,7 @@ import android.view.Gravity; import androidx.annotation.Nullable; import com.facebook.common.logging.FLog; import com.facebook.infer.annotation.Assertions; +import com.facebook.infer.annotation.Nullsafe; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.common.ReactConstants; @@ -66,6 +67,7 @@ import java.util.Map; * can be used in concrete classes to feed native views and compute layout. */ @LegacyArchitecture +@Nullsafe(Nullsafe.Mode.LOCAL) public abstract class ReactBaseTextShadowNode extends LayoutShadowNode { // Use a direction weak character so the placeholder doesn't change the direction of the previous @@ -105,10 +107,10 @@ public abstract class ReactBaseTextShadowNode extends LayoutShadowNode { ReactShadowNode child = textShadowNode.getChildAt(i); if (child instanceof ReactRawTextShadowNode) { - sb.append( - // NULLSAFE_FIXME[Parameter Not Nullable] - TextTransform.apply( - ((ReactRawTextShadowNode) child).getText(), textAttributes.getTextTransform())); + String childText = ((ReactRawTextShadowNode) child).getText(); + if (childText != null) { + sb.append(TextTransform.applyNonNull(childText, textAttributes.getTextTransform())); + } } else if (child instanceof ReactBaseTextShadowNode) { buildSpannedFromShadowNode( (ReactBaseTextShadowNode) child, @@ -154,8 +156,9 @@ public abstract class ReactBaseTextShadowNode extends LayoutShadowNode { sb.length() - INLINE_VIEW_PLACEHOLDER.length(), sb.length(), new TextInlineViewPlaceholderSpan(reactTag, (int) width, (int) height))); - // NULLSAFE_FIXME[Nullable Dereference] - inlineViews.put(reactTag, child); + + // supportsInlineViews is true, so we can assume that inlineViews is not null + Assertions.assertNotNull(inlineViews).put(reactTag, child); } else { throw new IllegalViewOperationException( "Unexpected view type nested under a or node: " + child.getClass()); @@ -261,14 +264,13 @@ public abstract class ReactBaseTextShadowNode extends LayoutShadowNode { if (text != null) { // Handle text that is provided via a prop (e.g. the `value` and `defaultValue` props on // TextInput). - // NULLSAFE_FIXME[Parameter Not Nullable] - sb.append(TextTransform.apply(text, textShadowNode.mTextAttributes.getTextTransform())); + sb.append( + TextTransform.applyNonNull(text, textShadowNode.mTextAttributes.getTextTransform())); } buildSpannedFromShadowNode(textShadowNode, sb, ops, null, supportsInlineViews, inlineViews, 0); textShadowNode.mContainsImages = false; - // NULLSAFE_FIXME[Field Not Nullable] textShadowNode.mInlineViews = inlineViews; float heightOfTallestInlineViewOrImage = Float.NaN; @@ -290,13 +292,13 @@ public abstract class ReactBaseTextShadowNode extends LayoutShadowNode { // Inline views cannot be layout-only because the ReactTextView needs to be able to grab // ahold of them on the UI thread to size and position them. - // NULLSAFE_FIXME[Nullable Dereference] - ReactShadowNode childNode = inlineViews.get(placeholder.getReactTag()); - // NULLSAFE_FIXME[Parameter Not Nullable] + ReactShadowNode childNode = + Assertions.assertNotNull(inlineViews).get(placeholder.getReactTag()); + + Assertions.assertNotNull(childNode); nativeViewHierarchyOptimizer.handleForceViewToBeNonLayoutOnly(childNode); // The ReactTextView is responsible for laying out the inline views. - // NULLSAFE_FIXME[Nullable Dereference] childNode.setLayoutParent(textShadowNode); } @@ -386,8 +388,8 @@ public abstract class ReactBaseTextShadowNode extends LayoutShadowNode { protected @Nullable String mFontFeatureSettings = null; protected boolean mContainsImages = false; - // NULLSAFE_FIXME[Field Not Initialized] - protected Map mInlineViews; + // Only nullable iff `supportsInlineViews` is `false`. + protected @Nullable Map mInlineViews; public ReactBaseTextShadowNode() { this(null); @@ -482,9 +484,8 @@ public abstract class ReactBaseTextShadowNode extends LayoutShadowNode { @ReactProp(name = ViewProps.COLOR, customType = "Color") public void setColor(@Nullable Integer color) { - mIsColorSet = (color != null); - if (mIsColorSet) { - // NULLSAFE_FIXME[Nullable Dereference] + if (color != null) { + mIsColorSet = true; mColor = color; } markUpdated(); @@ -497,9 +498,8 @@ public abstract class ReactBaseTextShadowNode extends LayoutShadowNode { // nodes get mapped to native views and native views get their background colors get set via // {@link BaseViewManager}. if (isVirtual()) { - mIsBackgroundColorSet = (color != null); - if (mIsBackgroundColorSet) { - // NULLSAFE_FIXME[Nullable Dereference] + if (color != null) { + mIsBackgroundColorSet = true; mBackgroundColor = color; } markUpdated(); diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextTransform.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextTransform.kt index f095711592d..d4c23f09e03 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextTransform.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextTransform.kt @@ -23,6 +23,10 @@ public enum class TextTransform { @JvmStatic public fun apply(text: String?, textTransform: TextTransform?): String? = text?.applyTextTransform(textTransform) + + @JvmStatic + public fun applyNonNull(text: String, textTransform: TextTransform?): String = + text.applyTextTransform(textTransform) } }