diff --git a/moshi/src/main/java/com/squareup/moshi/Moshi.java b/moshi/src/main/java/com/squareup/moshi/Moshi.java index 315f6fb..509d18b 100644 --- a/moshi/src/main/java/com/squareup/moshi/Moshi.java +++ b/moshi/src/main/java/com/squareup/moshi/Moshi.java @@ -66,6 +66,9 @@ public final class Moshi { @CheckReturnValue public JsonAdapter adapter(Type type, Class annotationType) { + if (annotationType == null) { + throw new NullPointerException("annotationType == null"); + } return adapter(type, Collections.singleton(Types.createJsonQualifierImplementation(annotationType))); } @@ -73,6 +76,10 @@ public final class Moshi { @CheckReturnValue @SuppressWarnings("unchecked") // Factories are required to return only matching JsonAdapters. public JsonAdapter adapter(Type type, Set annotations) { + if (annotations == null) { + throw new NullPointerException("annotations == null"); + } + type = Types.canonicalize(type); // If there's an equivalent adapter in the cache, we're done! diff --git a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java index 12be431..58fa8ca 100644 --- a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java +++ b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java @@ -724,8 +724,21 @@ public final class MoshiTest { .isEqualTo("{\"shout\":\"WHAT'S UP\",\"speak\":\"Yo dog\"}"); } - @Uppercase - static String uppercaseString; + @Test public void adapterLookupDisallowsNullAnnotations() { + Moshi moshi = new Moshi.Builder().build(); + try { + moshi.adapter(String.class, (Class) null); + fail(); + } catch (NullPointerException expected) { + assertThat(expected).hasMessage("annotationType == null"); + } + try { + moshi.adapter(String.class, (Set) null); + fail(); + } catch (NullPointerException expected) { + assertThat(expected).hasMessage("annotations == null"); + } + } @Test public void nextJsonAdapterDisallowsNullAnnotations() throws Exception { JsonAdapter.Factory badFactory = new JsonAdapter.Factory() { @@ -744,6 +757,9 @@ public final class MoshiTest { } } + @Uppercase + static String uppercaseString; + @Test public void delegatingJsonAdapterFactory() throws Exception { Moshi moshi = new Moshi.Builder() .add(new UppercaseAdapterFactory())