java的注解

以下内容为基础用法,我这里有个问题,想跟大家伙探讨.关于ElementType中的五个属性的用法,到底怎么用?解释字面意思的不要留言了.

定义一个注解

package com.datang.pet.data.anno;
public @interface NotNull {
}
View Code

以上代码定义了一个@NotNull注解.这个注解可以在任意的地方被使用.

package com.datang.pet.data.anno;

@NotNull
public class TestBean {

    @NotNull
    public String name;

    @NotNull
    public TestBean(){}

    @NotNull
    public void show(@NotNull int age){
        @NotNull
        int s = 12;
    }
}
View Code

以上代码则使用到了@NotNull注解,可以看到,我们几乎可以在一个类的任意地方使用自定义的注解.

限定使用范围 @Target

自定义注解可以使用元注解修饰,所谓元注解就是JDK包中带的注解,通常用来描述自定义注解的使用权限.

@Target元注解就是这样.我们可以将该注解添加到@NotNull自定义注解上.@Target注解的value属性的值是数组类型,数组的元素必须为ElementType的枚举项

/*
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package java.lang.annotation;

/**
 * Indicates the contexts in which an annotation type is applicable. The
 * declaration contexts and type contexts in which an annotation type may be
 * applicable are specified in JLS 9.6.4.1, and denoted in source code by enum
 * constants of {@link ElementType java.lang.annotation.ElementType}.
 *
 * <p>If an {@code @Target} meta-annotation is not present on an annotation type
 * {@code T} , then an annotation of type {@code T} may be written as a
 * modifier for any declaration except a type parameter declaration.
 *
 * <p>If an {@code @Target} meta-annotation is present, the compiler will enforce
 * the usage restrictions indicated by {@code ElementType}
 * enum constants, in line with JLS 9.7.4.
 *
 * <p>For example, this {@code @Target} meta-annotation indicates that the
 * declared type is itself a meta-annotation type.  It can only be used on
 * annotation type declarations:
 * <pre>
 *    &#064;Target(ElementType.ANNOTATION_TYPE)
 *    public &#064;interface MetaAnnotationType {
 *        ...
 *    }
 * </pre>
 *
 * <p>This {@code @Target} meta-annotation indicates that the declared type is
 * intended solely for use as a member type in complex annotation type
 * declarations.  It cannot be used to annotate anything directly:
 * <pre>
 *    &#064;Target({})
 *    public &#064;interface MemberType {
 *        ...
 *    }
 * </pre>
 *
 * <p>It is a compile-time error for a single {@code ElementType} constant to
 * appear more than once in an {@code @Target} annotation.  For example, the
 * following {@code @Target} meta-annotation is illegal:
 * <pre>
 *    &#064;Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
 *    public &#064;interface Bogus {
 *        ...
 *    }
 * </pre>
 *
 * @since 1.5
 * @jls 9.6.4.1 @Target
 * @jls 9.7.4 Where Annotations May Appear
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    ElementType[] value();
}
View Code
/*
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package java.lang.annotation;

/**
 * The constants of this enumerated type provide a simple classification of the
 * syntactic locations where annotations may appear in a Java program. These
 * constants are used in {@link Target java.lang.annotation.Target}
 * meta-annotations to specify where it is legal to write annotations of a
 * given type.
 *
 * <p>The syntactic locations where annotations may appear are split into
 * <em>declaration contexts</em> , where annotations apply to declarations, and
 * <em>type contexts</em> , where annotations apply to types used in
 * declarations and expressions.
 *
 * <p>The constants {@link #ANNOTATION_TYPE} , {@link #CONSTRUCTOR} , {@link
 * #FIELD} , {@link #LOCAL_VARIABLE} , {@link #METHOD} , {@link #PACKAGE} ,
 * {@link #PARAMETER} , {@link #TYPE} , and {@link #TYPE_PARAMETER} correspond
 * to the declaration contexts in JLS 9.6.4.1.
 *
 * <p>For example, an annotation whose type is meta-annotated with
 * {@code @Target(ElementType.FIELD)} may only be written as a modifier for a
 * field declaration.
 *
 * <p>The constant {@link #TYPE_USE} corresponds to the 15 type contexts in JLS
 * 4.11, as well as to two declaration contexts: type declarations (including
 * annotation type declarations) and type parameter declarations.
 *
 * <p>For example, an annotation whose type is meta-annotated with
 * {@code @Target(ElementType.TYPE_USE)} may be written on the type of a field
 * (or within the type of the field, if it is a nested, parameterized, or array
 * type), and may also appear as a modifier for, say, a class declaration.
 *
 * <p>The {@code TYPE_USE} constant includes type declarations and type
 * parameter declarations as a convenience for designers of type checkers which
 * give semantics to annotation types. For example, if the annotation type
 * {@code NonNull} is meta-annotated with
 * {@code @Target(ElementType.TYPE_USE)}, then {@code @NonNull}
 * {@code class C {...}} could be treated by a type checker as indicating that
 * all variables of class {@code C} are non-null, while still allowing
 * variables of other classes to be non-null or not non-null based on whether
 * {@code @NonNull} appears at the variable's declaration.
 *
 * @author  Joshua Bloch
 * @since 1.5
 * @jls 9.6.4.1 @Target
 * @jls 4.1 The Kinds of Types and Values
 */
public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,

    /** Field declaration (includes enum constants) */
    FIELD,

    /** Method declaration */
    METHOD,

    /** Formal parameter declaration */
    PARAMETER,

    /** Constructor declaration */
    CONSTRUCTOR,

    /** Local variable declaration */
    LOCAL_VARIABLE,

    /** Annotation type declaration */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE,

    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}
View Code

我们这里给@NotNull注解使用几个属性试试.

package com.datang.pet.data.anno;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(value = {ElementType.TYPE})
public @interface NotNull {
}
View Code

当我们给@Target元注解的value赋值为ElementType.TYPE时,发现使用@NotNull注解的TestBean类爆红了.显示@NotNull不能使用在成员变量,构造器等等.原因就是,当一个自定义注解没有使用@Target属性时,它并不限制使用范围,一旦使用了@Target元注解则使用范围只为@Target注解的value值.

我们给@NotNull注解多增加几个可使用范围.

package com.datang.pet.data.anno;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, 
        ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})
public @interface NotNull {
}
View Code

@Target元注解声明了6种可使用范围,分别为类,方法,成员变量,构造器,方法参数,局部变量.这六种也是最长用的.其实JDK直到1.8已经支持10种使用范围了.

注解的属性

元注解@Target有属性value,我们自定义的注解也可以有属性.

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
        ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})
public @interface NotNull {
    
    String value();
}
View Code

我们给自定义注解@NotNull声明了一个value属性.此时使用@NotNull注解的类报错了,原因是,我们声明了value属性,但是没有给出值.

@NotNull(value = "a")
public class TestBean {

    @NotNull(value = "a")
    public String name;

    @NotNull(value = "a")
    public TestBean() {
    }


    @NotNull("a")
    public void show(@NotNull("a") int age) {
        @NotNull("a")
        int s = 12;
    }
}
View Code

给注解增加属性,若注解的属性为value且仅有这一个则可以忽略属性名.

如果有多个属性,则不能省略value属性名.

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
        ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})
public @interface NotNull {

    String value();
    
    String name();
}
View Code

 

我们可以在创建注解时,给属性赋值默认值,这样使用该注解时,则不会要求必须覆盖此属性.

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
        ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})
public @interface NotNull {

    String value();

    String name();

    int age() default 100;
}
View Code

读取注解

当我们使用注解在代码中,其实没有实际的意义.注解的意义在于我们这样处理带有这些注解的类,方法.

 那么为什么此时我们获取不到注解呢?原因是我们自定义的注解还需要有@Retention注解.该注解有value属性,属性的类型是RetentionPolicy

该类型有三个可选值.

/*
 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package java.lang.annotation;

/**
 * Annotation retention policy.  The constants of this enumerated type
 * describe the various policies for retaining annotations.  They are used
 * in conjunction with the {@link Retention} meta-annotation type to specify
 * how long annotations are to be retained.
 *
 * @author  Joshua Bloch
 * @since 1.5
 */
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}
View Code

SOURCE注释将被编译器丢弃。CLASS保存在class文件中,是默认的值,通过特定的class文本读取工具可以读取到,常规的我们使用第三个值.RUNTIME在运行时被VM保留,因此它们可以被反射性地读取。

package com.datang.pet.data.anno;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
        ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface NotNull {

    String value();

    String name();

    int age() default 100;
}
View Code

读取注解的属性值

package com.datang.pet.data.anno;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class Main {
    public static void main(String[] args) throws Exception {
        TestBean testBean = new TestBean();
        Class<? extends TestBean> aClass = testBean.getClass();

        NotNull annotation = aClass.getAnnotation(NotNull.class);
        if (annotation != null) {
            System.out.println("TestBean类上有@NotNull注解");
            String value = annotation.value();
            String name = annotation.name();
            int age = annotation.age();
            System.out.println(value + "--" + name + "--" + age);
        } else {
            System.out.println("TestBean类上没有@NotNull注解");
        }

        Field nameField = aClass.getField("name");
        NotNull annotation1 = nameField.getAnnotation(NotNull.class);
        if (annotation1 != null) {
            System.out.println("name上有@NotNull注解");
            String value = annotation1.value();
            String name = annotation1.name();
            int age = annotation1.age();
            System.out.println(value + "--" + name + "--" + age);
        } else {
            System.out.println("name上没有@NotNull注解");
        }

        Constructor<? extends TestBean> declaredConstructor = aClass.getDeclaredConstructor();
        NotNull annotation2 = declaredConstructor.getAnnotation(NotNull.class);
        if (annotation2 != null) {
            System.out.println("构造器上有@NotNull注解");
            String value = annotation2.value();
            String name = annotation2.name();
            int age = annotation2.age();
            System.out.println(value + "--" + name + "--" + age);
        } else {
            System.out.println("构造器上没有@NotNull注解");
        }

        Method showMethod = aClass.getMethod("show",int.class);
        NotNull annotation3 = showMethod.getAnnotation(NotNull.class);
        if (annotation3 != null) {
            System.out.println("show方法上有@NotNull注解");
            String value = annotation3.value();
            String name = annotation3.name();
            int age = annotation3.age();
            System.out.println(value + "--" + name + "--" + age);
        } else {
            System.out.println("show方法上没有@NotNull注解");
        }

        Parameter[] parameters = showMethod.getParameters();
        for (Parameter parameter:parameters){
            String name1 = parameter.getName();
            if (parameter.getName().equals("age")){
                NotNull annotation4 = parameter.getAnnotation(NotNull.class);
                System.out.println("age参数上有@NotNull注解");
                String value = annotation4.value();
                String name = annotation4.name();
                int age = annotation4.age();
                System.out.println(value + "--" + name + "--" + age);
            }
        }




    }
}
View Code

子类可以继承父类的注解吗?

package com.datang.pet.data.anno;

public class TestBean2 extends TestBean{

}
View Code

从结果上看,子类确实没有继承到父类的注解.

@Inherited元注解注释到自定义注解上,则表示该注解可以继承.父类只能是class,不能是抽象的,也不能是接口.

package com.datang.pet.data.anno;

import java.lang.annotation.*;

@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
        ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface NotNull {

    String value();

    String name();

    int age() default 100;
}
View Code

 

原文地址:https://www.cnblogs.com/zumengjie/p/13020142.html