Does a method have an annotation : Annotation « Reflection « Java






Does a method have an annotation

   

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

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

class Marker {
  @MyMarker
  public static void myMeth() {
    Marker ob = new Marker();

    try {
      Method m = ob.getClass().getMethod("myMeth");

      if (m.isAnnotationPresent(MyMarker.class))
        System.out.println("MyMarker is present.");

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

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

   
    
  








Related examples in the same category

1.Uses reflection to display the annotation associated with a method.
2.Get annotation by annotation class
3.Show all annotations for a class and a method.
4.default values in 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