How to check classes have @CompileStatic annotation?

424 views Asked by At

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.

2

There are 2 answers

0
Bas Leijdekkers On BEST ANSWER

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

0
Leonard Brünings On

For completeness sake, Codenarc has a CompileStatic Rule since 1.4

CompileStatic Rule

Since CodeNarc 1.4

Enforces classes are annotated either with one of the @CompileStatic, >@GrailsCompileStatic or @CompileDynamic annotations.