DevOps / Apache Groovy Interview questions
What is @CompileStatic used for?
@CompileStatic instructs the Groovy compiler to type-check a class or method at compile time and generate optimized, statically-dispatched bytecode, similar to how Java itself compiles, instead of Groovy's normal dynamic method dispatch.
This catches certain type errors at compile time rather than only at runtime, and it generally improves performance since the compiler can resolve method calls directly instead of going through Groovy's dynamic runtime dispatch machinery on every call.
The tradeoff is losing some of Groovy's dynamic features, like freely adding methods at runtime via metaClass, within the annotated scope, since static compilation assumes the code's structure is fixed and known at compile time.
More Related questions...