zoukankan      html  css  js  c++  java
  • Java中的注解基础

    一、元注解
    元注解的作用就是负责注解其他注解。
    
    1.@Target
    @Target用来指明注解所修饰的目标,包括packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。
    
    取值(ElementType)有:
    CONSTRUCTOR:用于描述构造器
    FIELD:用于描述域
    LOCAL_VARIABLE:用于描述局部变量
    METHOD:用于描述方法
    PACKAGE:用于描述包
    PARAMETER:用于描述参数
    TYPE:用于描述类、接口(包括注解类型) 或enum声明
    
    例如:
    
    Target(ElementType.TYPE)
    public @interface AnnotationTest1 {
        public String tableName() default "className";
    }
    
    
    表示AnnotationTest1这个注解用来注解类、接口(包括注解类型) 或enum声明。
    
    @Target(ElementType.FIELD)
    public @interface AnnotationTest2 {
    
    }
    
    表示注解AnnotationTest2用于注解类的成员变量。
    
    2.@Retention
    @Retention定义了该Annotation的生命周期,某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取。
    
    取值(RetentionPoicy)有:
    SOURCE:在源文件中有效(即源文件保留)
    CLASS:在class文件中有效(即class保留)
    RUNTIME:在运行时有效(即运行时保留)
    
    例如:
    
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Column {
        public String name() default "fieldName";
        public String setFuncName() default "setField";
        public String getFuncName() default "getField"; 
        public boolean defaultDBValue() default false;
    }
    
    
    表示Column这个注解用来注解类的成员变量,并且注解一直保留到运行时。
    
    3.@Documented
    @Documented的作用是在生成javadoc文档的时候将该Annotation也写入到文档中。
    
    例如:
    
    @Target(ElementType.METHOD)
    @Documented  
    public @interface DocumentTest {   
        String hello();   
    } 
    
    
    该注解用来修饰成员方法,下面简单的使用;
    
    public class DocumentClass {   
        /**  
         * this is method of doSomething  
         */  
        @DocumentTest(hello = "yahaitt")   
        public void doSomething() {   
            System.out.println("do something");   
        }   
    } 
    
    
    生成的doc文件中如下:
    
    @DocumentTest(hello="yahaitt")
    public void doSomething()
    this is method of doSomething  
    
    
    4.@Inherited
    我们自定义注解(Annotation)时,把自定义的注解标注在父类上,但是它不会被子类所继承,我们可以在定义注解时给我们自定义的注解标注一个@Inherited注解来实现注解继承。
    
    例如:
    
    @Retention(RetentionPolicy.RUNTIME)  
    @Inherited  
    public @interface InheritedTest {  
        String value();  
    }  
    
    
    @InheritedTest("Jadyer")  
    public class Parent {  
        @InheritedTest("JavaEE")  
        public void doSomething() {  
            System.out.println("Parent do something");  
        }  
    }  
    
    
    public class Child extends Parent {  
    
    }
    
    public class Test {  
        public static void main(String[] args) throws SecurityException, NoSuchMethodException {  
            classTest();  
            methodTest();  
        }  
    
        /** 
         * 通过反射方式测试子类是否会继承父类中定义在类上面的注解 
         */  
        public static void classTest(){  
            Class<Child> c = Child.class;  
    
            if (c.isAnnotationPresent(InheritedTest.class)) {  
                InheritedTest inheritedTest = c.getAnnotation(InheritedTest.class);  
                String value = inheritedTest.value();  
                System.out.println(value);  
            }  
        }  
    
        /** 
         * 通过反射方式测试子类是否会继承父类中定义在方法上面的注解 
         */  
        public static void methodTest() throws SecurityException, NoSuchMethodException{  
            Class<Child> c = Child.class;  
            Method method = c.getMethod("doSomething", new Class[]{});  
    
            if (method.isAnnotationPresent(InheritedTest.class)) {  
                InheritedTest inheritedTest = method.getAnnotation(InheritedTest.class);  
                String value = inheritedTest.value();  
                System.out.println(value);  
            }  
        }  
    }  
    
    
    如果父类的注解是定义在类上面,那么子类是可以继承过来的
    如果父类的注解定义在方法上面,那么子类仍然可以继承过来
    如果子类重写了父类中定义了注解的方法,那么子类将无法继承该方法的注解,即子类在重写父类中被@Inherited标注的方法时,会将该方法连带它上面的注解一并覆盖掉
    
    二、Java内建注解
    Java提供了三种内建注解
    @Override——当我们想要复写父类中的方法时,我们需要使用该注解去告知编译器我们想要复写这个方法。这样一来当父类中的方法移除或者发生更改时编译器将提示错误信息。
    @Deprecated——当我们希望编译器知道某一方法不建议使用时,我们应该使用这个注解。Java在javadoc 中推荐使用该注解,我们应该提供为什么该方法不推荐使用以及替代的方法。
    @SuppressWarnings——这个仅仅是告诉编译器忽略特定的警告信息,例如在泛型中使用原生数据类型。它的保留策略是SOURCE并且被编译器丢弃。
    
    三、自定义注解
    使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节。
    定义注解格式:
    public @interface 注解名 {定义体}
    注解参数的可支持数据类型:
    1.所有基本数据类型(int,float,boolean,byte,double,char,long,short)
    2.String类型
    3.Class类型
    4.enum类型
    5.Annotation类型
    6.以上所有类型的数组
    
    Annotation类型里面的参数该怎么设定:
    第一,只能用public或默认(default)这两个访问权修饰.例如,String value();这里把方法设为defaul默认类型;
    第二,参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和 String,Enum,Class,annotations等数据类型,以及这一些类型的数组.例如,String value();这里的参数成员就为String;  
    第三,如果只有一个参数成员,最好把参数名称设为”value”,后加小括号
    第四,可以在使用default为每个参数设置一个默认值。注解元素必须有确定的值,要么在定义注解的默认值中指定,要么在使用注解时指定,非基本类型的注解元素的值不可为null。因此, 使用空字符串或0作为默认值是一种常用的做法。
    
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitName {
        String value() default "";
    }
    
    
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitColor {
    
        public enum Color{ BULE,RED,GREEN};
    
        Color fruitColor() default Color.GREEN;
    
    }
    
    
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitProvider {
         public int id() default -1;
    
         public String name() default "";
    
         public String address() default "";
    }
    
    
    四、注解的使用
    
    public class Apple {
        @FruitName("Apple")
        private String appleName;
    
        @FruitColor(fruitColor=Color.RED)
        private String appleColor;
    
    }
    
    
    参数名只有一个,并且设置为value,在对注解使用赋值的时候可以不用加参数名,直接赋值即可。其他情况需要指定参数名和参数值。
    
    五、注解处理
    Java在java.lang.reflect 包下新增了AnnotatedElement接口,该接口主要有如下几个实现类:
      Class:类定义
      Constructor:构造器定义
      Field:累的成员变量定义
      Method:类的方法定义
      Package:类的包定义
    所以,AnnotatedElement 接口是所有程序元素(Class、Method和Constructor)的父接口,程序通过反射获取了某个类的AnnotatedElement对象之后,程序就可以调用该对象的如下四个个方法来访问Annotation信息:
    
    //返回改程序元素上存在的、指定类型的注解,如果该类型注解不存在,则返回null。
    <T extends Annotation> T getAnnotation(Class<T> annotationClass)
    //返回该程序元素上存在的所有注解。
    Annotation[] getAnnotations()
    //判断该程序元素上是否包含指定类型的注解,存在则返回true,否则返回false.
    boolean is AnnotationPresent(Class<?extends Annotation> annotationClass)
    //返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。(如果没有注释直接存在于此元素上,则返回长度为零的一个数组。)该方法的调用者可以随意修改返回的数组;这不会对其他调用者返回的数组产生任何影响。
    Annotation[] getDeclaredAnnotations()
    
    
    在上面的@Inherited的讲解中也举个相应的例子,我们只需要反射得到Class、Method和Constructor,因为AnnotatedElement是Class、Method和Constructor的父接口,它们都实现了AnnotatedElement,所以,在Class、Method和Constructor中就可以使用AnnotatedElement中声明的方法来得到注解和注解内容。
  • 相关阅读:
    第七章
    第五章
    第六章
    Git使用入门
    源代码的下载和编译
    向中国最牛的前端群-鬼群致敬
    Normalize.css做了哪些事情--看代码
    谷歌浏览器:书签被误删了怎么办
    2013/8月读书计划
    Limu:JavaScript的那些书
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/10401213.html
Copyright © 2011-2022 走看看