英文:
Get method in which PsiMethodCallExpression is used
问题
目前,我正在编写代码检查,需要识别特定的方法使用情况。为此,我正在使用 visitCallExpression(expression: PsiCallExpression)
,它工作正常。然而,我需要了解在哪个方法中调用了 PsiCallExpression
,以便在某些情况下跳过检查。例如,我希望在测试类和某些特定方法中跳过我的检查。我已经尝试使用 getUseScope
,但完全不清楚如何处理结果并从中获取完全限定的方法名称。
英文:
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.
答案1
得分: 0
Here is the translated content:
最终,在IntelliJ支持中找到了答案:https://intellij-support.jetbrains.com/hc/en-us/community/posts/11522817858834-Get-method-in-which-PsiMethodCallExpression-is-used
总之,没有简单的方法来实现这一点,但可以通过遍历元素树中的元素,直到找到PsiMethod
来完成。
以下是执行此操作的代码片段:
var element = expression.getParent()
while (element != null) {
if (element is PsiMethod) {
val method = element as PsiMethod
// 对PsiMethod实例执行您想要的操作
} else {
element = element.getParent()
}
}
请注意,这是给定的内容的翻译部分。如果您需要任何其他内容的翻译,请提出具体要求。
英文:
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()
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论