1. Basic syntax
Java code
Import Java.lang.annotation.ElementType; Import Java.lang.annotation.Target; The @Bind tag. @Target (Elementtype.method) @Retention (retentionpolicy.runtime) public @interface Bind {public String name (); public int time () default 0; }
above is a simple @bind annotation class for the method level, which is somewhat like the structure of the interface, in fact, as with any other Java interface, annotations will be compiled into class files.
/** * Use the @Bind tag. */public class Bindcase {@Bind (name= ' case ', time=1) public void Method () {//do something: } public void Method1 () {//do something: } @Bind (name= "Case1", time=20) public void Method2 () {//do something: } }
The
writes the annotation processor:
The API that extends the reflection mechanism in Jase 1.5, provides us with the appropriate API for the annotation processor, and can also parse the annotated Java with an external tool apt Code.
public class bindcasetracker{ private static logger logger = logger.getlogger (BindCaseTracker.class); public static void Printbindcase (Class<?> bindclass) { assert bindclass != null; for ( Method method : bindclass.getdeclaredmethods ()) { bind bind = method.getannotation (Bind.class); if (Bind == null) continue; // Not found annotation. logger.debug (String.Format ("found [%s] bind case : %s-%d", method .getname (), bind.name (), bind.time ())); } } public static void main (String[] args) { bindcasetracker.printbindcase (bindcase.class); } }
/* Output: [debug] 11-08 14:15 Found [method] Bind case:case-1 [debug] 11-08 14:15 Found [method2] Bind case:case1-20 *///~
2. Meta-annotations
Three common standard annotations (Override, Deprecated, suppresswarnings) and four meta annotations are built into the J2SE:
@Target: indicates where the annotation can be used. Available ElementType enumeration types are mainly: TYPE : class, interface, or enum declaration FIELD: domain (attribute) Declarations METHOD: Method Declaration PARAMETER: parameter declaration CONSTRUCTOR: Construction Method Declaration local_variable: local variable declaration annotation_type: Comment Type declaration package: Package Declaration @Retention: indicates the level at which the annotation information needs to be saved. Available Retentionpolicy enumeration types are mainly: SOURCE: annotations will be discarded by the compiler. class : annotations may be in the class file. But will be discarded by the VM. RUNTIME: The VM will also save annotations at run time (use this value if you need to read the annotations through reflection). @Documented: to include this annotation in Javadoc. @Inherited: allows subclasses to inherit annotations from the parent class.
3. Tips for use
A. If you wish to define annotations for a variety of elementtype, you can write:
Import static Java.lang.annotation.ElementType @Target ({METHOD, FIELD, Annotation_type, CONSTRUCTOR, PARAMETER})
B. When declaring a method in an annotation, you can specify a default value by using defaults.
C. Method return types in declarative annotations can be used in conjunction with generics, such as:
class<? Extends payload>[] Payload () default {};
D. Nested annotations can be defined in the annotation class, such as:
Import static Java.lang.annotation.ElementType @Target ({METHOD, FIELD, Annotation_type, CONSTRUCTOR, PARAMETER}) @Re Tention (RUNTIME) @Documented public @interface notnull {String message () default "{Javax.validation.constraints.No Tnull.message} "; @Target ({METHOD, FIELD, Annotation_type, CONSTRUCTOR, PARAMETER}) @Retention (RUNTIME) @Documented @interf Ace List {notnull[] value (); } }
@NotNull. List (value = {@NotNull}) protected list<?> list;
E. There are few built-in annotations available in Jase, but JBoss provides a VALIDATION-API class library that provides common validation annotations. Interested friends can download and see their maven dependencies for:
<dependency> <groupId>javax.validation</groupId> <artifactid>validation-api</ Artifactid> <version>1.0</version> </dependency>
Java Custom Annotation method