Artanis
Artanis

Reputation: 631

False-positive Sonar issue

Question: why does sonar give me warning ("Change this issue so that it does not always evaluate to "false."). I've been able to prove that if (info == null) evaluates to true when the requestEntity doesn't contain a payload that's found in the db. So how can I get rid of this false positive? Does it have something to do with the @Nullable, @Checkfornull, or @Nonnull annotations? I know the postForObject method uses the @Nullable annotation.

      Info info = restTemplate.postForObject(connectionString, requestEntity,
         Info.class);

      if (info == null) {
         throw new ApplicationException(Constants.NO_RESULT_ERROR_CODE);
      }

Here is the postForObject method:

    @Override
    @Nullable
    public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType)
            throws RestClientException {

        RequestCallback requestCallback = httpEntityCallback(request, responseType);
        HttpMessageConverterExtractor<T> responseExtractor =
                new HttpMessageConverterExtractor<>(responseType, getMessageConverters());
        return execute(url, HttpMethod.POST, requestCallback, responseExtractor);
    }

Upvotes: 1

Views: 972

Answers (1)

NoDataFound
NoDataFound

Reputation: 11979

You can use // NOSONAR on the reported line (and probably explain why you ignored this issue).

That's not the best solution but it works (at least, when used in Eclipse).

You can also use @SuppressWarnings with the issue type (eg: like squid:02020).

Upvotes: 2

Related Questions