Groovy and annotations - what is wrong with this example?
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.
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 { }
精彩评论