• Java注解(Annotation)详解


    转:http://blog.csdn.net/zen99t/article/details/49508447

    (二)自定义注解

    先上一段代码有个大概的印象,再慢慢讲解(其实代码注释已经讲解地很清楚了):

    [java] view plain copy
     
    1. package diyDescription;  
    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. @Target({ElementType.METHOD,ElementType.TYPE})  
    11. @Retention(RetentionPolicy.RUNTIME)  
    12. @Inherited  
    13. @Documented  
    14. public @interface Description { //使用@interface关键字定义注解  
    15.   
    16.     //成员以无参数无异常方式声明  
    17.     String desc();  
    18.     /* String desc(int a); 
    19.      * String desc() throws Exception; 
    20.      * 都是错误的声明方式 
    21.      */  
    22.       
    23.     String author();  
    24.     // String author() default ”“; 合法的声明  
    25.       
    26.     //可以用default为成员指定一个默认值  
    27.     int age() default 18;  
    28.       
    29.     /* 
    30.      * 如果声明:Map map(); 则会报错: 
    31.      * Invalid type Map for the annotation attribute Description.map;  
    32.      * only primitive type, String, Class, annotation, enumeration  
    33.      * are permitted or 1-dimensional arrays thereof 
    34.      *  
    35.      * 只有原始类型和String, Class, annotation, enumeration才可以 
    36.      */  
    37. }  
    package diyDescription; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Description { //使用@interface关键字定义注解 //成员以无参数无异常方式声明 String desc(); /* String desc(int a); * String desc() throws Exception; * 都是错误的声明方式 */ String author(); // String author() default ""; 合法的声明 //可以用default为成员指定一个默认值 int age() default 18; /* * 如果声明:Map map(); 则会报错: * Invalid type Map for the annotation attribute Description.map; * only primitive type, String, Class, annotation, enumeration * are permitted or 1-dimensional arrays thereof * * 只有原始类型和String, Class, annotation, enumeration才可以 */ }

    1.自定义注解的语法要求

    首先看上面代码的下面部分:
    1.使用@interface关键字定义注解,注意关键字的位置
    2.成员以无参数无异常的方式声明,注意区别一般类成员变量的声明
    3.可以使用default为成员指定一个默认值,如上所示
    4.成员类型是受限的,合法的类型包括原始类型以及String、Class、Annotation、Enumeration (JAVA的基本数据类型有8种:byte(字节)、short(短整型)、int(整数型)、long(长整型)、float(单精度浮点数类型)、double(双精度浮点数类型)、char(字符类型)、boolean(布尔类型)
    5.注解类可以没有成员,没有成员的注解称为标识注解,例如JDK注解中的@Override、@Deprecation 
    6.如果注解只有一个成员,并且把成员取名为value(),则在使用时可以忽略成员名和赋值号“=” ,例如JDK注解的@SuppviseWarnings ;如果成员名不为value,则使用时需指明成员名和赋值号”=”,例子代码如下:
    [java] view plain copy
     
    1. package jtzeng;  
    2. import java.lang.annotation.ElementType;  
    3. import java.lang.annotation.Target;  
    4. @Target({ElementType.TYPE})  
    5. public @interface SingleValue1 {  
    6.     String desc();  
    7. }  
    8.   
    9.   
    10. package jtzeng;  
    11. import java.lang.annotation.ElementType;  
    12. import java.lang.annotation.Target;  
    13. @Target({ElementType.METHOD})  
    14. public @interface SingleValue2 {  
    15.     String value();  
    16. }  
    17.   
    18.   
    19. package jtzeng;  
    20. @SingleValue1( desc = “这是TYPE注解” )  //使用时需指明成员名和赋值号”=”  
    21. public class Test {  
    22.     @SingleValue2(“这是METHOD注解”)    //使用时可以忽略成员名和赋值号“=”  
    23.     public void print() {  
    24.         System.out.println();  
    25.     }  
    26. }   
    package jtzeng; import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target({ElementType.TYPE}) public @interface SingleValue1 { String desc(); } package jtzeng; import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target({ElementType.METHOD}) public @interface SingleValue2 { String value(); } package jtzeng; @SingleValue1( desc = "这是TYPE注解" ) //使用时需指明成员名和赋值号"=" public class Test { @SingleValue2("这是METHOD注解") //使用时可以忽略成员名和赋值号“=” public void print() { System.out.println(); } }

    2.元注解

             何为元注解?就是注解的注解,就是给你自己定义的注解添加注解,你自己定义了一个注解,但你想要你的注解有什么样的功能,此时就需要用元注解对你的注解进行说明了。
            元注解有4个,如下代码的上面部分:

    2.1. @Target

    即注解的作用域,用于说明注解的使用范围(即注解可以用在什么地方,比如类的注解,方法注解,成员变量注解等等)
    取值:
              ElemenetType.CONSTRUCTOR—————————-构造器声明 
              ElemenetType.FIELD ————————————–域声明(包括 enum 实例) 
              ElemenetType.LOCAL_VARIABLE————————- 局部变量声明 
              ElemenetType.METHOD ———————————-方法声明 
              ElemenetType.PACKAGE ——————————— 包声明 
              ElemenetType.PARAMETER ——————————参数声明 
              ElemenetType.TYPE————————————— 类,接口(包括注解类型)或enum声明
    使用实例:
            首先定义一个Description注解,
    [java] view plain copy
     
    1. package jtzeng;  
    2. import java.lang.annotation.ElementType;  
    3. import java.lang.annotation.Target;  
    4.   
    5. @Target({ElementType.TYPE,ElementType.FIELD})  
    6. public @interface Description {   
    7.   
    8.     String desc();  
    9.     String author();  
    10.     int age() default 21;  
    11. }   
    package jtzeng; import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target({ElementType.TYPE,ElementType.FIELD}) public @interface Description { String desc(); String author(); int age() default 21; }
            然后再定义一个Test类进行测试,为了能让大家看出错误,此处用图片给出。
            可以发现,因为上面定义注解时候,@Target只包含了ElemenetType.TYPEElemenetType.FIELD,所以在类和声明中注解是可以的,而在方法上注解会报错。
     

    2.2. @Retention

    描述的注解在什么范围内有效。
    取值有:
              RetentionPolicy.SOURCE————————–只在源码显示,编译时会丢失
              RetentionPolicy.CLASS—————————–编译时会记录到class中,运行时忽略 
              RetentionPolicy.RUNTIME————————- 运行时存在,可以通过反射读取
    使用实例:
             下面给出的是简单的定义,至于会有什么不同的效果,往后的解析注解部分会讲解。
    [java] view plain copy
     
    1. package jtzeng;  
    2. import java.lang.annotation.Retention;  
    3. import java.lang.annotation.RetentionPolicy;  
    4.   
    5. @Retention(RetentionPolicy.RUNTIME)     //运行时存在,可以通过反射读取  
    6. //@Retention(RetentionPolicy.SOURCE)    //只在源码显示,编译时会丢失  
    7. //@Retention(RetentionPolicy.CLASS)     //编译时会记录到class中,运行时忽略   
    8. public @interface Description {   
    9.     String desc();  
    10.     String author() default “JTZeng”;  
    11.     int age() default 21;  
    12. }  
    package jtzeng; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) //运行时存在,可以通过反射读取 //@Retention(RetentionPolicy.SOURCE) //只在源码显示,编译时会丢失 //@Retention(RetentionPolicy.CLASS) //编译时会记录到class中,运行时忽略 public @interface Description { String desc(); String author() default "JTZeng"; int age() default 21; }

    2.3. @Inherited

    1. 是一个标记注解,没有成员,表示允许子类继承该注解,也就是说如果一个使用了@Inherited修饰的注解被用于一个class时,则这个注解将被该class的子类继承拥有
    2. 使用了@Inherited修饰的注解只能被子类所继承,并不可以从它所实现的接口继承
    3. 子类继承父类的注解时,并不能从它所重载的方法继承注解
    使用实例:
    [java] view plain copy
     
    1. package jtzeng;  
    2. import java.lang.annotation.Inherited;  
    3.   
    4. @Inherited  
    5. public @interface Description {   
    6.     String desc();  
    7.     String author() default “JTZeng”;  
    8.     int age() default 21;  
    9. }  
    package jtzeng; import java.lang.annotation.Inherited; @Inherited public @interface Description { String desc(); String author() default "JTZeng"; int age() default 21; }

    2.4. @Documented

    @Documented是一个标记注解,没有成员。用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。(有点抽象,看例子吧)
    使用实例:
    [java] view plain copy
     
    1. /* 
    2.  * 测试@Documented的功能 
    3.  */  
    4. package jtzeng;  
    5. import java.lang.annotation.Documented;  
    6. import java.lang.annotation.ElementType;  
    7. import java.lang.annotation.Inherited;  
    8. import java.lang.annotation.Retention;  
    9. import java.lang.annotation.RetentionPolicy;  
    10. import java.lang.annotation.Target;  
    11. @Target({ElementType.METHOD,ElementType.TYPE})  
    12. @Retention(RetentionPolicy.RUNTIME)  
    13. @Inherited  
    14. @Documented  
    15. public @interface Description {   
    16.     String desc();  
    17.     String author() default “JTZeng”;  
    18.     int age() default 21;  
    19. }  
    20.   
    21. /* 
    22.  * 定义一个Test测试类,类和方法都有注解 
    23.  */  
    24. package jtzeng;  
    25. @Description(desc=“这是TYPE注解”,author=“JTZeng”,age=21)  
    26. public class Test {  
    27.     private String field = “自定义注解”;  
    28.     @Description(desc=“这是METHOD注解”,author=“JTZeng”,age=21)  
    29.     public void print() {  
    30.         System.out.println(field);  
    31.     }  
    32. }  
    /* * 测试@Documented的功能 */ package jtzeng; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Description { String desc(); String author() default "JTZeng"; int age() default 21; } /* * 定义一个Test测试类,类和方法都有注解 */ package jtzeng; @Description(desc="这是TYPE注解",author="JTZeng",age=21) public class Test { private String field = "自定义注解"; @Description(desc="这是METHOD注解",author="JTZeng",age=21) public void print() { System.out.println(field); } }
            然后,在Eclipse中,右键项目名称,选择Export,选择Java——>javadoc,下一步,完成。看结果,左边是添加了@Documented的效果,右边是没有添加的效果。
     
     
    自定义注解结束~~
  • 相关阅读:
    最短路回顾
    树链剖分【模板】
    grep 、find 、tree 新发现
    网口划VLAN
    JDK安装
    网口做trunk
    PXE搭建
    C# 生成DLL文件
    在form窗体里面 寻找当前焦点的控件
    在屏幕右下方显示提示信息(winform窗体)(借鉴)
  • 原文地址:https://www.cnblogs.com/zachary7/p/7994556.html
Copyright © 2020-2023  润新知