fix WritableArray, WritableMap nullable annotations (#23397)

Summary:
Recently, I added nullable annotations to ReadableArray, ReadableMap, WritableArray, WritableMap and subclasses to improve Kotlin developer experience. But found that I made mistake with pushArray, pushMap, pushString method of WritableArray, and putArray, putMap, putString methods of WritableMap. This PR fixes previous mistake.

Excerpt from WritableNativeArray.cpp.
```cpp
void WritableNativeArray::pushString(jstring value) {
  if (value == NULL) {
    pushNull();
    return;
  }
  throwIfConsumed();
  array_.push_back(wrap_alias(value)->toStdString());
}

void WritableNativeArray::pushNativeArray(WritableNativeArray* otherArray) {
  if (otherArray == NULL) {
    pushNull();
    return;
  }
  throwIfConsumed();
  array_.push_back(otherArray->consume());
}

void WritableNativeArray::pushNativeMap(WritableNativeMap* map) {
  if (map == NULL) {
    pushNull();
    return;
  }
  throwIfConsumed();
  array_.push_back(map->consume());
}
```

Excerpt from WritableNativeMap.cpp
```cpp
void WritableNativeMap::putString(std::string key, alias_ref<jstring> val) {
  if (!val) {
    putNull(std::move(key));
    return;
  }
  throwIfConsumed();
  map_.insert(std::move(key), val->toString());
}

void WritableNativeMap::putNativeArray(std::string key, WritableNativeArray* otherArray) {
  if (!otherArray) {
    putNull(std::move(key));
    return;
  }
  throwIfConsumed();
  map_.insert(key, otherArray->consume());
}

void WritableNativeMap::putNativeMap(std::string key, WritableNativeMap *otherMap) {
  if (!otherMap) {
    putNull(std::move(key));
    return;
  }
  throwIfConsumed();
  map_.insert(std::move(key), otherMap->consume());
}
```

[Android] [Changed] - fix nullable annotations in WritableArray, WritableMap
Pull Request resolved: https://github.com/facebook/react-native/pull/23397

Differential Revision: D14044014

Pulled By: cpojer

fbshipit-source-id: c44ea2e097e7b1156223b516aa640a181f0d4a9b
This commit is contained in:
Dulmandakh 2019-02-12 06:21:31 -08:00 committed by Facebook Github Bot
parent 77300ca91c
commit 7b33d6b0b9
6 changed files with 20 additions and 16 deletions

View File

@ -160,7 +160,7 @@ public class JavaOnlyArray implements ReadableArray, WritableArray {
} }
@Override @Override
public void pushString(@Nonnull String value) { public void pushString(@Nullable String value) {
mBackingList.add(value); mBackingList.add(value);
} }
@ -170,7 +170,7 @@ public class JavaOnlyArray implements ReadableArray, WritableArray {
} }
@Override @Override
public void pushMap(@Nonnull WritableMap map) { public void pushMap(@Nullable WritableMap map) {
mBackingList.add(map); mBackingList.add(map);
} }

View File

@ -12,6 +12,7 @@ import java.util.Iterator;
import java.util.Map; import java.util.Map;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/** /**
* Java {@link HashMap} backed implementation of {@link ReadableMap} and {@link WritableMap} * Java {@link HashMap} backed implementation of {@link ReadableMap} and {@link WritableMap}
@ -179,7 +180,7 @@ public class JavaOnlyMap implements ReadableMap, WritableMap {
} }
@Override @Override
public void putString(@Nonnull String key, @Nonnull String value) { public void putString(@Nonnull String key, @Nullable String value) {
mBackingMap.put(key, value); mBackingMap.put(key, value);
} }
@ -189,7 +190,7 @@ public class JavaOnlyMap implements ReadableMap, WritableMap {
} }
@Override @Override
public void putMap(@Nonnull String key, @Nonnull WritableMap value) { public void putMap(@Nonnull String key, @Nullable WritableMap value) {
mBackingMap.put(key, value); mBackingMap.put(key, value);
} }
@ -199,7 +200,7 @@ public class JavaOnlyMap implements ReadableMap, WritableMap {
} }
@Override @Override
public void putArray(@Nonnull String key, @Nonnull WritableArray value) { public void putArray(@Nonnull String key, @Nullable WritableArray value) {
mBackingMap.put(key, value); mBackingMap.put(key, value);
} }

View File

@ -8,6 +8,7 @@
package com.facebook.react.bridge; package com.facebook.react.bridge;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/** /**
* Interface for a mutable array. Used to pass arguments from Java to JS. * Interface for a mutable array. Used to pass arguments from Java to JS.
@ -18,7 +19,7 @@ public interface WritableArray extends ReadableArray {
void pushBoolean(boolean value); void pushBoolean(boolean value);
void pushDouble(double value); void pushDouble(double value);
void pushInt(int value); void pushInt(int value);
void pushString(@Nonnull String value); void pushString(@Nullable String value);
void pushArray(@Nonnull WritableArray array); void pushArray(@Nullable WritableArray array);
void pushMap(@Nonnull WritableMap map); void pushMap(@Nullable WritableMap map);
} }

View File

@ -8,6 +8,7 @@
package com.facebook.react.bridge; package com.facebook.react.bridge;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/** /**
* Interface for a mutable map. Used to pass arguments from Java to JS. * Interface for a mutable map. Used to pass arguments from Java to JS.
@ -18,9 +19,9 @@ public interface WritableMap extends ReadableMap {
void putBoolean(@Nonnull String key, boolean value); void putBoolean(@Nonnull String key, boolean value);
void putDouble(@Nonnull String key, double value); void putDouble(@Nonnull String key, double value);
void putInt(@Nonnull String key, int value); void putInt(@Nonnull String key, int value);
void putString(@Nonnull String key, @Nonnull String value); void putString(@Nonnull String key, @Nullable String value);
void putArray(@Nonnull String key, @Nonnull WritableArray value); void putArray(@Nonnull String key, @Nullable WritableArray value);
void putMap(@Nonnull String key, @Nonnull WritableMap value); void putMap(@Nonnull String key, @Nullable WritableMap value);
void merge(@Nonnull ReadableMap source); void merge(@Nonnull ReadableMap source);
} }

View File

@ -38,7 +38,7 @@ public class WritableNativeArray extends ReadableNativeArray implements Writable
@Override @Override
public native void pushInt(int value); public native void pushInt(int value);
@Override @Override
public native void pushString(@Nonnull String value); public native void pushString(@Nullable String value);
// Note: this consumes the map so do not reuse it. // Note: this consumes the map so do not reuse it.
@Override @Override
@ -50,7 +50,7 @@ public class WritableNativeArray extends ReadableNativeArray implements Writable
// Note: this consumes the map so do not reuse it. // Note: this consumes the map so do not reuse it.
@Override @Override
public void pushMap(@Nonnull WritableMap map) { public void pushMap(@Nullable WritableMap map) {
Assertions.assertCondition( Assertions.assertCondition(
map == null || map instanceof WritableNativeMap, "Illegal type provided"); map == null || map instanceof WritableNativeMap, "Illegal type provided");
pushNativeMap((WritableNativeMap) map); pushNativeMap((WritableNativeMap) map);

View File

@ -14,6 +14,7 @@ import com.facebook.infer.annotation.Assertions;
import com.facebook.proguard.annotations.DoNotStrip; import com.facebook.proguard.annotations.DoNotStrip;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/** /**
* Implementation of a write-only map stored in native memory. Use * Implementation of a write-only map stored in native memory. Use
@ -33,13 +34,13 @@ public class WritableNativeMap extends ReadableNativeMap implements WritableMap
@Override @Override
public native void putInt(@Nonnull String key, int value); public native void putInt(@Nonnull String key, int value);
@Override @Override
public native void putString(@Nonnull String key, @Nonnull String value); public native void putString(@Nonnull String key, @Nullable String value);
@Override @Override
public native void putNull(@NonNull String key); public native void putNull(@NonNull String key);
// Note: this consumes the map so do not reuse it. // Note: this consumes the map so do not reuse it.
@Override @Override
public void putMap(@Nonnull String key, @Nonnull WritableMap value) { public void putMap(@Nonnull String key, @Nullable WritableMap value) {
Assertions.assertCondition( Assertions.assertCondition(
value == null || value instanceof WritableNativeMap, "Illegal type provided"); value == null || value instanceof WritableNativeMap, "Illegal type provided");
putNativeMap(key, (WritableNativeMap) value); putNativeMap(key, (WritableNativeMap) value);
@ -47,7 +48,7 @@ public class WritableNativeMap extends ReadableNativeMap implements WritableMap
// Note: this consumes the map so do not reuse it. // Note: this consumes the map so do not reuse it.
@Override @Override
public void putArray(@Nonnull String key, @Nonnull WritableArray value) { public void putArray(@Nonnull String key, @Nullable WritableArray value) {
Assertions.assertCondition( Assertions.assertCondition(
value == null || value instanceof WritableNativeArray, "Illegal type provided"); value == null || value instanceof WritableNativeArray, "Illegal type provided");
putNativeArray(key, (WritableNativeArray) value); putNativeArray(key, (WritableNativeArray) value);