简体   繁体   中英

Two identical methods with different parameter types

I have two nearly identical methods in Java. The only difference is that they have different parameter types. They use generics and return the type T of the input parameter. How can I get rid of the duplicate code? Here are my two methods. Ultimately they both call Spring restTemplate.exchange() with the different types. Otherwise the methods are identical.

public <T> T send(Class<T> expectedClass) throws KenectRestClientException {
    if (this.logRequest) log.info("Making request: " + this.toString());

    HttpEntity<Object> entity = new HttpEntity<>(this.body, getTokenHeaders(this.token));
    RestTemplate restTemplate = new RestTemplate();

    String compiledUrl = UrlUtils.replacePathParamsInUrl(this.url, this.pathParams, this.uriEncode) + UrlUtils.compileRequestParamsToUrl(this.requestParams, this.uriEncode);

    Object[] incompatibleStrings = extractIncompatibleStrings(compiledUrl);

    try {
        return restTemplate.exchange(compiledUrl, this.httpMethod, entity, expectedClass, incompatibleStrings).getBody();
    } catch(RestClientResponseException e) {
        log.warning(RestClientResponseException.class.getSimpleName() +
                " url: " + url +
                " status code: " + e.getRawStatusCode() +
                " error body: " + e.getResponseBodyAsString() +
                " stacktrace: " + ExceptionUtils.getStackTrace(e));
        throw new KenectRestClientException("Failed to send request");
    } catch(ResourceAccessException e) {
        log.warning("Resource access exception " +
                " url: " + url +
                " stacktrace: " + ExceptionUtils.getStackTrace(e));
        throw new KenectRestClientException("Failed to send request");
    }
}

public <T> T send(ParameterizedTypeReference<T> parameterizedTypeReference) throws KenectRestClientException {
    if (this.logRequest) log.info("Making request: " + this.toString());

    HttpEntity<Object> entity = new HttpEntity<>(this.body, getTokenHeaders(this.token));
    RestTemplate restTemplate = new RestTemplate();

    String compiledUrl = UrlUtils.replacePathParamsInUrl(this.url, this.pathParams, this.uriEncode) + UrlUtils.compileRequestParamsToUrl(this.requestParams, this.uriEncode);

    Object[] incompatibleStrings = extractIncompatibleStrings(compiledUrl);

    try {
        return restTemplate.exchange(compiledUrl, this.httpMethod, entity, parameterizedTypeReference, incompatibleStrings).getBody();
    } catch(RestClientResponseException e) {
        log.warning(RestClientResponseException.class.getSimpleName() +
                " url: " + url +
                " status code: " + e.getRawStatusCode() +
                " error body: " + e.getResponseBodyAsString() +
                " stacktrace: " + ExceptionUtils.getStackTrace(e));
        throw new KenectRestClientException("Failed to send request");
    } catch(ResourceAccessException e) {
        log.warning("Resource access exception " +
                " url: " + url +
                " stacktrace: " + ExceptionUtils.getStackTrace(e));
        throw new KenectRestClientException("Failed to send request");
    }
}

What about passing a boolean flag, based on which you call exchange() passing either a Class<T> or a ParameterizedTypeReference<T> ?

public <T> T send(T neededType, boolean flag) throws KenectRestClientException {
    if (this.logRequest) log.info("Making request: " + this.toString());

    HttpEntity<Object> entity = new HttpEntity<>(this.body, getTokenHeaders(this.token));
    RestTemplate restTemplate = new RestTemplate();

    String compiledUrl = UrlUtils.replacePathParamsInUrl(this.url, this.pathParams, this.uriEncode) + UrlUtils.compileRequestParamsToUrl(this.requestParams, this.uriEncode);

    Object[] incompatibleStrings = extractIncompatibleStrings(compiledUrl);

    try {
        if (flag) {
            return restTemplate.exchange(compiledUrl, this.httpMethod, entity, neededType.getClass(), incompatibleStrings).getBody();
        } else {
            return restTemplate.exchange(compiledUrl, this.httpMethod, entity, ParameterizedTypeReference.forType(neededType.getClass()), incompatibleStrings).getBody();
        }

    } catch(RestClientResponseException e) {
        log.warning(RestClientResponseException.class.getSimpleName() +
                " url: " + url +
                " status code: " + e.getRawStatusCode() +
                " error body: " + e.getResponseBodyAsString() +
                " stacktrace: " + ExceptionUtils.getStackTrace(e));
        throw new KenectRestClientException("Failed to send request");
    } catch(ResourceAccessException e) {
        log.warning("Resource access exception " +
                " url: " + url +
                " stacktrace: " + ExceptionUtils.getStackTrace(e));
        throw new KenectRestClientException("Failed to send request");
    }
}

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