Skip to content

Commit

Permalink
nullable annotations to ReadableMap, WritableMap, ReadableArray, Writ…
Browse files Browse the repository at this point in the history
…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 b640b6f
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,11 +23,11 @@ public interface ReadableArray {
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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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++) {
Expand Down
Loading

0 comments on commit b640b6f

Please sign in to comment.