1

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);
    }
Artanis
  • 561
  • 1
  • 7
  • 26
  • Is this the only code? Particularly, is there any code between the `postForObject()` call and the test for null? If so, please show it. – Bohemian Dec 27 '18 at 20:03
  • No it's immediately after, as shown. I think there's some issue with sonar not recognizing that postForObject is capable of returning null but I don't know how to fix it. – Artanis Dec 27 '18 at 20:20
  • can you post "postForObject"? – Roy Shahaf Dec 27 '18 at 20:30
  • Probably `postForObject(...)` never returns a null therefore `info == null` is always false – Japheth Ongeri - inkalimeva Dec 27 '18 at 20:39
  • @JaphethOngeri-inkalimeva i've confirmed that it can return null. – Artanis Dec 27 '18 at 20:40
  • @RoyShahaf I updated the question with the postForObject method – Artanis Dec 27 '18 at 20:40
  • Perhaps you'll let us judge that it really never returns null by posting all the relevant code... – Roy Shahaf Dec 27 '18 at 20:52
  • 1
    @RoyShahaf I said it CAN return null if the db doesn't contain what's in the request payload, which means the condition does NOT always return false. I sent a request to simulate this and it is indeed the case. This is the relevant code. – Artanis Dec 27 '18 at 21:08

1 Answers1

2

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).

NoDataFound
  • 11,381
  • 33
  • 59