Skip to content
Permalink
Browse files

nullable annotations to ReadableMap, WritableMap, ReadableArray, Writ…

…able. (#23329)

Summary:
Kotlin is getting traction and more developers write RN native modules in it. This PR adds nullable annotations to help with Kotlin null inference and improve developer experience. Also it'll help checking code quality using lint.

I skimmed through JavaOnlyMap.java, JavaOnlyArray.java, ReadableNativeArray.java, ReadableNativeMap.java, WritableNativeArray.java and WritableNativeMap.java to infer nullability.

This is breaking change to Kotlin code.

[Android] [Changed] - Add nullable annotations to ReadableMap, WritableMap, ReadableArray, Writable.
Pull Request resolved: #23329

Differential Revision: D14002571

Pulled By: cpojer

fbshipit-source-id: 899d8b3b0a5dad43e8300e6c4ea4208cca0f01a9
  • Loading branch information...
dulmandakh authored and facebook-github-bot committed Feb 8, 2019
1 parent d002d30 commit b640b6faf77f7af955e64bd03ae630ce2fb09627
@@ -11,6 +11,9 @@
import java.util.Arrays;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* Java {@link ArrayList} backed implementation of {@link ReadableArray} and {@link WritableArray}
* Instances of this class SHOULD NOT be used for communication between java and JS, use instances
@@ -95,7 +98,7 @@ public int getInt(int index) {
}

@Override
public String getString(int index) {
public @Nullable String getString(int index) {
return (String) mBackingList.get(index);
}

@@ -115,12 +118,12 @@ public JavaOnlyMap getMap(int index) {
}

@Override
public Dynamic getDynamic(int index) {
public @Nonnull Dynamic getDynamic(int index) {
return DynamicFromArray.create(this, index);
}

@Override
public ReadableType getType(int index) {
public @Nonnull ReadableType getType(int index) {
Object object = mBackingList.get(index);

if (object == null) {
@@ -157,17 +160,17 @@ public void pushInt(int value) {
}

@Override
public void pushString(String value) {
public void pushString(@Nonnull String value) {
mBackingList.add(value);
}

@Override
public void pushArray(WritableArray array) {
public void pushArray(@Nullable WritableArray array) {
mBackingList.add(array);
}

@Override
public void pushMap(WritableMap map) {
public void pushMap(@Nonnull WritableMap map) {
mBackingList.add(map);
}

@@ -177,7 +180,7 @@ public void pushNull() {
}

@Override
public ArrayList<Object> toArrayList() {
public @Nonnull ArrayList<Object> toArrayList() {
return new ArrayList<Object>(mBackingList);
}

@@ -11,6 +11,8 @@
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Nonnull;

/**
* Java {@link HashMap} backed implementation of {@link ReadableMap} and {@link WritableMap}
* Instances of this class SHOULD NOT be used for communication between java and JS, use instances
@@ -77,52 +79,52 @@ public JavaOnlyMap() {
}

@Override
public boolean hasKey(String name) {
public boolean hasKey(@Nonnull String name) {
return mBackingMap.containsKey(name);
}

@Override
public boolean isNull(String name) {
public boolean isNull(@Nonnull String name) {
return mBackingMap.get(name) == null;
}

@Override
public boolean getBoolean(String name) {
public boolean getBoolean(@Nonnull String name) {
return (Boolean) mBackingMap.get(name);
}

@Override
public double getDouble(String name) {
public double getDouble(@Nonnull String name) {
return ((Number) mBackingMap.get(name)).doubleValue();
}

@Override
public int getInt(String name) {
public int getInt(@Nonnull String name) {
return ((Number) mBackingMap.get(name)).intValue();
}

@Override
public String getString(String name) {
public String getString(@Nonnull String name) {
return (String) mBackingMap.get(name);
}

@Override
public ReadableMap getMap(String name) {
public ReadableMap getMap(@Nonnull String name) {
return (ReadableMap) mBackingMap.get(name);
}

@Override
public JavaOnlyArray getArray(String name) {
public JavaOnlyArray getArray(@Nonnull String name) {
return (JavaOnlyArray) mBackingMap.get(name);
}

@Override
public Dynamic getDynamic(String name) {
public @Nonnull Dynamic getDynamic(@Nonnull String name) {
return DynamicFromMap.create(this, name);
}

@Override
public ReadableType getType(String name) {
public @Nonnull ReadableType getType(@Nonnull String name) {
Object value = mBackingMap.get(name);
if (value == null) {
return ReadableType.Null;
@@ -145,7 +147,7 @@ public ReadableType getType(String name) {
}

@Override
public ReadableMapKeySetIterator keySetIterator() {
public @Nonnull ReadableMapKeySetIterator keySetIterator() {
return new ReadableMapKeySetIterator() {
Iterator<String> mIterator = mBackingMap.keySet().iterator();

@@ -162,47 +164,47 @@ public String nextKey() {
}

@Override
public void putBoolean(String key, boolean value) {
public void putBoolean(@Nonnull String key, boolean value) {
mBackingMap.put(key, value);
}

@Override
public void putDouble(String key, double value) {
public void putDouble(@Nonnull String key, double value) {
mBackingMap.put(key, value);
}

@Override
public void putInt(String key, int value) {
public void putInt(@Nonnull String key, int value) {
mBackingMap.put(key, value);
}

@Override
public void putString(String key, String value) {
public void putString(@Nonnull String key, @Nonnull String value) {
mBackingMap.put(key, value);
}

@Override
public void putNull(String key) {
public void putNull(@Nonnull String key) {
mBackingMap.put(key, null);
}

@Override
public void putMap(String key, WritableMap value) {
public void putMap(@Nonnull String key, @Nonnull WritableMap value) {
mBackingMap.put(key, value);
}

@Override
public void merge(ReadableMap source) {
public void merge(@Nonnull ReadableMap source) {
mBackingMap.putAll(((JavaOnlyMap) source).mBackingMap);
}

@Override
public void putArray(String key, WritableArray value) {
public void putArray(@Nonnull String key, @Nonnull WritableArray value) {
mBackingMap.put(key, value);
}

@Override
public HashMap<String, Object> toHashMap() {
public @Nonnull HashMap<String, Object> toHashMap() {
return new HashMap<String, Object>(mBackingMap);
}

@@ -9,6 +9,9 @@

import java.util.ArrayList;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* Interface for an array that allows typed access to its members. Used to pass parameters from JS
* to Java.
@@ -20,11 +23,11 @@
boolean getBoolean(int index);
double getDouble(int index);
int getInt(int index);
String getString(int index);
ReadableArray getArray(int index);
ReadableMap getMap(int index);
Dynamic getDynamic(int index);
ReadableType getType(int index);
ArrayList<Object> toArrayList();
@Nullable String getString(int index);
@Nullable ReadableArray getArray(int index);
@Nullable ReadableMap getMap(int index);
@Nonnull Dynamic getDynamic(int index);
@Nonnull ReadableType getType(int index);
@Nonnull ArrayList<Object> toArrayList();

}
@@ -9,23 +9,26 @@

import java.util.HashMap;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* Interface for a map that allows typed access to its members. Used to pass parameters from JS to
* Java.
*/
public interface ReadableMap {

boolean hasKey(String name);
boolean isNull(String name);
boolean getBoolean(String name);
double getDouble(String name);
int getInt(String name);
String getString(String name);
ReadableArray getArray(String name);
ReadableMap getMap(String name);
Dynamic getDynamic(String name);
ReadableType getType(String name);
ReadableMapKeySetIterator keySetIterator();
HashMap<String, Object> toHashMap();
boolean hasKey(@Nonnull String name);
boolean isNull(@Nonnull String name);
boolean getBoolean(@Nonnull String name);
double getDouble(@Nonnull String name);
int getInt(@Nonnull String name);
@Nullable String getString(@Nonnull String name);
@Nullable ReadableArray getArray(@Nonnull String name);
@Nullable ReadableMap getMap(@Nonnull String name);
@Nonnull Dynamic getDynamic(@Nonnull String name);
@Nonnull ReadableType getType(@Nonnull String name);
@Nonnull ReadableMapKeySetIterator keySetIterator();
@Nonnull HashMap<String, Object> toHashMap();

}
@@ -13,6 +13,8 @@
import com.facebook.react.config.ReactFeatureFlags;
import java.util.ArrayList;
import java.util.Arrays;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
@@ -125,7 +127,7 @@ public int getInt(int index) {
private native int getIntNative(int index);

@Override
public String getString(int index) {
public @Nullable String getString(int index) {
if (ReactFeatureFlags.useArrayNativeAccessor) {
jniPassCounter++;
return getStringNative(index);
@@ -135,7 +137,7 @@ public String getString(int index) {
private native String getStringNative(int index);

@Override
public ReadableNativeArray getArray(int index) {
public @Nullable ReadableNativeArray getArray(int index) {
if (ReactFeatureFlags.useArrayNativeAccessor) {
jniPassCounter++;
return getArrayNative(index);
@@ -145,7 +147,7 @@ public ReadableNativeArray getArray(int index) {
private native ReadableNativeArray getArrayNative(int index);

@Override
public ReadableNativeMap getMap(int index) {
public @Nullable ReadableNativeMap getMap(int index) {
if (ReactFeatureFlags.useArrayNativeAccessor) {
jniPassCounter++;
return getMapNative(index);
@@ -155,7 +157,7 @@ public ReadableNativeMap getMap(int index) {
private native ReadableNativeMap getMapNative(int index);

@Override
public ReadableType getType(int index) {
public @Nonnull ReadableType getType(int index) {
if (ReactFeatureFlags.useArrayNativeAccessor) {
jniPassCounter++;
return getTypeNative(index);
@@ -166,12 +168,12 @@ public ReadableType getType(int index) {
private native ReadableType getTypeNative(int index);

@Override
public Dynamic getDynamic(int index) {
public @Nonnull Dynamic getDynamic(int index) {
return DynamicFromArray.create(this, index);
}

@Override
public ArrayList<Object> toArrayList() {
public @Nonnull ArrayList<Object> toArrayList() {
ArrayList<Object> arrayList = new ArrayList<>();

for (int i = 0; i < this.size(); i++) {
Oops, something went wrong.

0 comments on commit b640b6f

Please sign in to comment.
You can’t perform that action at this time.