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
181 lines
4.3 KiB
Java
181 lines
4.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 java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Java {@link ArrayList} backed impementation of {@link ReadableArray} and {@link WritableArray}
|
|
* Instances of this class SHOULD NOT be used for communication between java and JS, use instances
|
|
* of {@link WritableNativeArray} created via {@link Arguments#createArray} or just
|
|
* {@link ReadableArray} interface if you want your "native" module method to take an array from JS
|
|
* as an argument.
|
|
*
|
|
* Main purpose for this class is to be used in java-only unit tests, but could also be used outside
|
|
* of tests in the code that operates only in java and needs to communicate with RN modules via
|
|
* their JS-exposed API.
|
|
*/
|
|
public class JavaOnlyArray implements ReadableArray, WritableArray {
|
|
|
|
private final List mBackingList;
|
|
|
|
public static JavaOnlyArray from(List list) {
|
|
return new JavaOnlyArray(list);
|
|
}
|
|
|
|
public static JavaOnlyArray of(Object... values) {
|
|
return new JavaOnlyArray(values);
|
|
}
|
|
|
|
private JavaOnlyArray(Object... values) {
|
|
mBackingList = Arrays.asList(values);
|
|
}
|
|
|
|
private JavaOnlyArray(List list) {
|
|
mBackingList = new ArrayList(list);
|
|
}
|
|
|
|
public JavaOnlyArray() {
|
|
mBackingList = new ArrayList();
|
|
}
|
|
|
|
@Override
|
|
public int size() {
|
|
return mBackingList.size();
|
|
}
|
|
|
|
@Override
|
|
public boolean isNull(int index) {
|
|
return mBackingList.get(index) == null;
|
|
}
|
|
|
|
@Override
|
|
public double getDouble(int index) {
|
|
return (Double) mBackingList.get(index);
|
|
}
|
|
|
|
@Override
|
|
public int getInt(int index) {
|
|
return (Integer) mBackingList.get(index);
|
|
}
|
|
|
|
@Override
|
|
public String getString(int index) {
|
|
return (String) mBackingList.get(index);
|
|
}
|
|
|
|
@Override
|
|
public JavaOnlyArray getArray(int index) {
|
|
return (JavaOnlyArray) mBackingList.get(index);
|
|
}
|
|
|
|
@Override
|
|
public boolean getBoolean(int index) {
|
|
return (Boolean) mBackingList.get(index);
|
|
}
|
|
|
|
@Override
|
|
public JavaOnlyMap getMap(int index) {
|
|
return (JavaOnlyMap) mBackingList.get(index);
|
|
}
|
|
|
|
@Override
|
|
public Dynamic getDynamic(int index) {
|
|
return DynamicFromArray.create(this, index);
|
|
}
|
|
|
|
@Override
|
|
public ReadableType getType(int index) {
|
|
Object object = mBackingList.get(index);
|
|
|
|
if (object == null) {
|
|
return ReadableType.Null;
|
|
} else if (object instanceof Boolean) {
|
|
return ReadableType.Boolean;
|
|
} else if (object instanceof Double ||
|
|
object instanceof Float ||
|
|
object instanceof Integer) {
|
|
return ReadableType.Number;
|
|
} else if (object instanceof String) {
|
|
return ReadableType.String;
|
|
} else if (object instanceof ReadableArray) {
|
|
return ReadableType.Array;
|
|
} else if (object instanceof ReadableMap) {
|
|
return ReadableType.Map;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public void pushBoolean(boolean value) {
|
|
mBackingList.add(value);
|
|
}
|
|
|
|
@Override
|
|
public void pushDouble(double value) {
|
|
mBackingList.add(value);
|
|
}
|
|
|
|
@Override
|
|
public void pushInt(int value) {
|
|
mBackingList.add(value);
|
|
}
|
|
|
|
@Override
|
|
public void pushString(String value) {
|
|
mBackingList.add(value);
|
|
}
|
|
|
|
@Override
|
|
public void pushArray(WritableArray array) {
|
|
mBackingList.add(array);
|
|
}
|
|
|
|
@Override
|
|
public void pushMap(WritableMap map) {
|
|
mBackingList.add(map);
|
|
}
|
|
|
|
@Override
|
|
public void pushNull() {
|
|
mBackingList.add(null);
|
|
}
|
|
|
|
@Override
|
|
public ArrayList<Object> toArrayList() {
|
|
return new ArrayList<Object>(mBackingList);
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return mBackingList.toString();
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
if (this == o) return true;
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
JavaOnlyArray that = (JavaOnlyArray) o;
|
|
|
|
if (mBackingList != null ? !mBackingList.equals(that.mBackingList) : that.mBackingList != null)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return mBackingList != null ? mBackingList.hashCode() : 0;
|
|
}
|
|
}
|