Merge pull request #235 from NightlyNexus/master

Improve error message for qualified platform types
This commit is contained in:
Jesse Wilson
2017-01-30 20:56:34 -05:00
committed by GitHub
3 changed files with 31 additions and 2 deletions

View File

@@ -111,11 +111,11 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
*/ */
private boolean isPlatformType(Class<?> rawType) { private boolean isPlatformType(Class<?> rawType) {
String name = rawType.getName(); String name = rawType.getName();
return name.startsWith("android.") return (name.startsWith("android.")
|| name.startsWith("java.") || name.startsWith("java.")
|| name.startsWith("javax.") || name.startsWith("javax.")
|| name.startsWith("kotlin.") || name.startsWith("kotlin.")
|| name.startsWith("scala."); || name.startsWith("scala.")) && !Types.isAllowedPlatformType(rawType);
} }
/** Returns true if fields with {@code modifiers} are included in the emitted JSON. */ /** Returns true if fields with {@code modifiers} are included in the emitted JSON. */

View File

@@ -423,6 +423,22 @@ public final class Types {
return unknown; return unknown;
} }
/**
* 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;
}
private static int indexOf(Object[] array, Object toFind) { private static int indexOf(Object[] array, Object toFind) {
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
if (toFind.equals(array[i])) return i; if (toFind.equals(array[i])) return i;

View File

@@ -776,6 +776,19 @@ public final class MoshiTest {
} }
} }
@Test public void noTypeAdapterForQualifiedPlatformType() throws Exception {
Moshi moshi = new Moshi.Builder().build();
Field uppercaseStringField = MoshiTest.class.getDeclaredField("uppercaseString");
try {
moshi.adapter(uppercaseStringField.getGenericType(),
Util.jsonAnnotations(uppercaseStringField));
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("No JsonAdapter for class java.lang.String "
+ "annotated [@com.squareup.moshi.MoshiTest$Uppercase()]");
}
}
@Test public void objectArray() throws Exception { @Test public void objectArray() throws Exception {
Moshi moshi = new Moshi.Builder().build(); Moshi moshi = new Moshi.Builder().build();
JsonAdapter<String[]> adapter = moshi.adapter(String[].class); JsonAdapter<String[]> adapter = moshi.adapter(String[].class);