Don't handle WildcardTypes in ClassJsonAdapter. (#406)
This commit is contained in:
parent
0a6e836762
commit
a210d89a55
2 changed files with 23 additions and 2 deletions
|
@ -45,7 +45,8 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
|
|||
public static final JsonAdapter.Factory FACTORY = new JsonAdapter.Factory() {
|
||||
@Override public @Nullable JsonAdapter<?> create(
|
||||
Type type, Set<? extends Annotation> annotations, Moshi moshi) {
|
||||
Class<?> rawType = Types.getRawType(type);
|
||||
if (!(type instanceof Class)) return null;
|
||||
Class<?> rawType = (Class<?>) type;
|
||||
if (rawType.isInterface() || rawType.isEnum()) return null;
|
||||
if (isPlatformType(rawType) && !Types.isAllowedPlatformType(rawType)) {
|
||||
throw new IllegalArgumentException("Platform "
|
||||
|
@ -71,7 +72,7 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
|
|||
|
||||
ClassFactory<Object> classFactory = ClassFactory.get(rawType);
|
||||
Map<String, FieldBinding<?>> fields = new TreeMap<>();
|
||||
for (Type t = type; t != Object.class; t = Types.getGenericSuperclass(t)) {
|
||||
for (Type t = rawType; t != Object.class; t = Types.getGenericSuperclass(t)) {
|
||||
createFieldBindings(moshi, t, fields);
|
||||
}
|
||||
return new ClassJsonAdapter<>(classFactory, fields).nullSafe();
|
||||
|
|
|
@ -534,6 +534,26 @@ public final class MoshiTest {
|
|||
assertThat(adapter.toJson(null)).isEqualTo("null");
|
||||
}
|
||||
|
||||
@Test public void upperBoundedWildcardsAreNotHandled() {
|
||||
Moshi moshi = new Moshi.Builder().build();
|
||||
try {
|
||||
moshi.adapter(Types.subtypeOf(String.class));
|
||||
fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e).hasMessage("No JsonAdapter for ? extends java.lang.String annotated []");
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void lowerBoundedWildcardsAreNotHandled() {
|
||||
Moshi moshi = new Moshi.Builder().build();
|
||||
try {
|
||||
moshi.adapter(Types.supertypeOf(String.class));
|
||||
fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e).hasMessage("No JsonAdapter for ? super java.lang.String annotated []");
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void addNullFails() throws Exception {
|
||||
Type type = Object.class;
|
||||
Class<? extends Annotation> annotation = Annotation.class;
|
||||
|
|
Loading…
Reference in a new issue