Skip to content

Commit 867858d

Browse files
yasir6janfacebook-github-bot
authored andcommitted
refactor: migrate DynamicFromObject to Kotlin (#50754)
Summary: PR migrates DynamicFromObject class to Kotlin as part of #50513 work. ## Changelog: [ANDROID] [CHANGED] - Migrated DynamicFromObject to Kotlin Pull Request resolved: #50754 Test Plan: Run RN-Tester application and played around with it a bit. Reviewed By: mlord93 Differential Revision: D73118014 Pulled By: alanleedev fbshipit-source-id: 84958ff07ccafea9ec3dbdf06467c638eb92d49d
1 parent fa9d082 commit 867858d

3 files changed

Lines changed: 81 additions & 110 deletions

File tree

‎packages/react-native/ReactAndroid/api/ReactAndroid.api‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ public abstract interface class com/facebook/react/bridge/Dynamic {
704704
public abstract fun recycle ()V
705705
}
706706

707-
public class com/facebook/react/bridge/DynamicFromObject : com/facebook/react/bridge/Dynamic {
707+
public final class com/facebook/react/bridge/DynamicFromObject : com/facebook/react/bridge/Dynamic {
708708
public fun <init> (Ljava/lang/Object;)V
709709
public fun asArray ()Lcom/facebook/react/bridge/ReadableArray;
710710
public fun asBoolean ()Z

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromObject.java‎

Lines changed: 0 additions & 109 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.bridge
9+
10+
import com.facebook.common.logging.FLog
11+
import com.facebook.react.common.ReactConstants
12+
13+
/** Implementation of Dynamic wrapping a ReadableArray. */
14+
public class DynamicFromObject(private val value: Any?) : Dynamic {
15+
override fun recycle() {
16+
// Noop - nothing to recycle since there is no pooling
17+
}
18+
19+
override val isNull: Boolean
20+
get() = value == null
21+
22+
override fun asBoolean(): Boolean {
23+
if (value is Boolean) {
24+
return value
25+
}
26+
throw ClassCastException("Dynamic value from Object is not a boolean")
27+
}
28+
29+
override fun asDouble(): Double {
30+
if (value is Number) {
31+
return value as Double
32+
}
33+
throw ClassCastException("Dynamic value from Object is not a number")
34+
}
35+
36+
override fun asInt(): Int {
37+
if (value is Number) {
38+
// Numbers from JS are always Doubles
39+
return (value as Double).toInt()
40+
}
41+
throw ClassCastException("Dynamic value from Object is not a number")
42+
}
43+
44+
override fun asString(): String {
45+
if (value is String) {
46+
return value
47+
}
48+
throw ClassCastException("Dynamic value from Object is not a string")
49+
}
50+
51+
override fun asArray(): ReadableArray {
52+
if (value is ReadableArray) {
53+
return value
54+
}
55+
throw ClassCastException("Dynamic value from Object is not a ReadableArray")
56+
}
57+
58+
override fun asMap(): ReadableMap {
59+
if (value is ReadableMap) {
60+
return value
61+
}
62+
throw ClassCastException("Dynamic value from Object is not a ReadableMap")
63+
}
64+
65+
override val type: ReadableType
66+
get() {
67+
return when (value) {
68+
null -> ReadableType.Null
69+
is Boolean -> ReadableType.Boolean
70+
is Number -> ReadableType.Number
71+
is String -> ReadableType.String
72+
is ReadableMap -> ReadableType.Map
73+
is ReadableArray -> ReadableType.Array
74+
else -> {
75+
FLog.e(ReactConstants.TAG, "Unmapped object type " + (value.javaClass.name))
76+
ReadableType.Null
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)