Incorrect detection of @Deprecated methods

Please provide

  • Operating system: Amazon Linux 2 - aarch64
  • SonarLint plugin version: v3.20.2
  • Programming language you’re coding in: Java
  • Is connected mode used: No

And a thorough description of the problem / question:

I am using Apache Http Components’ HttpClient 5.2.x in my code. Specifically this method. There are a few variants of this method which are @Deprecated but not this one. Yet, SonarLint reports it as deprecated in VSCode. If I use the “go to definition” feature of VSCode, it takes me to the correct function definition (the one that I am using, that is not deprecated). So, the project seems to be set up correctly.

Rule Id - java:S1874

Sample code -
MyRequest.java

package com.mypackage;

import com.mypackage.BaseResponse;
import java.net.URI;
import lombok.AllArgsConstructor;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.io.entity.EntityUtils;

@AllArgsConstructor
public class MyRequest extends BaseRequest {

    private CloseableHttpClient httpClient;
    private URI requestURI;

    public BaseResonse executeRequest() throws Exception {
        HttpGet request = new HttpGet(requestURI);
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setCookieStore(new BasicCookieStore());
        return httpClient.execute(
            request,
            localContext,
            (ClassicHttpResponse _response) ->
                new BaseResponse(_response.getCode(), EntityUtils.toString(_response.getEntity()))
        );
    }
}

BaseResponse.java

package com.mypackage;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class BaseResponse {

    private int httpStatusCode;
    private String responseString;
}

Error message -

Remove this use of “execute”; it is deprecated.sonarlint(java:S1874)

Hi,

According to the JavaDocs, several variants of execute are deprecated. Is the one you’re using not one of them?

 
Ann

Yes, the variant I am using isn’t deprecated. Please refer the code above and the link I attached to the Javadoc of the method I am using. It isn’t deprecated.

1 Like

Hi,

Sorry, I overlooked your link. I’ve flagged this for the language experts.

 
Ann

1 Like

Hello @DashwoodIce9 ,

Unfortunately, I have not been able to reproduce the issue locally. On my machine,the resolution of the methods is done correctly, and so I don’t see the deprecated warnings.

What’s the content of the BaseRequest class, because it is not defined in your thread?