redhat-developer / vscode-java

Java Language Support for Visual Studio Code
Eclipse Public License 2.0
2.05k stars 420 forks source link

Static import quick fix offered on unrelated error #3656

Open sandersn opened 1 month ago

sandersn commented 1 month ago

A quick fix offerering to static import a static method is offered even when the method is already static imported. This happens when the method is called with incorrect arguments.  

Environment
Steps To Reproduce
  1. Create a class with a static method, eg DatabaseMock.connect
  2. import static java.fixes.DatabaseMock.connect
  3. Call connect incorrectly.

https://github.com/sandersn/ai-fix-examples/tree/main/java/fixes

I haven't used Java in almost 20 years, I was just experimenting with AI quick fixes in different languages. It's possible that my scrabbled-together project in the linked repo is required for the error to show up.

Expected Result

Only "Create method ..." fix should be offered.

Current Result

In addition, "Add static import for ..." fix is offered.

Additional Information

Clicking on the fix does nothing. The method is already static imported.

rgrunber commented 3 weeks ago

Thanks for the sample project. I'm able to reproduce as well. Interestingly, if I don't open BadCall.java as the first file (open DatabaseMock.java first) and then open BadCall.java afterwards , the incorrect code action is not offered. Do you see the same behaviour ?

Update: I think this aspect of the bug is related to the fact that we now discover static import favourites when a file is opened and the configuration updates, but simply opening a file doesn't update the configuration.

The other aspect of this issue is that we calculate static imports by simulating code completion with the unqualified method (eg. connect) in a file that lacks the imports.