如何确定字段的未删除类型?

时间:2021-05-08 04:16:19

I have a generic response wrapper class:

我有一个通用的响应包装类:

public class Response <T> {
    T response;
}

and unrelated classes to be wrapped:

和不相关的类包装:

public class ServiceResponse {
    String someField;
}

When I make a service request, I get a JSON response that looks something like:

当我发出服务请求时,我得到的JSON响应类似于:

{ "code":200, "response":{"someField":"some text"} }

Now, all my service responses have the same outer wrapper, i.e., they all have:

现在,我所有的服务响应都有相同的外包装,即它们都有:

{ "code":200, "timestamp":"....", "response":... }

But the actual format/type of the response field is different for each service request. When I deserialize the response, I need to know the type of the response field so I can create the appropriate instance, if the deserialization was done within Response, I could use:

但是响应字段的实际格式/类型对于每个服务请求是不同的。当我反序列化响应时,我需要知道响应字段的类型,以便我可以创建适当的实例,如果在响应中完成反序列化,我可以使用:

response = new T(jsonParser);

However, I'm doing all of this from within a library that is driven by reflection, so I normally deserialize the whole tree with code like:

但是,我在一个由反射驱动的库中执行所有这些操作,因此我通常使用以下代码反序列化整个树:

wrapper = deserializer.parseObject(Response<ServiceResponse>.class)

but, at this point my parseObject method can't correctly determine the type of T.

但是,此时我的parseObject方法无法正确确定T的类型。

I can use something like:

我可以使用类似的东西:

Response<ServiceResponse> response = new Response<>();
Field field = response.getClass().getDeclaredField("response");
Type type = field.getGenericType();

which then tells me that response is of type T but what I actually need is ServiceResponse

然后告诉我响应是类型T但我实际需要的是ServiceResponse

Per this SO question I tried casting as ParameterizedType but that would actually seem to apply to a field of type Response<ServiceResponse> and not the actual field within (and it fails because type can't be cast as ParameterizedType)

根据这个SO问题,我尝试将其作为ParameterizedType进行转换但实际上似乎适用于Response 类型的字段而不是实际字段(并且因为类型无法转换为ParameterizedType而失败)

Is there any way to determine (at run time) the raw type of response?

有没有办法确定(在运行时)原始类型的响应?

Eventually, I may wind up having to create an annotation providing more details about how to deserialize the field, probably by providing a function to do it, but would prefer a more transparent approach.

最后,我可能最终需要创建一个注释,提供有关如何反序列化字段的更多详细信息,可能通过提供执行此功能的功能,但更喜欢更透明的方法。

Another possibility might be to actually assign a void instance of T to response at initialization time and then I could grab the actual type from that...

另一种可能性是在初始化时实际将T的void实例分配给响应然后我可以从中获取实际类型...

1 个解决方案

#1


-1  

Check out this post: http://mydailyjava.blogspot.com/2013/06/advanced-java-generics-retreiving.html

看看这篇文章:http://mydailyjava.blogspot.com/2013/06/advanced-java-generics-retreiving.html

It's actually exactly what you're looking for.

这正是你正在寻找的。

According to this, you'll just need to extend your Response class and then query the generic type of its super.

根据这个,你只需要扩展你的Response类,然后查询它的超类的泛型类型。

#1


-1  

Check out this post: http://mydailyjava.blogspot.com/2013/06/advanced-java-generics-retreiving.html

看看这篇文章:http://mydailyjava.blogspot.com/2013/06/advanced-java-generics-retreiving.html

It's actually exactly what you're looking for.

这正是你正在寻找的。

According to this, you'll just need to extend your Response class and then query the generic type of its super.

根据这个,你只需要扩展你的Response类,然后查询它的超类的泛型类型。