views:

28

answers:

1

According to Guillaume Laforge, it is possible in Groovy 1.6.1 (and I would presume later versions) to define annotations directly in Groovy. However, I cannot make sense of the simple example below. I try to run this in the 1.7.1 version of the Groovy Console:

@Bar
@Foo
class A { }

@interface Bar { }
@interface Foo { }

for(ann in new A().getClass().getAnnotations())
{
    println ann
}

When running this example, the console prints

@org.codehaus.groovy.classgen.GroovyCompilerVersion(value=1.7.1)

and nothing else. What am I doing wrong here?

Related question - here the conclusion is that you cannot write annotations in Groovy, but I was under the impression that this was no longer true.

A: 

So, I figured it out. It had to do with the retention policy, which I foolishly forgot (spending most of my days in C#).

Adding a policy will fix this, e.g.:

@Retention(RetentionPolicy.RUNTIME)
@interface Bar { } 
Eyvind