[英]"Find usages" functionality as an IntelliJ plugin
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.我试图在 IntelliJ IDEA 中找到一种方法来查找特定项目中一些库方法调用和类的所有用法。
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.我该如何解决这个问题,我可以看到有一个MethodReferencesSearch
看起来可能会有所帮助,但是搜索方法需要 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如何创建与特定 lib 类中的方法匹配的 PSI 方法实例(假设我想在 Java 的String
类中找到concat(...)
方法的所有用法
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.例如,通过在库中查找某些方法调用来绘制一组路由的东西。 Ie 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.即,如果 A 类调用类型为 B 的 x(T),而类 B 调用类型为 C 的 x(T),我将有一个看起来像 A -> B -> C 等的图。查找用法很棒,它只是不能很好地满足我的需求。
You can get the true PsiMethod by JavaPsiFacade.getInstance(...).findClass("java.lang.String", ...allScope(...)).findsMethodByName("concat", false)[0].您可以通过 JavaPsiFacade.getInstance(...).findClass("java.lang.String", ...allScope(...)).findsMethodByName("concat", false)[0] 获得真正的 PsiMethod。 This method can then be passed to MethodReferenceSearch.然后可以将此方法传递给 MethodReferenceSearch。
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).我假设您不能保证您可以轻松使用concat
(例如,在打开的文档中用户的光标位置)。
A hacky way to do it would be to create a small, correct, self-contained java class in a String, like below:一个hacky的方法是在字符串中创建一个小的、正确的、自包含的java类,如下所示:
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
).然后,有一种方法(如果我没记错的话)使用 IntelliJ 来解析包含在这个 String 中的类,从而为您提供一个PsiReference
到您要查找用法的方法(在本例中为concat
)。
Would this approach be useful to you?这种方法对你有用吗?
If so, I can dig out a code example on how this can be done.如果是这样,我可以挖掘出一个关于如何做到这一点的代码示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.