Using @CompileStatic in Groovy for Static Type Checking and Performance Gains
Learn how to add Groovy's @CompileStatic annotation to achieve static type checking and Java‑like performance, with step‑by‑step commands, checks, and rollback guidance.
29 Nov 2025, 19:22 UTC

Desired outcome
Apply Groovy’s @CompileStatic annotation to a class or method so that the Groovy compiler performs static type checking, catches type‑related errors at compile time, and generates bytecode with performance close to plain Java.
Prerequisites
- Groovy 2.0 or later installed (the annotation was introduced in Groovy 2.0).
- JDK 8 or newer.
- A build tool that understands the Groovy plugin – Gradle or Maven – configured in your project.
- Basic familiarity with editing Groovy source files and running the build from a terminal or IDE.
Procedure
- Identify the target code – Choose a class or method that does not rely on dynamic Groovy features such as
methodMissing,propertyMissing, or runtime metaprogramming. - Add the annotation – Import
groovy.transform.CompileStaticand place the annotation on the class or method you want to statically compile.import groovy.transform.CompileStatic @CompileStatic class FibonacciCalculator { long compute(int n) { if (n < 2) return n long a = 0, b = 1 for (int i = 2; i <= n; i++) { long tmp = a + b a = b b = tmp } return b } } - Ensure explicit types – All variables, method parameters, and return types must have explicit types or be inferable by the static checker. If the compiler reports an error, add the missing type or refactor the dynamic call.
- Rebuild the project – Run the appropriate build command from the project root.
- Gradle:
./gradlew clean build - Maven:
mvn clean install
You need read/write access to the project directory; no special privileges are required.
- Gradle:
- Verify the result – After the build finishes:
- Check that the
compileGroovy(Gradle) orgroovy:compile(Maven) task completed without static‑typing errors. - Run the existing test suite (
./gradlew testormvn test) to confirm behavior hasn’t changed for the statically compiled parts. - Optionally, execute a micro‑benchmark (e.g., a tight numeric loop) before and after adding the annotation; a noticeable reduction in execution time indicates successful static compilation.
- Check that the
Expected checks
- Compilation succeeds with no
[Static type checking]errors. - All unit tests pass, showing that the logic remains correct.
- Performance measurement (if performed) shows the annotated code runs as fast as an equivalent Java implementation.
Recovery options (rollback)
If the annotation causes compilation failures because the code uses dynamic features, you can revert the change:
- Remove
@CompileStaticfrom the class or method. - Re‑run the build to confirm it succeeds again.
- Commit the reverted version or keep it in a feature branch while you refactor the dynamic code.
Rolling back only involves editing source files; no runtime state is altered, so there is no risk of corrupting a running application.
Limitations
@CompileStaticdisables dynamic Groovy capabilities:methodMissing,propertyMissing,invokeMethod, and runtime metaprogramming.- Certain idioms like GString interpolation inside closure parameters or implicit return types may need explicit typing or rewriting.
- Third‑party libraries that rely on Groovy’s dynamic behavior at runtime may fail when used from statically compiled code.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.