jdk1.5 It's starting to offer 4 Meta annotation :@Target,@Retention,@Documented,@Inherited. What is meta annotation ? It's the annotation of the annotation .
In program development , Sometimes we need to customize an annotation , This custom annotation class needs to be modified by meta annotation , To define some basic features of the class .
for example , We create one LogAnnotation Custom annotation class for :
@Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) public @
interface LogAnnotation { String module() default ""; }
@interface It means to declare an annotation , The method name corresponds to the parameter name , The return value type corresponds to the parameter type .
<>@Target
@Target Annotations are used to define where annotations are used , If there is no such item , Indicates that annotations can be used anywhere .@Target The format is :
// Single parameter @Target({ ElementType.METHOD }) // Multiparameter @Target(value = {ElementType.
METHOD,ElementType.TYPE})
@Target Of ElementType There are the following types of values :
* TYPE: class , Interface or enumeration
* FIELD: field , Contains enumeration constants
* METHOD: method
* PARAMETER: parameter
* CONSTRUCTOR: Construction method
* LOCAL_VARIABLE: local variable
* ANNOTATION_TYPE: annotation type
* PACKAGE: package
<>@Retention
@Retention Annotations are used to indicate the lifetime of decorated annotations , Which stage will be retained . The format is :
@Retention(RetentionPolicy.RUNTIME)
RetentionPolicy The values of include the following three types :
* SOURCE: Source level retention , Discard after compilation .
* CLASS: Compile level retention , Compiled class There is an error in the file , stay jvm Run time discard , This is the default .
* RUNTIME: Run level retention , Compiled class There is an error in the file , stay jvm Run time retention , Can be called by reflection .
<>@Documented
Notes indicating modifications , Can be, for example javadoc Documentation of such tools , Mark only , No member value .
<>@Inherited
@Inherited Annotation is used to mark whether the annotation of a parent class can be inherited by a subclass , If an annotation needs to be inherited by its subclass , Is used directly at declaration time @Inherited Annotation is enough . If this comment is not written , Cannot be inherited by subclasses . Let's do a test :
// Customize an annotation @interface MyAnnoation { public String key() default "key1"; public
Stringvalue() default "value1"; } @Target(ElementType.TYPE) @Retention(
RetentionPolicy.RUNTIME) @Documented // If the parent class uses HeritedApplication annotation , The subclass should inherit
@Inherited @MyAnnoation @interface HeritedApplication { }
// The parent class uses @HeritedApplication annotation @HeritedApplication class Person { } class Student
extends Person{ } class AnnotationInherited{ public static void main(String[]
args) throws Exception { Class clazz = Student.class;
//Student Does class have @HeritedApplication if(clazz.isAnnotationPresent(HeritedApplication
.class)){ System.out.println("true"); } } }
Run the program , The results are as follows true.
Technology
Daily Recommendation