Skip to content

Commit

Permalink
Convert com.facebook.react.bridge.ReadableNativeArray to Kotlin (#47484)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #47484

Small change to types in the base class: all non-primitives return optional from ReadableArray, which matches the semantics in ReadableMap. We already rely on this in some cases, but the current nullability annotations were incorrect, and null values from the array would be passed through from `getMap` and `getArray`.

Changelog: [Android][Breaking] ReadableArray non-primitive getters are now correctly typed as optional

Reviewed By: Abbondanzo

Differential Revision: D65596278

fbshipit-source-id: 5574e9000b07de292bd0da5f1b071aac0eb331d6
  • Loading branch information
javache authored and facebook-github-bot committed Nov 22, 2024
1 parent 3575e21 commit 145c72f
Show file tree
Hide file tree
Showing 21 changed files with 139 additions and 228 deletions.
2 changes: 1 addition & 1 deletion packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ public class com/facebook/react/bridge/ReadableNativeArray : com/facebook/react/
public fun getDouble (I)D
public fun getDynamic (I)Lcom/facebook/react/bridge/Dynamic;
public fun getInt (I)I
public static fun getJNIPassCounter ()I
public static final fun getJNIPassCounter ()I
public fun getLong (I)J
public synthetic fun getMap (I)Lcom/facebook/react/bridge/ReadableMap;
public fun getMap (I)Lcom/facebook/react/bridge/ReadableNativeMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public class InterpolationAnimatedNode(config: ReadableMap) : ValueAnimatedNode(
val outputRange = arrayOfNulls<DoubleArray>(size)

// Match the first pattern into a List, since we don't know its length yet
var m = numericPattern.matcher(array.getString(0))
var m = numericPattern.matcher(array.getString(0) ?: "")
val firstOutputRange: MutableList<Double> = ArrayList()
while (m.find()) {
firstOutputRange.add(m.group().toDouble())
Expand All @@ -143,7 +143,7 @@ public class InterpolationAnimatedNode(config: ReadableMap) : ValueAnimatedNode(
for (i in 1 until size) {
val outputArr = DoubleArray(firstOutputRangeArr.size)
var j = 0
m = numericPattern.matcher(array.getString(i))
m = numericPattern.matcher(array.getString(i) ?: "")
while (m.find() && j < firstOutputRangeArr.size) {
outputArr[j++] = m.group().toDouble()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ internal class ObjectAnimatedNode(
ReadableType.String -> result.pushString(source.getString(i))
ReadableType.Map -> {
val map = source.getMap(i)
if (map.hasKey(NODE_TAG_KEY) && map.getType(NODE_TAG_KEY) == ReadableType.Number) {
if (map != null &&
map.hasKey(NODE_TAG_KEY) &&
map.getType(NODE_TAG_KEY) == ReadableType.Number) {
val node = nativeAnimatedNodesManager.getNodeById(map.getInt(NODE_TAG_KEY))
requireNotNull(node) { "Mapped value node does not exist" }
if (node is ValueAnimatedNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ internal class TransformAnimatedNode(
config: ReadableMap,
private val nativeAnimatedNodesManager: NativeAnimatedNodesManager
) : AnimatedNode() {
private val transformConfigs: MutableList<TransformConfig>
private val transformConfigs: List<TransformConfig>

init {
val transforms = config.getArray("transforms")
transformConfigs =
if (transforms == null) mutableListOf()
if (transforms == null) emptyList()
else
MutableList<TransformConfig>(transforms.size()) { i ->
val transformConfigMap = transforms.getMap(i)
List<TransformConfig>(transforms.size()) { i ->
val transformConfigMap = checkNotNull(transforms.getMap(i))
val property = transformConfigMap.getString("property")
val type = transformConfigMap.getString("type")
if (type == "animated") {
Expand All @@ -46,7 +46,7 @@ internal class TransformAnimatedNode(

public fun collectViewUpdates(propsMap: JavaOnlyMap) {
val transforms =
MutableList<JavaOnlyMap>(transformConfigs.size) { i ->
List<JavaOnlyMap>(transformConfigs.size) { i ->
val transformConfig = transformConfigs[i]
val transform =
if (transformConfig is AnimatedTransformConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static boolean supportWideGamut() {
private static final String ATTR_SEGMENT = "attr/";

@Nullable
private static Integer getColorInteger(Object value, Context context) {
private static Integer getColorInteger(@Nullable Object value, Context context) {
if (value == null) {
return null;
}
Expand Down Expand Up @@ -84,7 +84,7 @@ private static Integer getColorInteger(Object value, Context context) {
}

@Nullable
public static Color getColorInstance(Object value, Context context) {
public static Color getColorInstance(@Nullable Object value, Context context) {
if (value == null) {
return null;
}
Expand Down Expand Up @@ -136,7 +136,7 @@ public static Color getColorInstance(Object value, Context context) {
"ColorValue: the value must be a number or Object.");
}

public static Integer getColor(Object value, Context context) {
public static Integer getColor(@Nullable Object value, Context context) {
try {
if (supportWideGamut()) {
Color color = getColorInstance(value, context);
Expand All @@ -150,7 +150,7 @@ public static Integer getColor(Object value, Context context) {
return getColorInteger(value, context);
}

public static Integer getColor(Object value, Context context, int defaultInt) {
public static Integer getColor(@Nullable Object value, Context context, int defaultInt) {
try {
return getColor(value, context);
} catch (JSApplicationCausedNativeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ public class JavaOnlyArray : ReadableArray, WritableArray {

override fun getLong(index: Int): Long = (backingList[index] as Number).toLong()

override fun getString(index: Int): String = backingList[index] as String
override fun getString(index: Int): String? = backingList[index] as String?

override fun getArray(index: Int): ReadableArray = backingList[index] as ReadableArray
override fun getArray(index: Int): ReadableArray? = backingList[index] as ReadableArray?

override fun getBoolean(index: Int): Boolean = backingList[index] as Boolean

override fun getMap(index: Int): ReadableMap = backingList[index] as ReadableMap
override fun getMap(index: Int): ReadableMap? = backingList[index] as ReadableMap?

override fun getDynamic(index: Int): Dynamic = DynamicFromArray.create(this, index)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import java.util.ArrayList
* to Kotlin.
*/
public interface ReadableArray {
public fun getArray(index: Int): ReadableArray
public fun getArray(index: Int): ReadableArray?

public fun getBoolean(index: Int): Boolean

Expand All @@ -26,9 +26,9 @@ public interface ReadableArray {

public fun getLong(index: Int): Long

public fun getMap(index: Int): ReadableMap
public fun getMap(index: Int): ReadableMap?

public fun getString(index: Int): String
public fun getString(index: Int): String?

public fun getType(index: Int): ReadableType

Expand Down

This file was deleted.

Loading

0 comments on commit 145c72f

Please sign in to comment.