Do not leak out a ClassCastException to our clients.

This commit is contained in:
Joel Leitch 2009-03-27 19:43:33 +00:00
parent 727dbcdd88
commit c9cb05fc72
4 changed files with 80 additions and 16 deletions

View File

@ -402,7 +402,6 @@ public final class Gson {
* @throws JsonParseException if json is not a valid representation for an object of type typeOfT * @throws JsonParseException if json is not a valid representation for an object of type typeOfT
* @since 1.3 * @since 1.3
*/ */
@SuppressWarnings("unchecked")
public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonParseException { public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonParseException {
T target = classOfT.cast(fromJson(json, (Type) classOfT)); T target = classOfT.cast(fromJson(json, (Type) classOfT));
return target; return target;

View File

@ -73,10 +73,14 @@ public abstract class JsonElement {
* first. * first.
* *
* @return get this element as a {@link JsonObject}. * @return get this element as a {@link JsonObject}.
* @throws ClassCastException if the element is of another type. * @throws IllegalStateException if the element is of another type.
*/ */
public JsonObject getAsJsonObject() { public JsonObject getAsJsonObject() {
return (JsonObject) this; if (isJsonObject()) {
return (JsonObject) this;
} else {
throw new IllegalStateException("This is not a JSON Object.");
}
} }
/** /**
@ -86,10 +90,14 @@ public abstract class JsonElement {
* first. * first.
* *
* @return get this element as a {@link JsonArray}. * @return get this element as a {@link JsonArray}.
* @throws ClassCastException if the element is of another type. * @throws IllegalStateException if the element is of another type.
*/ */
public JsonArray getAsJsonArray() { public JsonArray getAsJsonArray() {
return (JsonArray) this; if (isJsonArray()) {
return (JsonArray) this;
} else {
throw new IllegalStateException("This is not a JSON Array.");
}
} }
/** /**
@ -99,10 +107,14 @@ public abstract class JsonElement {
* first. * first.
* *
* @return get this element as a {@link JsonPrimitive}. * @return get this element as a {@link JsonPrimitive}.
* @throws ClassCastException if the element is of another type. * @throws IllegalStateException if the element is of another type.
*/ */
public JsonPrimitive getAsJsonPrimitive() { public JsonPrimitive getAsJsonPrimitive() {
return (JsonPrimitive) this; if (isJsonPrimitive()) {
return (JsonPrimitive) this;
} else {
throw new IllegalStateException("This is not a JSON Primitive.");
}
} }
/** /**
@ -112,11 +124,15 @@ public abstract class JsonElement {
* first. * first.
* *
* @return get this element as a {@link JsonNull}. * @return get this element as a {@link JsonNull}.
* @throws ClassCastException if the element is of another type. * @throws IllegalStateException if the element is of another type.
* @since 1.2 * @since 1.2
*/ */
public JsonNull getAsJsonNull() { public JsonNull getAsJsonNull() {
return (JsonNull) this; if (isJsonNull()) {
return (JsonNull) this;
} else {
throw new IllegalStateException("This is not a JSON Null.");
}
} }
/** /**

View File

@ -16,14 +16,18 @@
package com.google.gson.functional; package com.google.gson.functional;
import com.google.gson.Gson; import java.io.StringReader;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.common.TestTypes.BagOfPrimitives;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.io.StringReader; import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.common.TestTypes.Nested;
/** /**
* Functional tests for that use JsonParser and related Gson methods * Functional tests for that use JsonParser and related Gson methods
@ -49,6 +53,49 @@ public class JsonParserTest extends TestCase {
assertEquals("foo", target.stringValue); assertEquals("foo", target.stringValue);
} }
public void testBadTypeForDeserializingCustomTree() {
JsonObject obj = new JsonObject();
obj.addProperty("stringValue", "foo");
obj.addProperty("intValue", 11);
JsonArray array = new JsonArray();
array.add(obj);
try {
gson.fromJson(array, BagOfPrimitives.class);
fail("BagOfPrimitives is not an array");
} catch (UnsupportedOperationException expected) { }
}
public void testBadFieldTypeForCustomDeserializerCustomTree() {
JsonArray array = new JsonArray();
array.add(new JsonPrimitive("blah"));
JsonObject obj = new JsonObject();
obj.addProperty("stringValue", "foo");
obj.addProperty("intValue", 11);
obj.add("longValue", array);
try {
gson.fromJson(obj, BagOfPrimitives.class);
fail("BagOfPrimitives is not an array");
} catch (JsonParseException expected) { }
}
public void testBadFieldTypeForDeserializingCustomTree() {
JsonArray array = new JsonArray();
array.add(new JsonPrimitive("blah"));
JsonObject primitive1 = new JsonObject();
primitive1.addProperty("string", "foo");
primitive1.addProperty("intValue", 11);
JsonObject obj = new JsonObject();
obj.add("primitive1", primitive1);
obj.add("primitive2", array);
try {
gson.fromJson(obj, Nested.class);
fail("Nested has field BagOfPrimitives which is not an array");
} catch (IllegalStateException expected) { }
}
public void testChangingCustomTreeAndDeserializing() { public void testChangingCustomTreeAndDeserializing() {
StringReader json = StringReader json =
new StringReader("{'stringValue':'no message','intValue':10,'longValue':20}"); new StringReader("{'stringValue':'no message','intValue':10,'longValue':20}");

View File

@ -473,8 +473,10 @@ public class ParameterizedTypesTest extends TestCase {
private interface Immutable { private interface Immutable {
} }
public static final class Amount<Q extends Quantity> implements public static final class Amount<Q extends Quantity>
Measurable<Q>, Field<Amount<?>>, Serializable, Immutable { implements Measurable<Q>, Field<Amount<?>>, Serializable, Immutable {
private static final long serialVersionUID = -7560491093120970437L;
int value = 30; int value = 30;
} }