Java自定义注解

注解类:

 1 package com.www.yanwu.Annotation;
 2 
 3 import java.lang.annotation.Documented;
 4 import java.lang.annotation.ElementType;
 5 import java.lang.annotation.Inherited;
 6 import java.lang.annotation.Retention;
 7 import java.lang.annotation.RetentionPolicy;
 8 import java.lang.annotation.Target;
 9 
10 
11 /** 
12 * @ClassName: Demo2 
13 * @Description: 自定义注解
14 * @author Harvey
15 * @date 2017年7月27日 下午9:42:21 
16 *  
17 */
18 @Inherited  
19 @Documented 
20 @Target(value={ElementType.TYPE,ElementType.METHOD})
21 @Retention(RetentionPolicy.RUNTIME)
22 public @interface Demo2 {
23     String studentName() default "";
24     int age() default 0;
25     int id() default -1;
26 }
View Code

@Target注解,作用范围

@Retention注解可以在定义注解时为编译程序提供注解的保留策略(不填默认CLASS级别)。

CLASS
编译器将把注释记录在类文件中,但在运行时 VM 不需要保留注释。
RUNTIME
编译器将把注释记录在类文件中,在运行时 VM 将保留注释,因此可以反射性地读取。
SOURCE
编译器要丢弃的注释。

测试注解类:

 1 package com.www.yanwu.Annotation;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Date;
 5 import java.util.List;
 6 /** 
 7 * @ClassName: Demo1
 8 * @Description: 测试
 9 * @author Harvey
10 * @date 2017年7月27日 下午9:42:21 
11 *  
12 */
13 @Demo2(age=100,id=20,studentName="hello")
14 public class Demo1 {
15 
16     @Override
17     public String toString() {
18         // TODO Auto-generated method stub
19         return super.toString();
20     }
21     
22     @Demo2(age=10,id=2,studentName="harvey")
23     public void test001(){
24         System.out.println("hello");
25     }
26     @SuppressWarnings("all")
27     public static void main(String[] args) {
28         Date d=new Date();
29         //d.parse("2017");
30         //test001();
31         System.out.println(d);
32         List list=new ArrayList();
33     }
34     
35 
36 }
View Code

解析注解:

 1 package com.www.yanwu.Annotation;
 2 
 3 
 4 import java.lang.annotation.Annotation;
 5 import java.lang.reflect.Method;
 6 
 7 /** 
 8 * @ClassName: Demo3 
 9 * @Demo3: TODO(这里用一句话描述这个类的作用) 
10 * @author Harvey
11 * @date 2017年7月27日 下午9:42:58 
12 *  
13 */
14 public class Demo3 {
15     public static void test() throws ClassNotFoundException {
16         
17     }
18     
19     public static void main(String[] args) throws ClassNotFoundException {
20          /* 
21          * 1.使用类加载器加载类 
22          * Class.forName("类名字符串") (注意:类名字符串必须是全称,包名+类名) 
23          */  
24         Class c = Class.forName("com.www.yanwu.Annotation.Demo1");  
25           
26         //2.判断类上是否存在注解,并获取类上面注解的实例  
27         if(c.isAnnotationPresent(Demo2.class)){  
28             Demo2 d = (Demo2) c.getAnnotation(Demo2.class);  
29             System.out.println(d.studentName());  
30             System.out.println(d.age());  
31             System.out.println(d.id());  
32         }  
33           
34         //3.判断方法上是否存在注解,并获取方法上面注解的实例  
35         System.out.println("===========================");
36         Method[] ms = c.getMethods();  
37         for (Method method : ms) {  
38             if(method.isAnnotationPresent(Demo2.class)){  
39                 Demo2 d = (Demo2)method.getAnnotation(Demo2.class);  
40                 System.out.println(method.getName());
41                 System.out.println(d.studentName());  
42                 System.out.println(d.age());  
43                 System.out.println(d.id());    
44             }  
45         }  
46         //另一种获取方法上的注解的解析方法  
47         System.out.println("===========================");
48         for (Method method : ms) {  
49             Annotation[] as = method.getAnnotations();  
50             for (Annotation annotation : as) {  
51                 if(annotation instanceof Demo2){  
52                     System.out.println(((Demo2) annotation).id());  
53                     System.out.println(((Demo2) annotation).studentName());  
54                     System.out.println(((Demo2) annotation).age());  
55                 }  
56             }  
57         }  
58         
59         System.out.println("hello");
60     }
61         
62     
63 }
View Code
原文地址:https://www.cnblogs.com/harvey2017/p/7247989.html