Make "no adapter" error message friendlier.

In the very common case, there are no JsonQualifier annotations for the type.
This commit is contained in:
Eric Cochran
2018-04-08 00:48:38 -07:00
parent 2cc878da81
commit ba1318cc45
8 changed files with 28 additions and 20 deletions

View File

@@ -29,6 +29,7 @@ import javax.annotation.Nullable;
import static com.squareup.moshi.internal.Util.canonicalize;
import static com.squareup.moshi.internal.Util.jsonAnnotations;
import static com.squareup.moshi.internal.Util.typeAnnotatedWithAnnotations;
final class AdapterMethodsFactory implements JsonAdapter.Factory {
private final List<AdapterMethod> toAdapters;
@@ -52,7 +53,7 @@ final class AdapterMethodsFactory implements JsonAdapter.Factory {
} catch (IllegalArgumentException e) {
String missingAnnotation = toAdapter == null ? "@ToJson" : "@FromJson";
throw new IllegalArgumentException("No " + missingAnnotation + " adapter for "
+ type + " annotated " + annotations);
+ typeAnnotatedWithAnnotations(type, annotations));
}
} else {
delegate = null;

View File

@@ -29,6 +29,7 @@ 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.
@@ -56,9 +57,7 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
if (rawType.isInterface() || rawType.isEnum()) return null;
if (Util.isPlatformType(rawType) && !Types.isAllowedPlatformType(rawType)) {
throw new IllegalArgumentException("Platform "
+ type
+ " annotated "
+ annotations
+ typeAnnotatedWithAnnotations(type, annotations)
+ " requires explicit JsonAdapter to be registered");
}
if (!annotations.isEmpty()) return null;

View File

@@ -31,6 +31,7 @@ import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import static com.squareup.moshi.internal.Util.canonicalize;
import static com.squareup.moshi.internal.Util.typeAnnotatedWithAnnotations;
/**
* Coordinates binding between JSON values and Java objects.
@@ -139,7 +140,8 @@ public final class Moshi {
}
}
throw new IllegalArgumentException("No JsonAdapter for " + type + " annotated " + annotations);
throw new IllegalArgumentException(
"No JsonAdapter for " + typeAnnotatedWithAnnotations(type, annotations));
}
@CheckReturnValue
@@ -159,7 +161,7 @@ public final class Moshi {
if (result != null) return result;
}
throw new IllegalArgumentException("No next JsonAdapter for "
+ type + " annotated " + annotations);
+ typeAnnotatedWithAnnotations(type, annotations));
}
/** Returns a new builder containing all custom factories used by the current instance. */

View File

@@ -430,4 +430,9 @@ public final class Util {
}
}
}
public static String typeAnnotatedWithAnnotations(Type type,
Set<? extends Annotation> annotations) {
return type + (annotations.isEmpty() ? " (with no annotations)" : " annotated " + annotations);
}
}

View File

@@ -462,7 +462,7 @@ public final class AdapterMethodsTest {
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("No @FromJson adapter for interface "
+ "com.squareup.moshi.AdapterMethodsTest$Shape annotated []");
+ "com.squareup.moshi.AdapterMethodsTest$Shape (with no annotations)");
}
}
@@ -481,7 +481,7 @@ public final class AdapterMethodsTest {
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("No @ToJson adapter for interface "
+ "com.squareup.moshi.AdapterMethodsTest$Shape annotated []");
+ "com.squareup.moshi.AdapterMethodsTest$Shape (with no annotations)");
}
}

View File

@@ -541,7 +541,8 @@ public final class MoshiTest {
moshi.adapter(Types.subtypeOf(String.class));
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("No JsonAdapter for ? extends java.lang.String annotated []");
assertThat(e).hasMessage(
"No JsonAdapter for ? extends java.lang.String (with no annotations)");
}
}
@@ -551,7 +552,7 @@ public final class MoshiTest {
moshi.adapter(Types.supertypeOf(String.class));
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("No JsonAdapter for ? super java.lang.String annotated []");
assertThat(e).hasMessage("No JsonAdapter for ? super java.lang.String (with no annotations)");
}
}
@@ -908,22 +909,22 @@ public final class MoshiTest {
moshi.adapter(File.class);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage(
"Platform class java.io.File annotated [] requires explicit JsonAdapter to be registered");
assertThat(e).hasMessage("Platform class java.io.File (with no annotations) "
+ "requires explicit JsonAdapter to be registered");
}
try {
moshi.adapter(KeyGenerator.class);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage(
"Platform class javax.crypto.KeyGenerator annotated [] requires explicit JsonAdapter to be registered");
assertThat(e).hasMessage("Platform class javax.crypto.KeyGenerator (with no annotations) "
+ "requires explicit JsonAdapter to be registered");
}
try {
moshi.adapter(Pair.class);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage(
"Platform class android.util.Pair annotated [] requires explicit JsonAdapter to be registered");
assertThat(e).hasMessage("Platform class android.util.Pair (with no annotations) "
+ "requires explicit JsonAdapter to be registered");
}
}