mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Simplify example code. (#483)
* Simplify example code. This was copied code from a different example that used the delegate annotations. * Make brackets consistent.
This commit is contained in:
committed by
Jesse Wilson
parent
7cab83a8f2
commit
dbdf48777c
@@ -26,8 +26,6 @@ import java.io.IOException;
|
|||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.LinkedHashSet;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
@@ -48,25 +46,18 @@ final class FallbackEnum {
|
|||||||
@Nullable @Override @SuppressWarnings("unchecked")
|
@Nullable @Override @SuppressWarnings("unchecked")
|
||||||
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
|
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
|
||||||
Class<?> rawType = Types.getRawType(type);
|
Class<?> rawType = Types.getRawType(type);
|
||||||
if (!rawType.isEnum()) return null;
|
if (!rawType.isEnum()) {
|
||||||
if (annotations.isEmpty()) {
|
return null;
|
||||||
|
}
|
||||||
|
if (annotations.size() != 1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Annotation annotation = annotations.iterator().next();
|
||||||
|
if (!(annotation instanceof Fallback)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Class<Enum> enumType = (Class<Enum>) rawType;
|
Class<Enum> enumType = (Class<Enum>) rawType;
|
||||||
Set<? extends Annotation> delegateAnnotations = null;
|
Enum<?> fallback = Enum.valueOf(enumType, ((Fallback) annotation).value());
|
||||||
Enum<?> fallback = null;
|
|
||||||
for (Annotation annotation : annotations) {
|
|
||||||
if (annotation instanceof Fallback) {
|
|
||||||
delegateAnnotations = new LinkedHashSet<>(annotations);
|
|
||||||
delegateAnnotations.remove(annotation);
|
|
||||||
delegateAnnotations = Collections.unmodifiableSet(delegateAnnotations);
|
|
||||||
fallback = Enum.valueOf(enumType, ((Fallback) annotation).value());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (delegateAnnotations == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return new FallbackEnumJsonAdapter<>(enumType, fallback);
|
return new FallbackEnumJsonAdapter<>(enumType, fallback);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user