I have the following object initialization. Its part of a RestfulService class.
ParameterizedTypeReference<RestResponsePage<ScheduledScanDto>> ptr =
new ParameterizedTypeReference<RestResponsePage<ScheduledScanDto>>(){};
It is used several times in the class but with different params. e.g.
ParameterizedTypeReference<RestResponsePage<ScanDto>> ptr =
new ParameterizedTypeReference<RestResponsePage<ScanDto>>(){};
or
ParameterizedTypeReference<RestResponsePage<SomeOtherObject>> ptr =
new ParameterizedTypeReference<RestResponsePage<SomeOtherObject>>(){};
Its a bit wordy... I am trying to create a private method to return the ParameterizedTypeReference object as part of a private method. This will reduce boilerplate code in the class and code duplication.. I'm struggling with the syntax on the private method, in particular the nesting of generics.
Would like to get an instance of ParameterizedTypeReference objects by doing the following
pageTypeReference(AnyClassObjectHere.class)
To give some context. I plan to reduce boilerplate code by calling the private method when making a rest request. So would look like the following..
private List<ScanDto> methodMakeScanRequest{
List<ScanDto> = restTemplate.exchange("/localhost/scans-url",HttpMethod.GET, pageTypeReference(ScanDto.class)).getBody()
}
Update...
My attempt looks like this. This kind of works. However it now just maps to a generic linkedlist. The object type is ignored.
private <T> ParameterizedTypeReference<RestResponsePage<T>> pageTypeReference(Class<T> clazz) {
return new ParameterizedTypeReference<RestResponsePage<T>>(){};
}