Unchecked method invokation in generic method with ParameterizedTypeReference

Viewed 21

I have the next generic method for make a call to an external service.

public <T, R, Z extends Class<R>, C extends Collection<R>> Optional<List<R>>
    makeCollectionRequest(
        @NotNull @NotNull String endpoint,
        @NotNull @NotEmpty HttpMethod verb,
        T body,
        @NotNull Class<C> collectionType,
        @NotNull Z responseClass
    ) {
        ResponseEntity<List<R>> res = null;
        HttpEntity<?> entity = new HttpEntity<>(body, headers);

        try {
            res = this.restClientAzure.exchange(
                endpoint,
                verb,
                entity,
                ParameterizedTypeReference.forType(
                    TypeUtils.parameterize(collectionType, responseClass)
                )
            );
        } catch (Exception ex) {
            logger.logError("Error on Azure request", ex);
        }

        if (res != null)
            return this.azureRequestBodyParser(res, endpoint);
        else
            return Optional.empty();
    }

So the main idea is to have a generic to retrieve data from Azure, that will come in some kind of form of a collection, and I want to produce an API to the user that allows him to specify the type of the collection, and the type of data of the collection.

So, the method call looks like:

Optional<List<AzureResponse>> res = azureClient
                        .makeCollectionRequest(
                            URL,
                            HttpMethod.GET,
                            null,
                            List.class,
                            AzureResponse.class
                        );

And I want to get rid out of the warning that it's shown on the method call:

Unchecked method 'makeCollectionRequest(String, HttpMethod, T, Class<C>, Z)' invocation

How can I change my code to achieve it?

Thanks.

0 Answers
Related