Skip to content

Commit

Permalink
Better error messages for ReadableNativeMap ClassCastExceptions
Browse files Browse the repository at this point in the history
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 30d06b4
Showing 1 changed file with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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

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

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

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

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

Expand Down

0 comments on commit 30d06b4

Please sign in to comment.