Java自定义Annotation学习

本次学习的目标是为了获取如下Java类成员中ID的值: Java代码 package com.perficient.annotation; 
  1.  
  2. public class WebPage { 
  3.      
  4.     @Identifier(id= "A"
  5.     public String buttonA; 
  6.      
  7.     @Identifier(id= "B"
  8.     public String buttonB; 
  9.  
package com.perficient.annotation;public class WebPage {        @Identifier(id= "A")    public String buttonA;        @Identifier(id= "B")    public String buttonB;}[/pre]
  创建自定义的Annotation: Java代码  package com.perficient.annotation; 
  1.  
  2. import java.lang.annotation.ElementType; 
  3. import java.lang.annotation.Retention; 
  4. import java.lang.annotation.RetentionPolicy; 
  5. import java.lang.annotation.Target; 
  6.  
  7.  
  8. @Target(ElementType.FIELD)   
  9. @Retention(RetentionPolicy.RUNTIME)     
  10. public @interface Identifier { 
  11.      
  12.     public String id(); 
  13.      
package com.perficient.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME)    public @interface Identifier {        public String id();    }[/pre]
  
   注意:Target一定要给予正确的值,如果是类成员的话用Field,是方法要用Method 
  编写测试类 Java代码  package com.perficient.annotation; 
  1.  
  2. import java.lang.reflect.Field; 
  3.  
  4. public class AnnotationTest { 
  5.     public static void main(String[] args) throws ClassNotFoundException { 
  6.         String className = "com.perficient.annotation.WebPage"
  7.         Class<?> test = Class.forName(className); 
  8.         Field[] fields = test.getFields(); 
  9.         for (Field field : fields){ 
  10.             System.out.println("The field Name is:" + field); 
  11.             boolean flag = field.isAnnotationPresent(Identifier.class); 
  12.             if (flag) { 
  13.                 Identifier idt = (Identifier) field 
  14.                         .getAnnotation(Identifier.class); 
  15.                 System.out.println("Id is:" + idt.id()); 
  16.             } else { 
  17.                 System.out.println("The annotation can't be found"); 
  18.             }                         
  19.         }         
  20.     } 
package com.perficient.annotation;import java.lang.reflect.Field;public class AnnotationTest {    public static void main(String[] args) throws ClassNotFoundException {        String className = "com.perficient.annotation.WebPage";        Class<?> test = Class.forName(className);        Field[] fields = test.getFields();        for (Field field : fields){            System.out.println("The field Name is:" + field);            boolean flag = field.isAnnotationPresent(Identifier.class);            if (flag) {                Identifier idt = (Identifier) field                        .getAnnotation(Identifier.class);                System.out.println("Id is:" + idt.id());            } else {                System.out.println("The annotation can't be found");            }                                }            }}[/pre]
  
   注意:如果是Method,需要使用getDeclaredMethods(),如果用getMethods()不会返回我们所需要的结果 
  输出结果: Java代码The field Name is: public java.lang.String com.perficient.annotation.WebPage.buttonA 
  1. Id is:A 
  2. The field Name is:public java.lang.String com.perficient.annotation.WebPage.buttonB 
  3. Id is:B 

你可能感兴趣的:(java,String,测试,Class,interface)