“Find usages” functionality as an IntelliJ plugin

丶灬走出姿态 提交于 2019-12-01 12:13:15

问题


I'm trying to find a way in IntelliJ IDEA to find all the usages of a few library method calls and classes in a particular project.

The goal is to compile a list of classes which make reference to these specific methods or classes.

How can I go about this, I can see there is a MethodReferencesSearch which looks like it could be helpful, however the search method requires an instance of PsiMethod.

How can I create an instance of PSI method that matches the method in a particular lib class (say I wanted to find all the usages of the concat(...) method in Java's String class

Basically I'm trying to build a plugin that will generate a graph of certain method calls from within a project. For example something that would graph a set of routes by looking for certain method calls in a library. I.e. if Class A calls x(T) with type class B and class B calls x(T) with type of Class C, I would have a graph that looks like A -> B -> C etc. Find usages is great, it just doesnt work well for my needs.


回答1:


You can get the true PsiMethod by JavaPsiFacade.getInstance(...).findClass("java.lang.String", ...allScope(...)).findsMethodByName("concat", false)[0]. This method can then be passed to MethodReferenceSearch.




回答2:


I am presuming that you can't guarantee that you have a usage of concat easily available (for example, at the user's cursor position in an open document).

A hacky way to do it would be to create a small, correct, self-contained java class in a String, like below:

class Nothing { String s = "a".concat("b"); }

Then, there is a way (if I remember correctly) to use IntelliJ to parse the class contained in this String, thereby giving you a PsiReference to the method you want to find usages on (in this case, concat).

Would this approach be useful to you?

If so, I can dig out a code example on how this can be done.



来源:https://stackoverflow.com/questions/19425237/find-usages-functionality-as-an-intellij-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!