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 { }