Tiger Series 4: Annotation Part II: Custom Annotation

xiaoxiao2021-03-06  39

1. Customize annotation type

(1) Define annotation type

l Using @Interface declaration Annotation Type

Public @Interface inprogress {

}

l Use annotation type

Public class testannotation {

@Inprocess

Public void test () {

}

}

l If the annotation type and the class using it are not in the same package, you can use the IMPORT Annotation type to use @Inprogress directly.

(2) Add member

l Annotation Types can have a member variable to provide useful information

l Defining data members do not need to define getter and setter methods, just define a method named by a member name and specify the type of data required to return.

l Simple example:

Public @Interface Todo {

String value ();

}

l Use the Annotation type with a member:

Public class testannotation {

@Inprocess

@Todo ("Need to Finish this Method Later")

Public void test () {

}

}

(3) Set the default value

l Set the default value for members of the Annotation type, you need to use the default key when declared members:

Public @Interface grouptodo {

Public enum severity {

Critical, Important, Trivial, Documentation

}

Severity severity () Default severity.important;

String item ();

String assignedto ();

String Dateassigned ();

}

L Of course, the type of default value must be identical to the type of member variable declaration.

l The following is an example using the default value:

Public class testannotation {

@Inprocess

@Grouptodo

item = "Need to Finish this Method Later",

Assignedto = "Nelson_TU",

Dateassigned = "2005/02/05"

)

Public void test () {

}

}

l The following is an example of rewriting the default value:

Public class testannotation {

@Inprocess

@ TODO ("Need to Finish this Method Later")

@Grouptodo

Severity = grouptodo.severity.documentation,

item = "Need to Finish this Method Later",

Assignedto = "Nelson_TU",

Dateassigned = "2005/02/05"

)

Public void test () {

}

}

2, Yuan Annotation

L Yuan Annotation is Annotation's Annotation, JDK5 provides four predefined meta annotation (1) @target

l @Target Specifies the program element that can be applied to the Annotation type to make an ANNOTATION type in other program elements.

l The type of program element is defined by java.lang.annotation.ementType enumeration:

Package java.lang.annotation;

Public enum elementtype {

TYPE, // Class, Interface, or Enum (But not annotation)

Field, // Field (Including Enumerated Values)

Method, // Method (Does Not Include Construction ")

Parameter, // Method Parameter

Constructor, // Constructor

Local_variable, // local variable or catch clause

Annotation_Type, // annotation type (Meta-annotations)

Package // java package

}

l The following is an example of using @Target:

@Target ({ElementType.Type,

ElementType.method,

ElementType.constructor,

ElementType.annotation_type})

Public @Interface Todo {

String value ();

}

(2) @Retrion

L @ReTENTION and Java compiler processes the approach to annotation types

l These methods are defined by java.lang.annotation.RetentionPolicy enumeration:

Package java.lang.annotation;

Public enum restionpolicy {

Source, // annotation is discarded by the compiler

Class, // annotation is store, but ignore by the VM

Runtime // annotation is stored in the class file and read by the VM

}

l Use the @Retrion example to see the @Documented

(3) @document

l @documented indicates that you need ANNOTATION in Javadoc (the default is not included)

l The following is an example of using @Documented:

@Document

@ReTENTION (RETENTIONPOLICY.RUNTIME)

Public @Interface infrocess {

}

l A skill using @documented is to specify a retention policy to retectionPolicy.Runtime: This, Annotation will remain in the compiled class file and load it by virtual machine, then javadoc can extract Annotation, add to the HTML document Medium (4) @inherited

l @inherited is the most complicated, the least use of the most, and is most likely to confuse.

l Suppose using the @Inprogress tag a class that is being developed. As long as the @ Documented, Annotation information will appear in Javadoc; now you have to write a new class, expand the class being developed, then use the subclass, or view it. Documentation, there is no way to indicate where there is no completion; and it is hoping that the Annotation information of @Inprogress will be taken into the subclass, which requires @nnerited.

l The following example:

@Document

@Nnherited

@ReTENTION (RETENTIONPOLICY.RUNTIME)

Public @Interface infrocess {

}

转载请注明原文地址:https://www.9cbs.com/read-60010.html

New Post(0)