Method: getAnnotation(Class < MyAnno > annotationClass) : Method « java.lang.reflect « Java by API





Method: getAnnotation(Class < MyAnno > annotationClass)

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

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno {
  String str();

  int val();
}

public class MainClass {

  @MyAnno(str = "Annotation Example", val = 100)
  public static void myMeth() {
    MainClass ob = new MainClass();

    try {
      Class c = ob.getClass();

      Method m = c.getMethod("myMeth");

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

           
         
    
  










Related examples in the same category

1.Method: getExceptionTypes()
2.Method: getModifiers()
3.Method: getName()
4.Method: getParameterTypes()
5.Method: getReturnType()
6.Method: invoke(Object obj, Object... args)
7.Method: setAccessible(boolean flag)
8.Method: toGenericString()
9.Method: toString()