获取使用PsiMethodCallExpression的方法。

huangapple go评论65阅读模式
英文:

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()
    }
}

huangapple
  • 本文由 发表于 2023年5月17日 09:16:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定