Intellij/Java - 识别对带注释方法的调用 [英] Intellij/ Java - identify calls to annotated methods

查看:28
本文介绍了Intellij/Java - 识别对带注释方法的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够在编译时或通过使用静态代码分析识别 Intellij Idea 13 中具有特定注释的方法的调用,例如识别对 @Deprecated 方法的调用.

I need to be able to identify calls to methods with specific annotations in Intellij Idea 13, during compile time or by using static code analysis, like calls to @Deprecated methods are identified.

我研究过在idea中进行结构搜索,静态代码分析支持这些,并且能够从那里识别方法调用,但我找不到一种方法来将这些限制为对带有注释的方法的调用.

I have looked into doing a structural search in idea, these are supported in static code analysis, and am able to identify method calls from there, but I can't find a way to limit these to calls to method with annotations.

例如

public class A {
  @Foo
  public void foo(){
    // do something... 
  }

  public void bar() {
    // do something else.... 
  }

}

public class main {
  public static void main(String... args){
    A a = new A();

    a.foo(); // <---- should be highlighted
    a.bar();
  }
}

推荐答案

您可以在 IDEA 中执行此操作(这将涉及使用 IDEA 的内部接口;我不知道哪些可以访问注释).

You could do this in IDEA (which would involve using IDEA's internal interfaces; I don't know offhand which ones give access to annotations).

根据您的用例,另一种替代方法是使用外部工具,例如 Checker Framework.优点是它是外部支持的,并且有很多现有的功能,所以你自己编写和维护的代码会更少.此外,其他不使用 IDEA 的人也可以运行分析.缺点是与 IDE 的集成不那么紧密;您需要配置 IDEA 来运行分析,这很简单.

Depending on your use case, another alternative would be to use an external tool such as the Checker Framework. The advantage is that it is externally supported and has a lot of existing functionality, so there would be less of your own code to write and maintain. Additionally, other people who don't use IDEA would be able to run the analysis. The disadvantage is that there would be less tight integration with the IDE; you would need to configure IDEA to run the analysis, which is straightforward.

这篇关于Intellij/Java - 识别对带注释方法的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆