如果类型不匹配
null,
Converter.Factory则需要从中返回。将
Class<?>周围保持在一个字段中进行比较。
@Overridepublic Converter<ResponseBody, ?> fromResponseBody(Type type, Annotation[] annotations) { if (!this.type.equals(type)) { return null; } return new HALResponseBodyConverter<>(gson);}这将允许使用多个实例,因为每个实例仅适用于其自己的类型。
就是说,但是,您可能仅使用单个转换器并从
Type传入的类中拉取类就可以摆脱现实。
@Overridepublic Converter<ResponseBody, ?> fromResponseBody(Type type, Annotation[] annotations) { if (!HALResponse.class.isAssignableFrom(type)) { return null; } // TODO create converter with `type` now that you know what it is...}您可以查看回购中的Wire转换器,这是一个完整的示例。



