服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java编程实战篇:设计自己的annotation

第一部分:了解一下java1.5起默认的三个annotation类型:

一个是@override:只能用在方法之上的,用来告诉别人这一个方法是改写父类的。

一个是@deprecated:建议别人不要使用旧的api的时候用的,编译的时候会用产生警告信息,可以设定在程序里的所有的元素上.

一个是@suppresswarnings:这一个类型可以来暂时把一些警告信息消息关闭.

如果不清楚上面三个类型的具体用法,各位可以baidu或google一下的,很简单的。

第二部分:讲一下annotation的概念,再来讲一下怎样设计自己的annotation.

首先在jdk自带的java.lang.annotation包里,打开如下几个源文件:

1、源文件target.java

代码

@documented 
@retention(retentionpolicy.runtime) 
@target(elementtype.annotation_type) 
public @interface target { 
 elementtype[] value(); 
}

其中的@interface是一个关键字,在设计annotations的时候必须把一个类型定义为@interface,而不能用class或interface关键字(会不会觉得sun有点吝啬,偏偏搞得与interface这么像).

2、源文件retention.java

代码

@documented 
@retention(retentionpolicy.runtime) 
@target(elementtype.annotation_type) 
public @interface retention { 
 retentionpolicy value(); 
}

看到这里,大家可能都模糊了,都不知道在说什么,别急,往下看一下.

在上面的文件都用到了retentionpolicy,elementtype这两个字段,你可能就会猜到这是两个java文件.的确,这两个文件的源代码如下:

3、源文件retentionpolicy.java

代码

public enum retentionpolicy { 
 source, 
 class, 
 runtime 
}

这是一个enum类型,共有三个值,分别是source,class 和 runtime.

source代表的是这个annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,annotation的数据就会消失,并不会保留在编译好的.class文件里面。

class的意思是这个annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这一些信息加载到虚拟机(jvm)中去.注意一下,当你没有设定一个annotation类型的retention值时,系统默认值是class.

第三个,是runtime,表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到jvm中去的.

举一个例子,如@override里面的retention设为source,编译成功了就不要这一些检查的信息;相反,@deprecated里面的retention设为runtime,表示除了在编译时会警告我们使用了哪个被deprecated的方法,在执行的时候也可以查出该方法是否被deprecated.

4、源文件elementtype.java

代码

public enum elementtype { 
 type, field, method, parameter, constructor, 
 local_variable, annotation_type,package 
}

@target里面的elementtype是用来指定annotation类型可以用在哪一些元素上的.说明一下:type(类型), field(属性), method(方法), parameter(参数), constructor(构造函数),local_variable(局部变量), annotation_type,package(包),其中的type(类型)是指可以用在class,interface,enum和annotation类型上.

另外,从1的源代码可以看出,@target自己也用了自己来声明自己,只能用在annotation_type之上.

如果一个annotation类型没有指明@target使用在哪些元素上,那么它可以使用在任何元素之上,这里的元素指的是上面的八种类型.

举几个正确的例子:

@target(elementtype.method) 
@target(value=elementtype.method) 
@target(elementtype.method,elementtype.constructor)

具体参考一下javadoc文档

上面一下1和2的源文件,它们都使用了@documented,@documented的目的就是让这一个annotation类型的信息能够显示在javaapi说明文档上;没有添加的话,使用javadoc生成api文档的时候就会找不到这一个类型生成的信息.

另外一点,如果需要把annotation的数据继承给子类,那么就会用到@inherited这一个annotation类型.

 

第三部分:下面讲的设计一个最简单的annotation例子,这一例子共用四个文件;

1、description.java

代码

package lighter.javaeye.com; 

import java.lang.annotation.documented; 
import java.lang.annotation.elementtype; 
import java.lang.annotation.retention; 
import java.lang.annotation.retentionpolicy; 
import java.lang.annotation.target; 

@target(elementtype.type) 
@retention(retentionpolicy.runtime) 
@documented 
public @interface description { 
 string value(); 
}

说明:所有的annotation会自动继承java.lang.annotation这一个接口,所以不能再去继承别的类或是接口.

最重要的一点,annotation类型里面的参数该怎么设定:

第一,只能用public或默认(default)这两个访问权修饰.例如,string value();这里把方法设为defaul默认类型.

第二,参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和string,enum,class,annotations等数据类型,以及这一些类型的数组.例如,string value();这里的参数成员就为string.

第三,如果只有一个参数成员,最好把参数名称设为"value",后加小括号.例:上面的例子就只有一个参数成员.

2、name.java

代码

package lighter.javaeye.com; 

import java.lang.annotation.documented; 
import java.lang.annotation.elementtype; 
import java.lang.annotation.retention; 
import java.lang.annotation.retentionpolicy; 
import java.lang.annotation.target; 

//注意这里的@target与@description里的不同,参数成员也不同 
@target(elementtype.method) 
@retention(retentionpolicy.runtime) 
@documented 
public @interface name { 
 string originate(); 
 string community(); 
}

3、javaeyer.java

代码

package lighter.javaeye.com; 

@description("****,做最棒的软件开发交流社区") 
public class javaeyer { 
 @name(originate="创始人:****",community="****") 
 public string getname() 
 { 
  return null; 
 } 

 @name(originate="创始人:****",community="****") 
 public string getname2() 
 { 
  return "****"; 
 } 
}

4、最后,写一个可以运行提取javaeyer信息的类testannotation

代码

package lighter.javaeye.com; 

import java.lang.reflect.method; 
import java.util.hashset; 
import java.util.set; 

public class testannotation { 
 /** 
 * author lighter 
 * 说明:具体关天annotation的api的用法请参见javadoc文档 
 */ 
 public static void main(string[] args) throws exception { 
  string class_name = "lighter.javaeye.com.javaeyer"; 
  class test = class.forname(class_name); 
  method[] method = test.getmethods(); 
  boolean flag = test.isannotationpresent(description.class); 
  if(flag) 
  { 
   description des = (description)test.getannotation(description.class); 
   system.out.println("描述:"+des.value()); 
   system.out.println("-----------------"); 
  } 

  //把javaeyer这一类有利用到@name的全部方法保存到set中去 
  set<method> set = new hashset<method>(); 
  for(int i=0;i<method.length;i++) 
  { 
   boolean otherflag = method[i].isannotationpresent(name.class); 
   if(otherflag) set.add(method[i]); 
  } 
  for(method m: set) 
  { 
   name name = m.getannotation(name.class); 
   system.out.println(name.originate()); 
   system.out.println("创建的社区:"+name.community()); 
  } 
 } 
}

5、运行结果:

描述:****,做最棒的软件开发交流社区

-----------------

创始人:****

创建的社区:****

创始人:****

创建的社区:****

扫描关注微信公众号