定义注解
java.lang.annotation.Target
/* * 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> * @Target(ElementType.ANNOTATION_TYPE) * public @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> * @Target({}) * public @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> * @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD}) * public @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(); }
java.lang.annotation.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 }
RUNTIME,注解会在CLASS的字节码文件中存在,在运行时通过反射可以拿到。SOURCE在编译的时候就会被忽略掉。
com.mmall.concurrency.annoations.ThreadSafe
package com.mmall.concurrency.annoations; 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.SOURCE) public @interface ThreadSafe { String value() default ""; }
com.mmall.concurrency.annoations.NotThreadSafe
package com.mmall.concurrency.annoations; 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.SOURCE) public @interface NotThreadSafe { String value() default ""; }
com.mmall.concurrency.annoations.Recommend
package com.mmall.concurrency.annoations; 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.SOURCE) public @interface Recommend { String value() default ""; }
com.mmall.concurrency.annoations.NotRecommend
package com.mmall.concurrency.annoations; 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.SOURCE) public @interface NotRecommend { String value() default ""; }