Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"<method name> is never used" hint, despite being used in generated source #327

Open
kalaracey opened this issue Nov 11, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@kalaracey
Copy link

kalaracey commented Nov 11, 2024

In my project, I have a method of a class that is only invoked by generated source code, and this extension appears to think the method is unused. I have attempted to create a minimum-reproducible example in https://github.com/kalaracey/oracle-vscode-incorrect-unused-hint:

Steps to reproduce:

  • Run gradle build.
  • Open the project in VSCode with the Oracle.oracle-java extension enabled and other Java extensions disabled.
  • Open the file app/src/main/java/org/example/FooController.java.
  • Note the squiggle under the post method and the warning (on hover) "post is never used":
    FooController-warning

This is despite the fact that FooController#post is used in the generated file app/build/generated/sources/annotationProcessor/java/main/org/example/FooController$Route.java:

      var result = controller.post();

Note that right-clicking on that invocation of post in FooController$Route.java and clicking Go To Definition works - it navigates back to FooController.java.

VSCode 1.94.2
Oracle.oracle-java v23.0.0

@kalaracey kalaracey changed the title "<method name> is never unused" hint, despite being used in generated source "<method name> is never used" hint, despite being used in generated source Nov 11, 2024
@kalaracey
Copy link
Author

I also noticed that right-clicking on post in FooController.java and clicking Go To References does not work. Perhaps generated sources are not indexed by this extension by default? If so, is it possible to enable indexing of generated sources?

@kalaracey
Copy link
Author

I thought perhaps the extension relies on all sources being in a Gradle source set, and maybe the generated sources don't belong to any source set.

# build.gradle.kt
tasks.register("sourceSets") {
    doLast {
        sourceSets.forEach {
            println(it)
            it.allSource.forEach {
                println(it)
            }
        }
    }
}
$ gradle sourceSets

> Task :app:sourceSets
source set 'main'
/Users/kal/Code/incorrect-unused-hint/app/src/main/java/org/example/App.java
/Users/kal/Code/incorrect-unused-hint/app/src/main/java/org/example/FooController.java
source set 'test'
/Users/kal/Code/incorrect-unused-hint/app/src/test/java/org/example/AppTest.java

BUILD SUCCESSFUL in 2s
1 actionable task: 1 executed

Note there is no reference to FooController$Route.java.

However, that doesn't seem to be the issue. Even when I add to build.gradle.kts:

// This doesn't seem to have any affect.
sourceSets["main"].java.srcDir(file("build/generated/sources/annotationProcessor/java/main"))

and confirm that the generated source is now in the main source set:

$ gradle sourceSets

> Task :app:sourceSets
source set 'main'
/Users/kal/Code/incorrect-unused-hint/app/src/main/java/org/example/App.java
/Users/kal/Code/incorrect-unused-hint/app/src/main/java/org/example/FooController.java
/Users/kal/Code/incorrect-unused-hint/app/build/generated/sources/annotationProcessor/java/main/org/example/FooController$Route.java
source set 'test'
/Users/kal/Code/incorrect-unused-hint/app/src/test/java/org/example/AppTest.java

BUILD SUCCESSFUL in 966ms
1 actionable task: 1 executed

the "post is never used" hint is still present.

@sid-srini
Copy link
Member

Hi @kalaracey. Thank you for reaching out and bringing this issue to our notice.

Using your reproducer example, I was able to confirm that:

  1. The extension does actually scan the generated sources, without even requiring the 2 subsequent additions you tried for registering the sourceSets.
    • This was observed in the extension Output logs like below:
    INFO [org.netbeans.modules.parsing.impl.indexing.RepositoryUpdater]: Indexing of: .../app/build/generated/sources/annotationProcessor/java/main took: 4 ms
    
  2. The generated source code is analysed properly by the extension when opened in the editor and navigation to the definitions works.
  3. However, in the main app code, the hints analyser and navigation to the references in the generated sources does not work.

Based on this assessment, I am labelling this as a bug that may be targeted in a future release.
Please let us know if you have any other inputs on this. Thank you.

@sid-srini sid-srini added the bug Something isn't working label Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants