From 3df2db1f1696c49dc01d1e8a60073e5a7f39a999 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Thu, 12 Apr 2012 18:27:48 +0000 Subject: [PATCH] Don't permit a type adapter for String to be registered. --- .../java/com/google/gson/GsonBuilder.java | 4 +-- .../java/com/google/gson/GsonBuilderTest.java | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index b81e62a8..f8bc01c7 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -446,7 +446,7 @@ public final class GsonBuilder { || typeAdapter instanceof JsonDeserializer || typeAdapter instanceof InstanceCreator || typeAdapter instanceof TypeAdapter); - if (Primitives.isPrimitive(type) || Primitives.isWrapperType(type)) { + if (Primitives.isPrimitive(type) || Primitives.isWrapperType(type) || type == String.class) { throw new IllegalArgumentException( "Cannot register type adapters for " + type); } @@ -485,7 +485,7 @@ public final class GsonBuilder { * * @param baseType the class definition for the type adapter being registered for the base class * or interface - * @param typeAdapter This object must implement at least one of {@link TypeAdapter}, + * @param typeAdapter This object must implement at least one of {@link TypeAdapter}, * {@link JsonSerializer} or {@link JsonDeserializer} interfaces. * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @since 1.7 diff --git a/gson/src/test/java/com/google/gson/GsonBuilderTest.java b/gson/src/test/java/com/google/gson/GsonBuilderTest.java index 9745a18c..f9a104f0 100755 --- a/gson/src/test/java/com/google/gson/GsonBuilderTest.java +++ b/gson/src/test/java/com/google/gson/GsonBuilderTest.java @@ -16,7 +16,11 @@ package com.google.gson; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import java.io.IOException; import java.lang.reflect.Modifier; +import java.lang.reflect.Type; import junit.framework.TestCase; /** @@ -25,6 +29,14 @@ import junit.framework.TestCase; * @author Inderjeet Singh */ public class GsonBuilderTest extends TestCase { + private static final TypeAdapter NULL_TYPE_ADAPTER = new TypeAdapter() { + @Override public void write(JsonWriter out, Object value) { + throw new AssertionError(); + } + @Override public Object read(JsonReader in) { + throw new AssertionError(); + } + }; public void testCreatingMoreThanOnce() { GsonBuilder builder = new GsonBuilder(); @@ -39,6 +51,24 @@ public class GsonBuilderTest extends TestCase { assertEquals("{\"d\":\"d\"}", gson.toJson(new HasModifiers())); } + public void testRegisterTypeAdapterForUnsupportedType() { + Type[] types = { + byte.class, + int.class, + double.class, + Short.class, + Long.class, + String.class, + }; + for (Type type : types) { + try { + new GsonBuilder().registerTypeAdapter(type, NULL_TYPE_ADAPTER); + fail(type.toString()); + } catch (IllegalArgumentException e) { + } + } + } + @SuppressWarnings("unused") static class HasModifiers { private String a = "a";