Get annotation by annotation class : Annotation « Reflection « Java

Get annotation by annotation class

import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

@interface MyAnno {
  String str();

  int val();

class Meta {
  @MyAnno(str = "Two Parameters", val = 19)
  public static void myMeth(String str, int i) {
    Meta ob = new Meta();

    try {
      Class c = ob.getClass();

      Method m = c.getMethod("myMeth", String.class, int.class);

      MyAnno anno = m.getAnnotation(MyAnno.class);

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

  public static void main(String args[]) {
    myMeth("test", 10);


Related examples in the same category

1.Uses reflection to display the annotation associated with a method.
2.Show all annotations for a class and a method.
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