Find usages from source class in library
Hey guys,
I have a use case where I want to find usages of classes, methods, etc from a source java class that will be located in a library class. Ive seen other topics such as https://intellij-support.jetbrains.com/hc/en-us/community/posts/206946285-Find-Usages-in-libraries?input_string=Find%20usages%20from%20source%20class%20in%20library which sounds like the same question but were a bit dated so Ill make a new thread.
To easily repo this, I created a simple multi-module maven project with two module, module A and module B. module B depends on module A
Module A has a simple java class (Class A)
Module B has a simple java class (Class B) that with a private member variable of type Class A
Doing a usage search for Class A will result in a usage in Class B.
Now, run mvn install to generate the maven jars. Go into main pom.xml and comment out module B from the list of modules. This will mimic pulling module B as a library since it will pull in the generate jar that was generated earlier.
If you try to search for usages in Project + Libraries you will see there are no results.
I assume this is by design as this is not the typical dependency graph but we have a use for finding those usages (even if its less performant).
I am hoping to figure out what it would take to get that to work? I am willing to add code into a plugin to allow this but I have not dug too much into IntelliJ indexing implementation so some documentation or tips on how to start would help.
Thanks in advance!
请先登录再写评论。
Works for me: calling "Find usages" for the class in jar library file finds it's usage in project:
Make sure to have 'Project and Libraries' scope selected in Find Usages settings dialog.
If problem remains, provide a sample project and the exact steps to reproduce.
Hey Andrey,
Thanks for the image! It looks like you were removed module A from the list of modules instead of module B but everything else looks spot on. (see image for my repo)
Thanks for your help on this!
Find usages action works only for the sources inside modules or in files from library jars. It does not work in the files that are not part of the sources (you can see it has different icon).
Andrey,
Thanks for the info, I have updated the project to contain a new module, Module C, module C depends on Module B such that the project pulls in module B as a library (I forgot once I simplified the project that once I remove module B, nothing was referencing it). The attached image now shows that I am viewing Class B as a library source, while the search does not detect the usage. It sounds like based off your comment, this is not expected and maybe a bug.
Which usages are not found? ClassB? But I do not see any of it's usages based on the screenshot.
Please provide a sample project and steps to reproduce: which class you are serachinng usages for and where. Thank you.
Andrey, I have placed the repo here: https://github.com/brian-mcnamara/intellijsearchtest - see read me for instructions
I was still doing a search for ClassA which is referenced in the source jar of moduleB
Thank you for the sample. The ClassA from module A is not used anywhere in project from direct dependencies (see the module's Dependencies tab). IDE does not search the dependencies for the libraries themselves, it uses the dependencies of the modules in project. There are related requests in https://youtrack.jetbrains.com/issue/IDEA-101317 linked.
Thanks Audrey for taking a look. I realize the usecase is out of the scope of IntelliJ. I was hoping to learn more about what it would take for a plugin to index these libraries that are out of scope but still included in the project. My goal is to provide this functionality for our usecase using a plugin but like I mentioned, I have not looked much into the IntelliJ indexing functionality but would love some references to get started on implementing this.
Thanks again for all your help, I really appreciate your help on this.
Just to close the loop... I had to very hackilly swap code out. I pretty much had to override CoreResolveManagerImpl to substitute ModuleWithDependentScope with my own implementation that allowed me to include library references. I know its a pretty ugly hack but there is not much extensions we can rely off of for this kind of feature.