Mega Code Archive

 
Categories / Java / Language Basics
 

An annotation type declaration 2

/* Java 2, v5.0 (Tiger) New Features by Herbert Schildt ISBN: 0072258543 Publisher: McGraw-Hill/Osborne, 2004 */ import java.lang.annotation.*;  import java.lang.reflect.*;    @Retention(RetentionPolicy.RUNTIME)   @interface MyAnno {    String str();    int val();  }    public class Meta1 {      // myMeth now has two arguments.    @MyAnno(str = "Two Parameters", val = 19)    public static void myMeth(String str, int i)     {      Meta1 ob = new Meta1();        try {        Class c = ob.getClass();          // Here, the parameter types are specified.        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);    }  }