@CompileStatic and @TypeChecked
By default Groovy resolves a call when it runs, so a wrong argument type is a run-time error. @TypeChecked checks the types at compile time and still compiles dynamic dispatch where it can, and @CompileStatic compiles the class the way javac would, with direct calls and no meta class.
A statically compiled class
static.groovy
import groovy.transform.CompileStatic
@CompileStatic
class Calculator {
int add(int a, int b) {
a + b
}
int addAll(List<Integer> values) {
int total = 0
for (int value in values) {
total += value
}
total
}
}
def calculator = new Calculator()
println calculator.add(2, 3)
println calculator.addAll([1, 2, 3, 4])
groovy static.groovy
5
10
The methods compile to ordinary bytecode: no meta class lookup for add or for the += of the loop. The price is that a dynamic trick no longer works inside the class; the metaprogramming lessons show what is given up.
The error the compiler reports
A type error that the dynamic compiler accepts is a compile error here. The block below is the same mistake, and the page shows the message it gets:
typeerror.groovy
import groovy.transform.CompileStatic
@CompileStatic
class Age {
int value = 'thirty'
}
println new Age().value
groovy typeerror.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
typeerror.groovy: 5: [Static type checking] - Cannot assign value of type java.lang.String to variable of type int
@ line 5, column 17.
int value = 'thirty'
^
1 error
exit status: 1
The first line names the file, the line and the error, and the caret points at the expression. A build fails on this message, where the same class without the annotation compiles and fails later.
The same mistake without the check
dynamic.groovy
class Age {
int value = 'thirty'
}
def age = new Age()
println age.value.getClass().name
println age.value + 1
groovy dynamic.groovy
Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'thirty' with class 'java.lang.String' to class 'int'
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'thirty' with class 'java.lang.String' to class 'int'
at Age.<init>(dynamic.groovy)
at dynamic.run(dynamic.groovy:5)
exit status: 1
The class compiles, and the failure moved to run time: the setter of the typed property casts the string and throws GroovyCastException, which names the class and the value but not the line of the assignment. With a def property the string would be accepted and the object would not be an age at all, which is worse. That distance between the mistake and the message is the reason a class with a fixed shape is worth annotating.
The middle ground
@TypeChecked checks the calls it can type and leaves the dynamic ones alone, which is the setting for a class that mixes both. Neither annotation changes @CompileStatic's other effect: a method of a statically compiled class is faster, and a call into it from dynamic code still resolves at run time.
Related
- def and static types: the declarations these annotations enforce.
- methodMissing and propertyMissing: the dynamic feature a static class cannot use.
Article author: Arthur Isaev