23

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  );

    }
  }
1
  • you might want to edit the code to remove the unused 'annotations' local variable or use: for (Annotation annotation : annotations) {... Commented Apr 8, 2009 at 6:13

3 Answers 3

43

You need to specify the annotation as being a Runtime annotation using the @Retention annotation on the annotation interface.

i.e.

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{}
Sign up to request clarification or add additional context in comments.

Comments

26

Short answer: you need to add @Retention(RetentionPolicy.RUNTIME) to your annotation definition.

Explanation:

Annotations are by default not kept by the compiler. They simply don't exist at runtime. This may sound silly at first, but there are lots of annotations that are only used by the compiler (@Override) or various source code analyzers (@Documentation, etc).

If you want to actually USE the annotation via reflection like in your example, you'll need to let Java know that you want it to make a note of that annotation in the class file itself. That note looks like this:

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation{}

For more information, check out the official docs1 and especially note the bit about RetentionPolicy.

Comments

3

Use @Retention(RetentionPolicy.RUNTIME) Check the below code. It is working for me:

import java.lang.reflect.*;
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1{}

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{}

public class FooTest {
    @MyAnnotation1
    public void doFoo() {
    }

    @MyAnnotation2
    public void doFooo() {
    }

    public static void main(String[] args) throws Exception {
        Method method = FooTest.class.getMethod( "doFoo" );
        for( Annotation annotation : method.getAnnotations() )
            System.out.println( "Annotation: " + annotation  );

        method = FooTest.class.getMethod( "doFooo" );
        for( Annotation annotation : method.getAnnotations() )
            System.out.println( "Annotation: " + annotation  );
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.