Tag your Java source code with Tiger’s annotation

Tiger中的Annotation又被称作Metadata(关于数据的数据),就是关于源代码的数据,具体来说就是关于Java的类、方法、字段等关联的附加数据。这些数据可以被Java的编译器或者其它工具(比如Java IDE)来发现和使用。

BTW:其实从数据的数据的角度来说,JavaDoc算是最早的Metadata了。 :-)

JUnit4.0中,就改用Annotation来创建单元测试,而不再用"test"开头的函数了。这样更自然,而且更符合标准。

看看J2SE 5.0 in a Nutshell中给出的例子:

  1. import java.lang.annotation.*;
  2. import java.lang.reflect.*;
  3.                                                                                
  4. @Retention(java.lang.annotation.RetentionPolicy.RUNTIME) @interface debug  {
  5.     boolean  devbuild() default false;
  6.     int counter();
  7. }
  8.                                                                                
  9. public class MetaTest {
  10.     final boolean production=true;
  11.  
  12.     @debug(devbuild=production,counter=1) public void testMethod()  {
  13.     }
  14.  
  15.                                                                                
  16.     public static void main(String[] args) {
  17.                                                                                
  18.         MetaTest mt = new MetaTest();
  19.         try {
  20.             Annotation[] a = mt.getClass().getMethod("testMethod").getAnnotations();
  21.             for (int i=0; i<a.length ; i++)  { 
  22.                  System.out.println("a["+i+"]="+a[i]+" ");
  23.              } 
  24.          } catch(NoSuchMethodException e) { 
  25.              System.out.println(e);
  26.          } 
  27.      } 
  28.  }

这个例子很简单,就是在运行时打印出某个函数的所有的Annotation。

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)表明这个Annotation作用于RUNTIME级别,相应的还有:

  1. SOURCE:源代码级别,编译后将消失;
  2. CLASS:类文件级别,运行时无法访问;
  3. RUNTIME:运行时级别,运行时依然存在,通过Reflection进行访问。

因此,JavaDoc应该算是Source级别的Annotation,而JUnit 4.0中应该是RUNTIME级别的Annotation。

J2SE5.0中系统定义了三个Annotation:

@Override

顾名思义,用来标识本函数应该是Override父类的。用来在编译时找到一些由于疏忽而造成的在运行期非常难以排查的Bug。比如:

  1. @Override
  2. public int hasCode() {
  3.     return toString().hashCode();
  4. }

这段代码的本意应该是"hashCode",由于疏忽而写成了"hasCode",这个Bug在运行时非常难找到,而加了Annotation,可以在编译的时候,就能把这个臭虫揪出来,当然,还需要标识所有Override这个良好习惯的养成。

@Deprecated

标识Deprecated函数,编译时给出警告。具体见

@SuppressWarnings

这是用于抑制在J2SE 5.0中的,如果Collection等不使用泛型语法而给出的编译警告信息。具体见

可见,给源代码打上Tag不但能够进行编译期检查,还能进行代码分析以及其它一些应用(单元测试)。

除去这些系统定义的Annotation,我们还可以定义自己的Annotation来帮助自己的工作。

Tiger刚出来的时候,我曾经想自己定义一个Annotation - "DesignPattern",用来标识我的代码中用到的各种Design Pattern,然后再编写一个工具来制作Tag Cosmos。看看我到底用了多少DP,哪些用的最多,呵呵(显然,这是个SOURCE级别的Annotation)。

  1. /**
  2.  * Annotation type to indicate a design pattern used
  3.  */
  4. @Retention(RetentionPolicy.SOURCE)
  5. public @interface DesignPattern {
  6.     String value();
  7. }

与社会性软件(SocailSoftware)中的Tag一样,Java中的Annotation也是将人们对数据的认识以“数据的数据”的方式存储到数据中,用于帮助我们更好的认识事物。不同的是,社会性软件中的Tag更加偏重于让人们根据Tag来进行知识共享,而代码中的Tag更加偏重于利用这些Tag来帮助程序员进行开发或者方便第三方程序进行扩展。

Reference:

JSR 175: A Metadata Facility for the JavaTM Programming Language

J2SE 5.0 in a Nutshell - Calvin Austin

Annotations in Tiger, Part 1: Add metadata to Java code - Brett McLaughlin

Annotations in Tiger, Part 2: Custom annotations - Brett McLaughlin

注释(Annotation)与 ASM - Dennis Sosnoski

xDoclet

Popularity: 30%

Related entries:

  • No Related Posts

One Response to “Tag your Java source code with Tiger’s annotation”

  1. Meng Yan ( 孟岩 ) @ Weblog » Blog Archive » “JUnit4.0 in 10 minutes” learning minutes Says:

    […] 测试函数名称不用再以"test"开头,而用Annotation"@Test"来修饰; […]

Leave a comment

(required)

(required)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.


RSS feed for comments on this post | TrackBack URI

 

Creative Commons License
This work is licensed under a Creative Commons License.

一天就好象是这短暂的一生,一生它只是无尽的路上短暂的一天