Java Annotation get all annotations

Introduction

To get all annotations with RUNTIME retention, call getAnnotations() on that item.

It has this general form:

Annotation[ ] getAnnotations() 

It returns an array of the annotations.

getAnnotations() can be called on objects of type Class, Method, Constructor, and Field.

The following code shows how to obtain all annotations associated with a class and with a method.

It declares two annotations. It then uses those annotations to annotate a class and a method.

// Show all annotations for a class and a method. 
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno {
  String str();/*from ww  w  .ja v a2 s .  c  o m*/

  int val();
}

@Retention(RetentionPolicy.RUNTIME)
@interface What {
  String description();
}

@What(description = "An annotation test class")
@MyAnno(str = "Meta2", val = 99)
public class Main {

  @What(description = "An annotation test method")
  @MyAnno(str = "Testing", val = 100)
  public static void myMethod() {
    Main ob = new Main();

    try {
      Annotation annos[] = ob.getClass().getAnnotations();
      // Display all annotations for Meta2.
      System.out.println("All annotations for Meta2:");
      for (Annotation a : annos)
        System.out.println(a);

      System.out.println();

      // Display all annotations for myMethod.
      Method m = ob.getClass().getMethod("myMethod");
      annos = m.getAnnotations();

      System.out.println("All annotations for myMethod:");
      for (Annotation a : annos)
        System.out.println(a);

    } catch (NoSuchMethodException exc) {
      System.out.println("Method Not Found.");
    }
  }

  public static void main(String args[]) {
    myMethod();
  }
}



PreviousNext

Related