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.
Eventually it was answered at intelij support: https://intellij-support.jetbrains.com/hc/en-us/community/posts/11522817858834-Get-method-in-which-PsiMethodCallExpression-is-used
All in all there is no trivial way to do this, but it's possible to iterate through the elements in the elements tree till you find PsiMethod
and in general this should be it.
Here is the code snippet which does the trick:
var element = expression.getParent()
while (element != null) {
if (element is PsiMethod) {
val method = element as PsiMethod
// do whatever you want to do with PsiMethod instance
} else {
element = element.getParent()
}
}