Get method in which PsiMethodCallExpression is used
Answered
Currently I'm writing a code inspection in which I need to identify certain methods usages. For this purpose I'm using visitCallExpression(expression: PsiCallExpression)
and it works fine. However I need to understand in which method PsiCallExpression
was called in order to skip inspection in certain cases. For example I want my inspection to be skipped in test classes and some specific methods. I already tried using getUseScope
, but it's completely unclear how to proceed with the result and get fully qualified method name from it.
Please sign in to leave a comment.
Hi,
Could you please clarify your case? What does it mean in which method is called? A call in a method body or in a method parameter? How did you try to use getUseScope()?
If you want to skip the inspection in tests, check if a current file is under test source root:
Similarly, check a method body or parent method call and see if it's what you want to ignore.
If it doesn't answer your question, please clarify the case and how you are trying to use getUseScope().
Sure. By "In which method is called" I mostly mean next scenario
In this example I need to know that bar method was called inside the foo method.
Regarding the getUseScope, I mostly just mentioned that I'm aware of such api, however I don't know how to get any information from the result. It returns SearchScope object and there is not that much possible methods to call from it in order to get actual scope
Please try the approaches I mentioned. Regarding checking a method containing an inspected call, use PSI API to get the containing method. To understand PSI, see PsiViewer: https://plugins.jetbrains.com/docs/intellij/explore-api.html#31-use-internal-mode-and-psiviewer
I saw the plugin and also checked documentation related to PSI, however wasn't able to get scope in which method was called from PsiCallExpression. Regarding the line of code suggested in the initial answer, I'm not sure what are those variables you are using (virtualFile, project). All I have is PsiCallExpression expression which I get in visitCallExpression visitor method. If it is possible to get PsiMethod in which expression was originally called then it would be great to take a look at a code example
hej ru(?),
if I understand you correctly you are implementing
visitCallExpression(expression: PsiCallExpression)
in a JavaElementVisitor and you want to check whether the call represented by that expression is within a method with a certain name (within foo in your example).The method getUseScope is for something else. As Karol already suggested, you can use the PSIViewer plugin to view the PSI structure of code to find out what to search for:
You can then use e.g. methods from PsiTreeUtil (getParentOfType(...)) to find the tree node for the enclosing method (PsiMethod:foo in the screenshot).
Regarding the variables: virtualFile represents the file containing the code, project is the corresponding project. You can get to both via methods of your PsiElement (.getProject() and .getContainingFile().getVirtualFile()).
Alright, so it seems like the only way to get it is to traverse trough the parents and find first PsiMethod instance which should be the required method. Okay, thanks, I think this helped