Show all annotations for a class and a method. : Annotation « Reflection « Java

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;

@interface MyAnno {
  String str();

  int val();

@interface What {
  String description();

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

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

    try {
      Annotation annos[] = ob.getClass().getAnnotations();

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

      Method m = ob.getClass().getMethod("myMeth");
      annos = m.getAnnotations();

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

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

  public static void main(String args[]) {


Related examples in the same category

1.Uses reflection to display the annotation associated with a method.
2.Get annotation by annotation class
3.default values in an annotation.
4.Does a method have an annotation
5.Get Annotation Parameter
6.A better concise toString method for annotation types
7.Find Annotated Method
8.Find Annotated Fields
9.Get default annotation value
10.Get annotation value
11.Is Field Annotation Present
12.Get Annotated Declared Fields