简体   繁体   中英

instantiate using an inherited generic type

I have the following case

public class ResponseType <T> {
  private ParameterizedTypeReference<HttpResponse<T>> type;

  private ResponseType() {
    this.type = new ParameterizedTypeReference<HttpResponse<T>>() {};
  }

  public static <T> ResponseType<T> create() {
    return new ResponseType<T>();
  }
}

When I call ReponseType.<MyClass>create() where how can I construct a ParameterizedTypeReference<HttpResponse<MyClass>> ?

Currently if I call this, the type will be ParameterizedTypeReference<HttpResponse<T>>

What I want to do is to call

restTemplate.exchange(
    ...,
    responseType.getType()
);

where responseType = ResponseType.create() or resonseType = ResponseType.<MyClass>create()

instead of

restTemplate.exchange(
    ...,
    new ParameterizedTypeReference<HttpResponse<MyClass>>() {}
);

You are creating in the constructor that is called by create() witht he type T and storing inside the class as 'type'

If its public:

ParameterizedTypeReference<HttpResponse<MyClass>> obj = ReponseType.<MyClass>create().type;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM