@SuppressWarnings({"rawtypes", "unchecked"}) public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) { Class<? super T> clazz = targetType.getRawType(); JsonAdapter annotation = clazz.getAnnotation(JsonAdapter.class); if (annotation == null) return null; TypeAdapter adapter = getAnnotationTypeAdapter(gson, constructorConstructor, annotation); return adapter; }
static TypeAdapter<?> getAnnotationTypeAdapter( Gson gson, ConstructorConstructor constructorConstructor, JsonAdapter annotation) { Class<? extends TypeAdapter<?>> adapterClass = annotation.value(); ObjectConstructor<? extends TypeAdapter<?>> constructor = constructorConstructor.get(TypeToken.get(adapterClass)); TypeAdapter<?> adapter = constructor.construct(); Gson.$$Internal.addGeneratedTypeAdapter(gson, adapter); return adapter; }