You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检查Groovy类是否缺失@CompileStatic注解?(IntelliJ/CodeNarc方案)

Got it, let's break down how to automatically find Groovy classes missing the @CompileStatic annotation—whether you want to use IntelliJ's built-in tools or roll a custom CodeNarc rule. Both approaches work great, depending on your use case:

Using IntelliJ IDEA to Identify Missing @CompileStatic Annotations

IntelliJ has two solid ways to do this without writing any custom code:

Structural Search & Replace (SSR)

This is my favorite for quick, targeted searches because it avoids false positives by matching the exact class structure. Here's how to set it up:

  • Open the Structural Search dialog: Go to Edit > Find > Search Structurally (or use the shortcut Ctrl+Shift+S on Windows/Linux, Cmd+Shift+S on Mac).
  • Create a new Groovy class template: Click the + icon and select Groovy Class from the dropdown.
  • Modify the template to exclude classes with @CompileStatic:
    The default template looks like this:
    class $Class$ {
      $Members$
    }
    
    Click on the class keyword, then in the right-hand panel under Annotations, check the "Negate" box and type CompileStatic into the annotation field. This tells IntelliJ to only find classes that don't have this annotation.
  • Run the search: Hit Find, and IntelliJ will list all matching classes. Bonus: You can switch to the Replace tab to auto-add the annotation (just test this on a backup first to avoid surprises!).

Built-in Inspection Profile

If you want this check to be part of your regular code reviews or IDE warnings, use IntelliJ's built-in inspection:

  • Open the Inspections settings: Go to File > Settings > Editor > Inspections (or IntelliJ IDEA > Settings > Editor > Inspections on Mac).
  • Navigate to Groovy > Performance > Class without @CompileStatic.
  • Enable the inspection, then adjust the severity (I usually set it to "Warning" so it shows up in the editor but doesn't block commits).
  • Run a full project inspection: Right-click your project root > Analyze > Inspect Code, select your inspection profile, and wait for the results. You'll get a list of all classes missing the annotation, grouped by severity.
Creating a Custom CodeNarc Rule for Missing @CompileStatic

If you need to integrate this check into your CI/CD pipeline or share it across your team, CodeNarc is the way to go. Here's how to build a custom rule:

Step 1: Set Up Your CodeNarc Project

First, create a Groovy project with CodeNarc as a dependency. For Gradle, add this to your build.gradle:

dependencies {
    implementation 'org.codenarc:CodeNarc:2.2.0' // Use the latest stable version
    implementation 'org.codehaus.groovy:groovy-all:3.0.17' // Match your project's Groovy version
}

Step 2: Write the Custom Rule

Create a new Groovy class that extends CodeNarc's AbstractAstVisitorRule. This rule will scan the AST (Abstract Syntax Tree) of your Groovy files to check for missing annotations:

import org.codenarc.rule.AbstractAstVisitorRule
import org.codenarc.rule.AbstractAstVisitor
import org.codehaus.groovy.ast.ClassNode

class MissingCompileStaticRule extends AbstractAstVisitorRule {
    String name = 'MissingCompileStatic'
    int priority = 2 // 1 = highest severity, adjust based on your needs
    Class astVisitorClass = MissingCompileStaticAstVisitor
}

class MissingCompileStaticAstVisitor extends AbstractAstVisitor {
    @Override
    void visitClass(ClassNode classNode) {
        // Skip interfaces, enums, annotations, and inner classes if needed
        if (!classNode.isInterface() && !classNode.isEnum() && !classNode.isAnnotation()) {
            boolean hasCompileStatic = classNode.annotations.any { annotation ->
                annotation.classNode.name == 'groovy.transform.CompileStatic'
            }
            if (!hasCompileStatic) {
                addViolation(classNode, "Class ${classNode.nameWithoutPackage} should use @CompileStatic for improved performance and type safety")
            }
        }
        super.visitClass(classNode)
    }
}

Step 3: Configure CodeNarc to Use Your Rule

Create a codenarc.groovy configuration file pointing to your custom rule:

ruleset {
    ruleset('path/to/your/custom/rules') // Replace with the actual path to your rule class
    // Uncomment below to include default CodeNarc rulesets if needed
    // ruleset('rulesets/basic.xml')
}

Step 4: Run CodeNarc

Add the CodeNarc plugin to your build.gradle to run checks via Gradle:

plugins {
    id 'org.codenarc' version '2.2.0'
}

codenarc {
    configFile = file('codenarc.groovy')
    sourceSets = [sourceSets.main]
}

Then run ./gradlew codenarcMain—the report will be generated in build/reports/codenarc/main.html, listing all classes missing the @CompileStatic annotation.

内容的提问来源于stack exchange,提问作者rvazquezglez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:08:25