Search code examples
javaintellij-ideaannotationsstatic-code-analysis

Intellij/ Java - identify calls to annotated methods


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.

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.

For example

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

Solution

  • 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).

    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.