Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Tiger系列四:Annotation第二部分:定制Annotation

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

1、自定义Annotation类型

1)定义Annotation类型

l         使用@interface声明Annotation类型

public @interface InProgress {
 
}

l         使用Annotation类型

public class TestAnnotation {
       @InProcess
       public void test() {
         
       }
}

l         如果Annotation类型和使用它的类不在相同的包中,可以import Annotation类型,以便直接使用 @InProgress

2)添加成员

l         Annotation类型可以有成员变量,以提供有用的信息

l         定义数据成员不需要定义gettersetter方法,只需要定义一个以成员名称命名的方法,并指定返回类型为需要的数据类型

l         简单的例子:

public @interface TODO {
       String value();
}

l         使用带成员的Annotation类型:

public class TestAnnotation {
       @InProcess
       @TODO("Need to finish this method later")
       public void test() {
         
       }
}

3)设置缺省值

l         要为Annotation类型的成员设置缺省值,需要在声明成员时使用default关键字:

public @interface GroupTODO {
       public enum Severity {
              CRITICAL, IMPORTANT, TRIVIAL, DOCUMENTATION
       };
 
       Severity severity() default Severity.IMPORTANT;
       String item();
       String assignedTo();
       String dateAssigned();
}

l         当然,缺省值的类型必须与成员变量声明的类型完全相同

l         下面是使用缺省值的例子:

public class TestAnnotation {
       @InProcess
       @GroupTODO(
              item="Need to finish this method later",
              assignedTo="nelson_tu",
              dateAssigned="2005/02/05"
       )
       public void test() {
         
       }
}

l         下面是改写缺省值的例子:

public class TestAnnotation {
       @InProcess
       //@TODO("Need to finish this method later")
       @GroupTODO(
              severity=GroupTODO.Severity.DOCUMENTATION,
              item="Need to finish this method later",
              assignedTo="nelson_tu",
              dateAssigned="2005/02/05"
       )
       public void test() {
         
       }
}

 

2、元Annotation

l         Annotation就是AnnotationAnnotationJDK5提供了4种预定义的元Annotation

1@Target

l         @Target指定Annotation类型可以应用的程序元素,以便在其它程序元素中误用Annotation类型

l         程序元素的类型由java.lang.annotation.ElementType枚举类定义:

package java.lang.annotation;
 
public enum ElementType {
  TYPE,          // Class, interface, or enum (but not annotation)
  FIELD,         // Field (including enumerated values)
  METHOD,        // Method (does not include constructors)
  PARAMETER,             // Method parameter
  CONSTRUCTOR,           // Constructor
  LOCAL_VARIABLE, // Local variable or catch clause
  ANNOTATION_TYPE,       // Annotation Types (meta-annotations)
  PACKAGE        // Java package
}

l         下面是使用@Target的例子:

@Target({ElementType.TYPE,
    ElementType.METHOD,
    ElementType.CONSTRUCTOR,
    ElementType.ANNOTATION_TYPE})
public @interface TODO {
       String value();
}

2@Retention

l         @Retention Java 编译器处理Annotation类型的方式有关

l         这些方式由java.lang.annotation.RetentionPolicy 枚举类定义:

package java.lang.annotation;
 
public enum RetentionPolicy {
  SOURCE,       // Annotation is discarded by the compiler
  CLASS,       // Annotation is stored in the class file, but ignored by the VM
  RUNTIME       // Annotation is stored in the class file and read by the VM
}

l         使用@Retention的例子参看后面的@Documented

3@Documented

l         @Documented指明需要在Javadoc中包含Annotation(缺省是不包含的)

l         下面是一个使用@Documented的例子:

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface InProcess {
 
}

l         使用@Documented的一个技巧就是指定保持性策略为RetentionPolicy.RUNTIME:这样,Annotation就会保留在编译后的类文件中并且由虚拟机加载,然后Javadoc就可以抽取出Annotation,添加到类的HTML文档中

4@Inherited

l         @Inherited最复杂、使用最少、也最容易造成混淆的一个

l         假设使用@InProgress 标记一个正在开发的类,只要正确应用@DocumentedAnnotation信息就会出现在Javadoc中;现在要编写一个新类,扩展那个正在开发的类,那么使用子类,或者查看它的文档,根本没法表明还有什么地方没有完成;而本来是希望@InProgress Annotation信息会被带到子类中,这就需要使用@Inherited

l         下面是这样的例子:

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface InProcess {
 
}

 




相关文章

相关软件