From a920b9be3aba4aabdcc89d5d2f559a95cf70ca8e Mon Sep 17 00:00:00 2001 From: Eric Cochran Date: Mon, 19 Nov 2018 02:23:25 -0800 Subject: [PATCH] Improve error message for platform types. The error message about platform types will never contain qualifiers. Also, simplify platform type check. --- .../kotlin/reflect/KotlinJsonAdapterTest.kt | 4 ++-- .../com/squareup/moshi/ClassJsonAdapter.java | 10 ++++---- .../main/java/com/squareup/moshi/Types.java | 16 ------------- .../java/com/squareup/moshi/MoshiTest.java | 24 +++++++++---------- 4 files changed, 18 insertions(+), 36 deletions(-) diff --git a/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/reflect/KotlinJsonAdapterTest.kt b/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/reflect/KotlinJsonAdapterTest.kt index d15e3c0..cb695f9 100644 --- a/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/reflect/KotlinJsonAdapterTest.kt +++ b/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/reflect/KotlinJsonAdapterTest.kt @@ -481,8 +481,8 @@ class KotlinJsonAdapterTest { moshi.adapter(Triple::class.java) fail() } catch (e: IllegalArgumentException) { - assertThat(e).hasMessage("Platform class kotlin.Triple (with no annotations) " - + "requires explicit JsonAdapter to be registered") + assertThat(e).hasMessage( + "Platform class kotlin.Triple requires explicit JsonAdapter to be registered") } } diff --git a/moshi/src/main/java/com/squareup/moshi/ClassJsonAdapter.java b/moshi/src/main/java/com/squareup/moshi/ClassJsonAdapter.java index 71d4534..d2a347c 100644 --- a/moshi/src/main/java/com/squareup/moshi/ClassJsonAdapter.java +++ b/moshi/src/main/java/com/squareup/moshi/ClassJsonAdapter.java @@ -29,7 +29,6 @@ import java.util.TreeMap; import javax.annotation.Nullable; import static com.squareup.moshi.internal.Util.resolve; -import static com.squareup.moshi.internal.Util.typeAnnotatedWithAnnotations; /** * Emits a regular class as a JSON object by mapping Java fields to JSON object properties. @@ -55,12 +54,11 @@ final class ClassJsonAdapter extends JsonAdapter { } Class rawType = Types.getRawType(type); if (rawType.isInterface() || rawType.isEnum()) return null; - if (Util.isPlatformType(rawType) && !Types.isAllowedPlatformType(rawType)) { - throw new IllegalArgumentException("Platform " - + typeAnnotatedWithAnnotations(type, annotations) - + " requires explicit JsonAdapter to be registered"); - } if (!annotations.isEmpty()) return null; + if (Util.isPlatformType(rawType)) { + throw new IllegalArgumentException( + "Platform " + type + " requires explicit JsonAdapter to be registered"); + } if (rawType.isAnonymousClass()) { throw new IllegalArgumentException("Cannot serialize anonymous class " + rawType.getName()); diff --git a/moshi/src/main/java/com/squareup/moshi/Types.java b/moshi/src/main/java/com/squareup/moshi/Types.java index 6997c48..a5d5c17 100644 --- a/moshi/src/main/java/com/squareup/moshi/Types.java +++ b/moshi/src/main/java/com/squareup/moshi/Types.java @@ -326,20 +326,4 @@ public final class Types { return null; } } - - /** - * Returns true if this is a Type supported by {@link StandardJsonAdapters#FACTORY}. - */ - static boolean isAllowedPlatformType(Type type) { - return type == Boolean.class - || type == Byte.class - || type == Character.class - || type == Double.class - || type == Float.class - || type == Integer.class - || type == Long.class - || type == Short.class - || type == String.class - || type == Object.class; - } } diff --git a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java index 1303b63..41b4492 100644 --- a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java +++ b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java @@ -937,22 +937,22 @@ public final class MoshiTest { moshi.adapter(File.class); fail(); } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Platform class java.io.File (with no annotations) " - + "requires explicit JsonAdapter to be registered"); + assertThat(e).hasMessage( + "Platform class java.io.File requires explicit JsonAdapter to be registered"); } try { moshi.adapter(KeyGenerator.class); fail(); } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Platform class javax.crypto.KeyGenerator (with no annotations) " - + "requires explicit JsonAdapter to be registered"); + assertThat(e).hasMessage("Platform class javax.crypto.KeyGenerator requires explicit " + + "JsonAdapter to be registered"); } try { moshi.adapter(Pair.class); fail(); } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Platform class android.util.Pair (with no annotations) " - + "requires explicit JsonAdapter to be registered"); + assertThat(e).hasMessage( + "Platform class android.util.Pair requires explicit JsonAdapter to be registered"); } } @@ -975,7 +975,7 @@ public final class MoshiTest { fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( - "Platform java.util.ArrayList (with no annotations) requires explicit " + "Platform java.util.ArrayList requires explicit " + "JsonAdapter to be registered" + "\nfor java.util.ArrayList strings" + "\nfor class com.squareup.moshi.MoshiTest$HasPlatformType" @@ -983,7 +983,7 @@ public final class MoshiTest { + "com.squareup.moshi.MoshiTest$HasPlatformType>"); assertThat(e).hasCauseExactlyInstanceOf(IllegalArgumentException.class); assertThat(e.getCause()).hasMessage("Platform java.util.ArrayList " - + "(with no annotations) requires explicit JsonAdapter to be registered"); + + "requires explicit JsonAdapter to be registered"); } } @@ -994,14 +994,14 @@ public final class MoshiTest { fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( - "Platform java.util.ArrayList (with no annotations) requires explicit " + "Platform java.util.ArrayList requires explicit " + "JsonAdapter to be registered" + "\nfor java.util.ArrayList strings" + "\nfor class com.squareup.moshi.MoshiTest$HasPlatformType hasPlatformType" + "\nfor class com.squareup.moshi.MoshiTest$HasPlatformType$Wrapper"); assertThat(e).hasCauseExactlyInstanceOf(IllegalArgumentException.class); assertThat(e.getCause()).hasMessage("Platform java.util.ArrayList " - + "(with no annotations) requires explicit JsonAdapter to be registered"); + + "requires explicit JsonAdapter to be registered"); } } @@ -1012,7 +1012,7 @@ public final class MoshiTest { fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( - "Platform java.util.ArrayList (with no annotations) requires explicit " + "Platform java.util.ArrayList requires explicit " + "JsonAdapter to be registered" + "\nfor java.util.ArrayList strings" + "\nfor class com.squareup.moshi.MoshiTest$HasPlatformType" @@ -1020,7 +1020,7 @@ public final class MoshiTest { + "\nfor class com.squareup.moshi.MoshiTest$HasPlatformType$ListWrapper"); assertThat(e).hasCauseExactlyInstanceOf(IllegalArgumentException.class); assertThat(e.getCause()).hasMessage("Platform java.util.ArrayList " - + "(with no annotations) requires explicit JsonAdapter to be registered"); + + "requires explicit JsonAdapter to be registered"); } }