mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
fe229f8104
Summary:
NativeReadable{Map,Array} classes have convenient to{HashMap,ArrayList}
methods that make it easier to interoperate with existing Java code
from within a ReactNative application (e.g., Native Module) ...
Thanks for submitting a PR! Please read these instructions carefully:
- [x] Explain the **motivation** for making this change.
- [x] Provide a **test plan** demonstrating that the code is solid.
- [x] Match the **code formatting** of the rest of the codebase.
- [x] Target the `master` branch, NOT a "stable" branch.
NativeReadable{Map,Array} classes have convenient to{HashMap,ArrayList}
methods that make it easier to interoperate with existing Java code
from within a ReactNative application (e.g., Native Module)
These changes make these same methods available to any code using
the Readable{Map,Array} interfaces, instead of forcing consumers to
cast their generic instances into the NativeReadable* equivalents
Moving this methods up to the interfaces also makes it easier to
write unit tests for Native Modules - using the JavaOnly{Map,Array}
implementations of Readable{Map,Array} - while still relying on the
to{HashMap,ArrayList} methods
* Write a native module that receives a JSON object as `ReadableMap` and a JSON array `ReadableArray`.
* Print out the result of `toHashMap` and `toArrayList`.
* Make sure `NativeReadable{Map,Array}` works:
* Call the native module's method from JavaScript, passing an `Object` and an `Array`.
* Compare the printed values with the passed content.
* Make sure `JavaOnly{Map,Array}` works:
* Call the native module's method from the Java code and pass a `JavaOnlyMap` and a `JavaOnlyArray`.
* Compare the printed values with the passed content.
**Please advise if there is an automated test suite where I could add a case for this.**
Closes https://github.com/facebook/react-native/pull/14072
Differential Revision: D5123120
Pulled By: javache
fbshipit-source-id: 343f4396b99e03ecaf47993db6955d7932152f77
118 lines
3.3 KiB
Java
118 lines
3.3 KiB
Java
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
package com.facebook.react.bridge;
|
|
|
|
import com.facebook.jni.HybridData;
|
|
import com.facebook.proguard.annotations.DoNotStrip;
|
|
|
|
import java.util.HashMap;
|
|
|
|
/**
|
|
* Implementation of a read-only map in native memory. This will generally be constructed and filled
|
|
* in native code so you shouldn't construct one yourself.
|
|
*/
|
|
@DoNotStrip
|
|
public class ReadableNativeMap extends NativeMap implements ReadableMap {
|
|
static {
|
|
ReactBridge.staticInit();
|
|
}
|
|
|
|
protected ReadableNativeMap(HybridData hybridData) {
|
|
super(hybridData);
|
|
}
|
|
|
|
@Override
|
|
public native boolean hasKey(String name);
|
|
@Override
|
|
public native boolean isNull(String name);
|
|
@Override
|
|
public native boolean getBoolean(String name);
|
|
@Override
|
|
public native double getDouble(String name);
|
|
@Override
|
|
public native int getInt(String name);
|
|
@Override
|
|
public native String getString(String name);
|
|
@Override
|
|
public native ReadableNativeArray getArray(String name);
|
|
@Override
|
|
public native ReadableNativeMap getMap(String name);
|
|
@Override
|
|
public native ReadableType getType(String name);
|
|
|
|
@Override
|
|
public Dynamic getDynamic(String name) {
|
|
return DynamicFromMap.create(this, name);
|
|
}
|
|
|
|
@Override
|
|
public ReadableMapKeySetIterator keySetIterator() {
|
|
return new ReadableNativeMapKeySetIterator(this);
|
|
}
|
|
|
|
@Override
|
|
public HashMap<String, Object> toHashMap() {
|
|
ReadableMapKeySetIterator iterator = keySetIterator();
|
|
HashMap<String, Object> hashMap = new HashMap<>();
|
|
|
|
while (iterator.hasNextKey()) {
|
|
String key = iterator.nextKey();
|
|
switch (getType(key)) {
|
|
case Null:
|
|
hashMap.put(key, null);
|
|
break;
|
|
case Boolean:
|
|
hashMap.put(key, getBoolean(key));
|
|
break;
|
|
case Number:
|
|
hashMap.put(key, getDouble(key));
|
|
break;
|
|
case String:
|
|
hashMap.put(key, getString(key));
|
|
break;
|
|
case Map:
|
|
hashMap.put(key, getMap(key).toHashMap());
|
|
break;
|
|
case Array:
|
|
hashMap.put(key, getArray(key).toArrayList());
|
|
break;
|
|
default:
|
|
throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
|
|
}
|
|
}
|
|
return hashMap;
|
|
}
|
|
|
|
/**
|
|
* Implementation of a {@link ReadableNativeMap} iterator in native memory.
|
|
*/
|
|
@DoNotStrip
|
|
private static class ReadableNativeMapKeySetIterator implements ReadableMapKeySetIterator {
|
|
@DoNotStrip
|
|
private final HybridData mHybridData;
|
|
|
|
// Need to hold a strong ref to the map so that our native references remain valid.
|
|
@DoNotStrip
|
|
private final ReadableNativeMap mMap;
|
|
|
|
public ReadableNativeMapKeySetIterator(ReadableNativeMap readableNativeMap) {
|
|
mMap = readableNativeMap;
|
|
mHybridData = initHybrid(readableNativeMap);
|
|
}
|
|
|
|
@Override
|
|
public native boolean hasNextKey();
|
|
@Override
|
|
public native String nextKey();
|
|
|
|
private static native HybridData initHybrid(ReadableNativeMap readableNativeMap);
|
|
}
|
|
}
|