Java注解详解

来源:互联网 发布:守望先锋网络很卡 编辑:程序博客网 时间:2024/06/05 16:08

JDK提供三种常见注解:

(1)@Override注解表示子类要重写从父类继承的方法。如果该方法不是覆盖父类的方法,将会在编译时报错

(2)@Deprecated注解表示api中某些类,方法,成员已经过时,是不推荐被使用的。

(3)@SuppressWarnings表示压制警告。

     @SuppressWarnings与前两个注释有所不同,你需要添加一个参数才能正确使用,这些参数值都是已经定义好了的,我们选择性的使用就好了,参数如下:

deprecation   使用了过时的类或方法时的警告

unchecked  执行了未检查的转换时的警告,例如当使用集合时没有用泛型 (Generics) 来指定集合保存的类型

fallthrough   当 Switch 程序块直接通往下一种情况而没有 Break 时的警告

path   在类路径、源文件路径等中有不存在的路径时的警告

serial 当在可序列化的类上缺少 serialVersionUID 定义时的警告

finally    任何 finally 子句不能正常完成时的警告

all 关于以上所有情况的警告


程序员可以自定义注解:

当我们用@interface关键字定义一个注解类型的时候,该注解类型隐含地继承了java.lang.annotation.Annotation接口;如果我们定义了一个接口,该接口继承自Annotation接口,那么我们所定义的仍然是一个接口而不是注解类型;Annotation本身是接口而不是注解类型。


自定义注解实现代码

1. 只有一个参数的Annotation实现
[java] view plaincopy
  1. package chb.test.annotation;  
  2. import java.lang.annotation.Documented;  
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7. @Target(ElementType.TYPE)  
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Documented  
  10. public @interface MyAnnotation1 {  
  11.         String value();  
  12. }  
 
2. 有两个参数的Annotation实现
[java] view plaincopy
  1. package chb.test.annotation;  
  2. import java.lang.annotation.Documented;  
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7. @Target(ElementType.METHOD)  
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Documented  
  10. public @interface MyAnnotation2 {  
  11.         String description();  
  12.         boolean isAnnotation();  
  13. }  
3. Annotation实验类
[java] view plaincopy
  1. package chb.test.annotation;  
  2. @MyAnnotation1("this is annotation1")  
  3. public class AnnotationDemo {  
  4.         @MyAnnotation2(description="this is annotation2",isAnnotation=true)  
  5.         public void sayHello(){  
  6.                 System.out.println("hello world!");  
  7.         }  
  8. }  
4.Annotation测试说明类
[java] view plaincopy
  1. package chb.test.annotation;  
  2. import java.lang.reflect.Method;  
  3. import org.junit.Test;  
  4. public class TestAnnotation {  
  5.         @Test  
  6.         public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException{  
  7.                 Class<?> cls = Class.forName("chb.test.annotation.AnnotationDemo");  
  8.                 boolean flag = cls.isAnnotationPresent(MyAnnotation1.class);  
  9.                 if(flag){  
  10.                         System.out.println("判断类是annotation");  
  11.                         MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class);  
  12.                         System.out.println(annotation1.value());  
  13.                 }  
  14.                   
  15.                 Method method = cls.getMethod("sayHello");  
  16.                 flag = method.isAnnotationPresent(MyAnnotation2.class) ;  
  17.                 if(flag){  
  18.                         System.out.println("判断方法也是annotation");  
  19.                         MyAnnotation2 annotation2 = method.getAnnotation(MyAnnotation2.class);  
  20.                         System.out.println(annotation2.description()+"/t"+annotation2.isAnnotation());  
  21.                 }  
  22.         }  
  23.           
  24. }  

实验结果,控制台打出如下信息:
 
判断类是annotation
this is annotation1
判断方法也是annotation
this is annotation2     true
 
代码简介及说明
1. MyAnnotation1中的@Target(ElementType.TYPE)
      @Target里面的ElementType是用来指定Annotation类型可以用在哪些元素上的.例如:
       TYPE(类型)、FIELD(属性)、METHOD(方法)、PARAMETER(参数)、CONSTRUCTOR(构造函数)、LOCAL_VARIABLE(局部变量),、PACKAGE(包),其中的TYPE(类型)是指可以用在Class,Interface,Enum和Annotation类型上。

2. MyAnnotation1中的@Retention(RetentionPolicy.RUNTIME)
 RetentionPolicy 共有三种策略,分别为:
  • SOURCE:这个Annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,Annotation的数据就会消失,并不会保留在编译好的.class文件里面
  • CLASS:这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这些信息加载到JVM中。注:默认策略为CLASS类型
  • RUNTIME:表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的
3. MyAnnotation1中的@Documented
目的就是将这一Annotation的信息显示在JAVA API文档上,如果没有增加@Documented的话,JAVA API文档上不会显示相关annotation信息
4. MyAnnotation1中的@interface
   关键字,表示该类为Annotation定义
5. MyAnnotation1中的 String value();
   表示有一个成员参数,名字为value,访问权为默认(default)修饰符,注意以下两点:
  • 访问权只能用public和默认(default)修饰
  • 参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和String,Enum,Class,annotations等数据类型,以及这一些类型的数组
6.AnnotationDemo中的@MyAnnotation1("this is annotation1")
   因为MyAnnotation1只有一个参数,因此可以直接在括号中写上value值。注:如果Annotation只有一个参数,则建议最好将该参数名称定义为value
7.TestAnnotation中的cls.isAnnotationPresent(MyAnnotation1.class)
    判断该类是否使用了MyAnnotation1的注释
8. TestAnnotation中的MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class)
    返回该类针对MyAnnotation1的注释
9. TestAnnotation中的method.isAnnotationPresent(MyAnnotation2.class)
    判断该方法是否使用了MyAnnotation2的注释