mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Throw a better message attempting to adapt a platform type.
This commit is contained in:
@@ -35,7 +35,14 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
|
||||
@Override public JsonAdapter<?> create(
|
||||
Type type, Set<? extends Annotation> annotations, Moshi moshi) {
|
||||
Class<?> rawType = Types.getRawType(type);
|
||||
if (rawType.isInterface() || rawType.isEnum() || isPlatformType(rawType)) return null;
|
||||
if (rawType.isInterface() || rawType.isEnum()) return null;
|
||||
if (isPlatformType(rawType)) {
|
||||
throw new IllegalArgumentException("Platform "
|
||||
+ type
|
||||
+ " annotated "
|
||||
+ annotations
|
||||
+ " requires explicit JsonAdapter to be registered");
|
||||
}
|
||||
if (!annotations.isEmpty()) return null;
|
||||
|
||||
if (rawType.getEnclosingClass() != null && !Modifier.isStatic(rawType.getModifiers())) {
|
||||
|
4
moshi/src/test/java/android/util/Pair.java
Normal file
4
moshi/src/test/java/android/util/Pair.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package android.util;
|
||||
|
||||
public final class Pair {
|
||||
}
|
@@ -21,8 +21,6 @@ import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.SimpleTimeZone;
|
||||
import java.util.UUID;
|
||||
import javax.crypto.KeyGenerator;
|
||||
import okio.Buffer;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -329,11 +327,6 @@ public final class ClassJsonAdapterTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void platformClassNotSupported() throws Exception {
|
||||
assertThat(ClassJsonAdapter.FACTORY.create(UUID.class, NO_ANNOTATIONS, moshi)).isNull();
|
||||
assertThat(ClassJsonAdapter.FACTORY.create(KeyGenerator.class, NO_ANNOTATIONS, moshi)).isNull();
|
||||
}
|
||||
|
||||
@Test public void anonymousClassNotSupported() throws Exception {
|
||||
Comparator<Object> c = new Comparator<Object>() {
|
||||
@Override public int compare(Object a, Object b) {
|
||||
|
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.squareup.moshi;
|
||||
|
||||
import android.util.Pair;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -27,6 +29,7 @@ import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import javax.crypto.KeyGenerator;
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.squareup.moshi.TestUtil.newReader;
|
||||
@@ -707,6 +710,31 @@ public final class MoshiTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void platformTypeThrows() throws IOException {
|
||||
Moshi moshi = new Moshi.Builder().build();
|
||||
try {
|
||||
moshi.adapter(File.class);
|
||||
fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e).hasMessage(
|
||||
"Platform class java.io.File annotated [] 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");
|
||||
}
|
||||
try {
|
||||
moshi.adapter(Pair.class);
|
||||
fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e).hasMessage(
|
||||
"Platform class android.util.Pair annotated [] requires explicit JsonAdapter to be registered");
|
||||
}
|
||||
}
|
||||
|
||||
static class Pizza {
|
||||
final int diameter;
|
||||
final boolean extraCheese;
|
||||
|
Reference in New Issue
Block a user