mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
refactor: migrate DynamicFromObject to Kotlin (#50754)
Summary: PR migrates DynamicFromObject class to Kotlin as part of https://github.com/facebook/react-native/issues/50513 work. ## Changelog: [ANDROID] [CHANGED] - Migrated DynamicFromObject to Kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/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
This commit is contained in:
committed by
Facebook GitHub Bot
parent
fa9d082747
commit
867858df65
@@ -704,7 +704,7 @@ public abstract interface class com/facebook/react/bridge/Dynamic {
|
||||
public abstract fun recycle ()V
|
||||
}
|
||||
|
||||
public class com/facebook/react/bridge/DynamicFromObject : com/facebook/react/bridge/Dynamic {
|
||||
public final class com/facebook/react/bridge/DynamicFromObject : com/facebook/react/bridge/Dynamic {
|
||||
public fun <init> (Ljava/lang/Object;)V
|
||||
public fun asArray ()Lcom/facebook/react/bridge/ReadableArray;
|
||||
public fun asBoolean ()Z
|
||||
|
||||
-109
@@ -1,109 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import com.facebook.common.logging.FLog;
|
||||
import com.facebook.infer.annotation.Nullsafe;
|
||||
import com.facebook.react.common.ReactConstants;
|
||||
|
||||
/** Implementation of Dynamic wrapping a ReadableArray. */
|
||||
@Nullsafe(Nullsafe.Mode.LOCAL)
|
||||
public class DynamicFromObject implements Dynamic {
|
||||
private @Nullable Object mObject;
|
||||
|
||||
public DynamicFromObject(@Nullable Object obj) {
|
||||
mObject = obj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recycle() {
|
||||
// Noop - nothing to recycle since there is no pooling
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNull() {
|
||||
return mObject == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean asBoolean() {
|
||||
if (mObject == null || !(mObject instanceof Boolean)) {
|
||||
throw new ClassCastException("Dynamic value from Object is not a boolean");
|
||||
}
|
||||
return (boolean) mObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double asDouble() {
|
||||
if (mObject == null || !(mObject instanceof Number)) {
|
||||
throw new ClassCastException("Dynamic value from Object is not a number");
|
||||
}
|
||||
return (double) mObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int asInt() {
|
||||
if (mObject == null || !(mObject instanceof Number)) {
|
||||
throw new ClassCastException("Dynamic value from Object is not a number");
|
||||
}
|
||||
// Numbers from JS are always Doubles
|
||||
return ((Double) mObject).intValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
if (mObject == null || !(mObject instanceof String)) {
|
||||
throw new ClassCastException("Dynamic value from Object is not a string");
|
||||
}
|
||||
return (String) mObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableArray asArray() {
|
||||
if (mObject == null || !(mObject instanceof ReadableArray)) {
|
||||
throw new ClassCastException("Dynamic value from Object is not a ReadableArray");
|
||||
}
|
||||
return (ReadableArray) mObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableMap asMap() {
|
||||
if (mObject == null || !(mObject instanceof ReadableMap)) {
|
||||
throw new ClassCastException("Dynamic value from Object is not a ReadableMap");
|
||||
}
|
||||
return (ReadableMap) mObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableType getType() {
|
||||
if (isNull()) {
|
||||
return ReadableType.Null;
|
||||
}
|
||||
if (mObject instanceof Boolean) {
|
||||
return ReadableType.Boolean;
|
||||
}
|
||||
if (mObject instanceof Number) {
|
||||
return ReadableType.Number;
|
||||
}
|
||||
if (mObject instanceof String) {
|
||||
return ReadableType.String;
|
||||
}
|
||||
if (mObject instanceof ReadableMap) {
|
||||
return ReadableType.Map;
|
||||
}
|
||||
if (mObject instanceof ReadableArray) {
|
||||
return ReadableType.Array;
|
||||
}
|
||||
FLog.e(
|
||||
ReactConstants.TAG,
|
||||
"Unmapped object type "
|
||||
+ (mObject == null ? "<NULL object>" : mObject.getClass().getName()));
|
||||
return ReadableType.Null;
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge
|
||||
|
||||
import com.facebook.common.logging.FLog
|
||||
import com.facebook.react.common.ReactConstants
|
||||
|
||||
/** Implementation of Dynamic wrapping a ReadableArray. */
|
||||
public class DynamicFromObject(private val value: Any?) : Dynamic {
|
||||
override fun recycle() {
|
||||
// Noop - nothing to recycle since there is no pooling
|
||||
}
|
||||
|
||||
override val isNull: Boolean
|
||||
get() = value == null
|
||||
|
||||
override fun asBoolean(): Boolean {
|
||||
if (value is Boolean) {
|
||||
return value
|
||||
}
|
||||
throw ClassCastException("Dynamic value from Object is not a boolean")
|
||||
}
|
||||
|
||||
override fun asDouble(): Double {
|
||||
if (value is Number) {
|
||||
return value as Double
|
||||
}
|
||||
throw ClassCastException("Dynamic value from Object is not a number")
|
||||
}
|
||||
|
||||
override fun asInt(): Int {
|
||||
if (value is Number) {
|
||||
// Numbers from JS are always Doubles
|
||||
return (value as Double).toInt()
|
||||
}
|
||||
throw ClassCastException("Dynamic value from Object is not a number")
|
||||
}
|
||||
|
||||
override fun asString(): String {
|
||||
if (value is String) {
|
||||
return value
|
||||
}
|
||||
throw ClassCastException("Dynamic value from Object is not a string")
|
||||
}
|
||||
|
||||
override fun asArray(): ReadableArray {
|
||||
if (value is ReadableArray) {
|
||||
return value
|
||||
}
|
||||
throw ClassCastException("Dynamic value from Object is not a ReadableArray")
|
||||
}
|
||||
|
||||
override fun asMap(): ReadableMap {
|
||||
if (value is ReadableMap) {
|
||||
return value
|
||||
}
|
||||
throw ClassCastException("Dynamic value from Object is not a ReadableMap")
|
||||
}
|
||||
|
||||
override val type: ReadableType
|
||||
get() {
|
||||
return when (value) {
|
||||
null -> ReadableType.Null
|
||||
is Boolean -> ReadableType.Boolean
|
||||
is Number -> ReadableType.Number
|
||||
is String -> ReadableType.String
|
||||
is ReadableMap -> ReadableType.Map
|
||||
is ReadableArray -> ReadableType.Array
|
||||
else -> {
|
||||
FLog.e(ReactConstants.TAG, "Unmapped object type " + (value.javaClass.name))
|
||||
ReadableType.Null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user