@Documented @Retention(RetentionPolicy.SOURCE) @Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD}) @GroovyASTTransformationClass("org.codehaus.groovy.transform.FinalASTTransformation") public @interface Final
Annotation to add the final modifier to classes, methods, constructors, and fields.
Using `final
` and directly using `@Final
` will have the same result.
However, the intention is almost never to use `@Final
` directly but rather as part
of an annotation collector (meta-annotation).
If you like the behavior of an existing annotation but would really like a version that
also ensured the respective annotated node was final, you can create such an element, e.g.:
@AnnotationCollector @Canonical @Final @interface MyCanonical {} @MyCanonical class Foo {}Here, class
Foo
will be final as well as having all the normal Canonical
enhancements.
Similarly, if you wanted to, you could define:
@AnnotationCollector([Singleton, Final]) @interface MySingleton {}Classes annotated with @MySingleton would be final as well as have all the
Singleton
enhancements.
As another example, you could define:
@AnnotationCollector([NullCheck, Final, AutoFinal]) @interface MyNullCheck {}Methods annotated with @MyNullCheck would be final (from @Final), would have all parameters marked final (from @AutoFinal), and would have all parameters checked against
null
(from @NullCheck).
In general, it would be bad style to have an explicit final
modifier and a @Final
annotation
(or more than one @Final
annotation),
but in that scenario if there is an explicit final
or at least one enabled @Final
,
then the annotated class or member will be final.
Type | Name and Description |
---|---|
boolean |
enabled When disabled, this annotation effectively becomes a no-op. |
When disabled, this annotation effectively becomes a no-op.
Typically only used to override an annotation collector already containing an enabled @Final
annotation.
Care must be taken when disabling final in this way since the annotation collector probably had good reason for enabling final.