Java Tutorial/Reflection/Annotation

Материал из Java эксперт
Перейти к: навигация, поиск

Get annotation by Annotation class

   <source lang="java">

import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @Retention(RetentionPolicy.RUNTIME) @interface MySingle {

 int value(); // this variable name must be value

} class Single {

 @MySingle(100)
 public static void myMeth() {
   Single ob = new Single();
   try {
     Method m = ob.getClass().getMethod("myMeth");
     MySingle anno = m.getAnnotation(MySingle.class);
     System.out.println(anno.value()); // displays 100
   } catch (NoSuchMethodException exc) {
     System.out.println("Method Not Found.");
   }
 }
 public static void main(String args[]) {
   myMeth();
 }

}</source>





Get annotation by type

   <source lang="java">

import java.lang.annotation.Annotation; import javax.ejb.Entity; public class GetAnnotation {

   public static void main(String[] args) {
       Annotation[] as = Person.class.getAnnotations();
       for(int i=0;i<as.length;i++){
           System.out.println(as[i].annotationType());        
       }
       Annotation a =Person.class.getAnnotation(Entity.class);
       System.out.println(a);  
       Annotation[] das = Person.class.getDeclaredAnnotations();
       for(int i=0;i<das.length;i++){
           System.out.println(das[i].annotationType());        
       }        
   }

}

///

import javax.ejb.Entity; import javax.ejb.AccessType; import javax.ejb.Id; import javax.ejb.GeneratorType; import java.io.Serializable; @Entity(access = AccessType.FIELD) public class Person implements Serializable {

 @Id(generate = GeneratorType.AUTO)
 Integer id;
 String name;

}</source>





Show all annotations for a class and a method.

   <source lang="java">

import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @Retention(RetentionPolicy.RUNTIME) @interface MyAnno {

 String str();
 int val();

} @Retention(RetentionPolicy.RUNTIME) @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)
       System.out.println(a);
     Method m = ob.getClass().getMethod("myMeth");
     annos = m.getAnnotations();
     System.out.println("All annotations for myMeth:");
     for (Annotation a : annos)
       System.out.println(a);
   } catch (NoSuchMethodException exc) {
     System.out.println("Method Not Found.");
   }
 }
 public static void main(String args[]) {
   myMeth();
 }

}</source>





Use reflection to display the annotation associated with a method.

   <source lang="java">

import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @Retention(RetentionPolicy.RUNTIME) @interface MyAnno {

 String str();
 int val();

} class Meta {

 @MyAnno(str = "Annotation Example", val = 100)
 public static void myMeth() {
   Meta ob = new Meta();
   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();
 }

}</source>