Skip to content
Permalink
Browse files

Better error messages for ReadableNativeMap ClassCastExceptions

Reviewed By: mdvacca

Differential Revision: D7800266

fbshipit-source-id: 3fa224d3adc54a7bc59a6a37b9ad6928ac33ceb6
  • Loading branch information...
ayc1 authored and facebook-github-bot committed May 7, 2018
1 parent 0125813 commit 30d06b42862fc5e8704e109db652d62f86f8eabc
Showing with 28 additions and 6 deletions.
  1. +28 −6 ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java
@@ -121,20 +121,41 @@ private Object getValue(String name) {
}
throw new NoSuchKeyException(name);
}

private <T> T getValue(String name, Class<T> type) {
Object value = getValue(name);
checkInstance(name, value, type);
return (T) value;
}

private @Nullable Object getNullableValue(String name) {
if (hasKey(name)) {
return getLocalMap().get(name);
}
throw new NoSuchKeyException(name);
}

private @Nullable <T> T getNullableValue(String name, Class<T> type) {
Object value = getNullableValue(name);
checkInstance(name, value, type);
return (T) value;
}

private void checkInstance(String name, Object value, Class type) {
if (value != null && !type.isInstance(value)) {
throw new ClassCastException(
"Value for " + name + " cannot be cast from " +
value.getClass().getSimpleName() + " to " + type.getSimpleName());
}
}

@Override
public boolean getBoolean(String name) {
if (mUseNativeAccessor) {
mJniCallCounter++;
return getBooleanNative(name);
}
return ((Boolean) getValue(name)).booleanValue();
return getValue(name, Boolean.class).booleanValue();
}
private native boolean getBooleanNative(String name);

@@ -144,7 +165,7 @@ public double getDouble(String name) {
mJniCallCounter++;
return getDoubleNative(name);
}
return ((Double) getValue(name)).doubleValue();
return getValue(name, Double.class).doubleValue();
}
private native double getDoubleNative(String name);

@@ -154,8 +175,9 @@ public int getInt(String name) {
mJniCallCounter++;
return getIntNative(name);
}

// All numbers coming out of native are doubles, so cast here then truncate
return ((Double) getValue(name)).intValue();
return getValue(name, Double.class).intValue();
}
private native int getIntNative(String name);

@@ -165,7 +187,7 @@ public int getInt(String name) {
mJniCallCounter++;
return getStringNative(name);
}
return (String) getNullableValue(name);
return getNullableValue(name, String.class);
}
private native String getStringNative(String name);

@@ -175,7 +197,7 @@ public int getInt(String name) {
mJniCallCounter++;
return getArrayNative(name);
}
return (ReadableArray) getNullableValue(name);
return getNullableValue(name, ReadableArray.class);
}
private native ReadableNativeArray getArrayNative(String name);

@@ -185,7 +207,7 @@ public int getInt(String name) {
mJniCallCounter++;
return getMapNative(name);
}
return (ReadableNativeMap) getNullableValue(name);
return getNullableValue(name, ReadableNativeMap.class);
}
private native ReadableNativeMap getMapNative(String name);

0 comments on commit 30d06b4

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