From 810199a442376ba0dcaa2c953cc4bd86ca4b8d94 Mon Sep 17 00:00:00 2001 From: Eric Cochran Date: Tue, 18 Sep 2018 17:55:28 -0700 Subject: [PATCH] Disallow Object for RuntimeJsonAdapterFactory's base type. --- .../moshi/adapters/RuntimeJsonAdapterFactory.java | 14 ++++++++------ .../adapters/RuntimeJsonAdapterFactoryTest.java | 10 ++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/adapters/src/main/java/com/squareup/moshi/adapters/RuntimeJsonAdapterFactory.java b/adapters/src/main/java/com/squareup/moshi/adapters/RuntimeJsonAdapterFactory.java index 1e805c1..bd833a3 100644 --- a/adapters/src/main/java/com/squareup/moshi/adapters/RuntimeJsonAdapterFactory.java +++ b/adapters/src/main/java/com/squareup/moshi/adapters/RuntimeJsonAdapterFactory.java @@ -21,7 +21,6 @@ import com.squareup.moshi.JsonReader; import com.squareup.moshi.JsonWriter; import com.squareup.moshi.Moshi; import com.squareup.moshi.Types; -import com.squareup.moshi.internal.Util; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Type; @@ -41,7 +40,7 @@ public final class RuntimeJsonAdapterFactory implements JsonAdapter.Factory { final Map labelToType = new LinkedHashMap<>(); /** - * @param baseType The base type for which this factory will create adapters. + * @param baseType The base type for which this factory will create adapters. Cannot be Object. * @param labelKey The key in the JSON object whose value determines the type to which to map the * JSON object. */ @@ -49,6 +48,10 @@ public final class RuntimeJsonAdapterFactory implements JsonAdapter.Factory { public static RuntimeJsonAdapterFactory of(Class baseType, String labelKey) { if (baseType == null) throw new NullPointerException("baseType == null"); if (labelKey == null) throw new NullPointerException("labelKey == null"); + if (baseType == Object.class) { + throw new IllegalArgumentException( + "The base type must not be Object. Consider using a marker interface."); + } return new RuntimeJsonAdapterFactory<>(baseType, labelKey); } @@ -86,10 +89,9 @@ public final class RuntimeJsonAdapterFactory implements JsonAdapter.Factory { typeToLabel.put(typeValue, label); labelToAdapter.put(label, moshi.adapter(typeValue)); } - JsonAdapter objectJsonAdapter = moshi.nextAdapter( - this, Object.class, Util.NO_ANNOTATIONS); - return new RuntimeJsonAdapter(labelKey, labelToAdapter, typeToLabel, objectJsonAdapter) - .nullSafe(); + JsonAdapter objectJsonAdapter = moshi.adapter(Object.class); + return new RuntimeJsonAdapter(labelKey, labelToAdapter, typeToLabel, + objectJsonAdapter).nullSafe(); } static final class RuntimeJsonAdapter extends JsonAdapter { diff --git a/adapters/src/test/java/com/squareup/moshi/adapters/RuntimeJsonAdapterFactoryTest.java b/adapters/src/test/java/com/squareup/moshi/adapters/RuntimeJsonAdapterFactoryTest.java index 6e04f53..2998aaa 100644 --- a/adapters/src/test/java/com/squareup/moshi/adapters/RuntimeJsonAdapterFactoryTest.java +++ b/adapters/src/test/java/com/squareup/moshi/adapters/RuntimeJsonAdapterFactoryTest.java @@ -171,6 +171,16 @@ public final class RuntimeJsonAdapterFactoryTest { assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT); } + @Test public void disallowObjectBaseType() { + try { + RuntimeJsonAdapterFactory.of(Object.class, "type"); + fail(); + } catch (IllegalArgumentException expected) { + assertThat(expected).hasMessage( + "The base type must not be Object. Consider using a marker interface."); + } + } + interface Message { }