intellij-ideagroovystatic-analysisstructural-searchcodenarc

How to check classes have @CompileStatic annotation?


I want to perform an automated static check analysis on my Groovy code to identify which clases doesn't have the @CompileStatic annotation.

I'd like to know how that can be done either with IntelliJ or CodeNarc custom rule.


Solution

  • It's possible to use IntelliJ IDEA's Structural Search (Edit | Find | Search Structurally...) and its Structural Search Inspection for this purpose. Use a simple pattern like this:

    class $X$ {}
    

    File type Groovy, Context File. And add a Script Constraint like the following (click on Edit Variables... to add constraints):

    com.intellij.codeInsight.AnnotationUtil.findAnnotation(X.parent, "groovy.transform.CompileStatic") == null
    

    This should find all classes that do not have a @CompileStatic annotation