I’m trying to use Java annotations, but can’t seem to get my code to recognize that one exists. What am I doing wrong?
import java.lang.reflect.*; import java.lang.annotation.*; @interface MyAnnotation{} public class FooTest { @MyAnnotation public void doFoo() { } public static void main(String[] args) throws Exception { Method method = FooTest.class.getMethod( 'doFoo' ); Annotation[] annotations = method.getAnnotations(); for( Annotation annotation : method.getAnnotations() ) System.out.println( 'Annotation: ' + annotation ); } }
You need to specify the annotation as being a Runtime annotation using the @Retention annotation on the annotation interface.
i.e.