In-depth understanding of Java: Annotations (Annotation) Getting Started with custom annotations

Source: Internet
Author: User

 To learn more about annotations, we must be able to define our own annotations and use annotations to define our own annotations before we have to understand the syntax of the meta-annotations and related definition annotations that Java provides us.

Meta-Annotations:

[ Email protected],
[email  PROTECTED],
[ Email protected],
[email   PROTECTED]

  @Target:

@Target illustrates the range of objects that annotation modifies: annotation can be used for packages, types (classes, interfaces, enumerations, annotation types), type members (methods, constructor methods, member variables, enumeration values), Method parameters and local variables (such as loop variables, catch parameters). Target can be more clearly decorated by using target in the declaration of the annotation type.

  Function: Used to describe the scope of use of annotations (i.e., where the annotations described can be used)

 The values (ElementType) are:

1.CONSTRUCTOR: Used to describe the constructor
2.FIELD: Used to describe the domain
3.local_variable: Used to describe local variables
4.METHOD: Used to describe the method
7.TYPE: Used to describe classes, interfaces ( include annotation type) or enum declaration

Usage examples:

650) this.width=650; "style=" Border:medium none;padding:0px;margin:0px; "alt=" Copy Code "src=" http://common.cnblogs.com/ Images/copycode.gif "/>

@Target (elementtype.type) public @interface Table {/** * datasheet name annotation, the default value is class name * @return */public String Tabl ENAME () Default "ClassName";} @Target (Elementtype.field) public @interface Nodbcolumn {}

650) this.width=650; "style=" Border:medium none;padding:0px;margin:0px; "alt=" Copy Code "src=" http://common.cnblogs.com/ Images/copycode.gif "/>

Note Table can be used for annotation classes, interfaces (including annotation types), or enum declarations, and annotation nodbcolumn can only be used for member variables of the annotation class.

@Retention:

 @Retention defines how long the annotation is retained: Some annotation appear only in the source code and are discarded by the compiler, while others are compiled in the class file The annotation that are compiled in the class file may be ignored by the virtual machine, while others will be read when class is loaded (note that it does not affect class execution because annotation is separated from the class in use). Using this meta-annotation can limit the "life cycle" of annotation.

  Function: Indicates the level at which the annotation information needs to be saved to describe the life cycle of the annotation (i.e., the scope of the annotation being described is valid)

 The values (Retentionpoicy) are:

1.SOURCE: Valid in source file (i.e., source file retention)
2.CLASS: Valid in class file (that is, class reserved)
3.RUNTIME: Valid at run time (that is, runtime retention)

The Retention meta-annotation type has a unique value as a member, and its value is derived from the enumeration type value of Java.lang.annotation.RetentionPolicy. The concrete examples are as follows:

650) this.width=650; "style=" Border:medium none;padding:0px;margin:0px; "alt=" Copy Code "src=" http://common.cnblogs.com/ Images/copycode.gif "/>

@Target (Elementtype.field) @Retention (retentionpolicy.runtime) public @interface Column {public String name () default "    FieldName ";    Public String setfuncname () default "SetField";     Public String getfuncname () default "GetField"; public boolean defaultdbvalue () default false;}

650) this.width=650; "style=" Border:medium none;padding:0px;margin:0px; "alt=" Copy Code "src=" http://common.cnblogs.com/ Images/copycode.gif "/>

The attribute value of the retentionpolicy of column annotations is rutime so that the annotation processor can get the property value of the annotation by reflection, thus doing some logic processing of the runtime

@Documented:

  @ Documented is used to describe other types of annotation that should be used as public APIs for annotated program members, so they can be documented by tools such as Javadoc. Documented is a markup annotation with no members.

650) this.width=650; "style=" Border:medium none;padding:0px;margin:0px; "alt=" Copy Code "src=" http://common.cnblogs.com/ Images/copycode.gif "/>

@Target (Elementtype.field) @Retention (retentionpolicy.runtime) @Documentedpublic @interface Column {public String    Name () default "FieldName";    Public String setfuncname () default "SetField";     Public String getfuncname () default "GetField"; public boolean defaultdbvalue () default false;}

650) this.width=650; "style=" Border:medium none;padding:0px;margin:0px; "alt=" Copy Code "src=" http://common.cnblogs.com/ Images/copycode.gif "/>

@Inherited:

  @Inherited meta-annotation is a markup annotation, @Inherited illustrates that a type that is labeled is inherited. If a annotation type that uses the @inherited modifier is used for a class, the annotation will be used for subclasses of that class.

Note: @Inherited the annotation type is inherited by subclasses of the class being labeled. The annotation class does not inherit from the interface it implements, and the method does not inherit annotation from the method it overloads.

The reflection API enhances this inheritance when the retention of the @inherited annotation type callout annotation is retentionpolicy.runtime. If we use Java.lang.reflect to query the annotation of a @inherited annotation type, the reflection code check will work: Examine the class and its parent class until the specified annotation type is found, or reach the top level of the class inheritance structure.

Instance code:

650) this.width=650; "style=" Border:medium none;padding:0px;margin:0px; "alt=" Copy Code "src=" http://common.cnblogs.com/ Images/copycode.gif "/>

/** * * @author peida * */@Inheritedpublic @interface Greeting {public enum fontcolor{Bule,red,green};    String name (); FontColor fontcolor () default Fontcolor.green;}

650) this.width=650; "style=" Border:medium none;padding:0px;margin:0px; "alt=" Copy Code "src=" http://common.cnblogs.com/ Images/copycode.gif "/>

Custom annotations:

When you use @interface to customize annotations, the Java.lang.annotation.Annotation interface is automatically inherited and other details are automatically completed by the compiler. When you define annotations, you cannot inherit other annotations or interfaces. @interface is used to declare an annotation, in which each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter (the return value type can only be base type, Class, String, enum). You can declare the default value of a parameter through default.

  To define the annotation format:
Public @interface annotation name {definition body}

supported data types for annotation parameters:

1. All basic data Types (int,float,boolean,byte,double,char,long,short)
2.String type
3. Class type
4.enum type BR style= "padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"    > 5.Annotation type

How to set the parameters of the annotation type:
First, it can only be decorated with public or default access rights. For example, String value (); Here the method is set to Defaul default type;
Second, parameter members can only use basic types Byte,short,char,int,long,float,double,boolean eight basic data types and string,enum,class,annotations data types, As well as these types of arrays. For example, String value (), where the parameter member is string;
Thirdly, if there is only one parameter member, it is best to set the parameter name to "value", followed by parentheses. Example: The following example Fruitname annotation has only one parameter member.

650) this.width=650; "id=" Aimg_cxxd5 "class=" Zoom "border=" 0 "src=" https://img.alicdn.com/imgextra/i1/332189337/ tb2oxzvjvxxxxcmxpxxxxxxxxxx_!! 332189337.png "Width="/>

< span>  

System settings: Modify the system name, mail server configuration, SMS account settings, image watermark configuration, configuration
Chat in time: Open Chat window, group chat, one-to-one chat
22. Form building: Drag-and-drop fast custom build form, rich elements, rich text, upload controls, drop-down boxes, etc.
--------------------------------------------------------------------------------------------------------------- ----------Database Management
23. Database backup: Can back up single table, whole library, support local and remote database backup (Java Interface Programming technology, socket programming technology)
Backup timer: Quartz 2.2 Powerful Task Scheduler, multi-threaded backup database, task startup shutdown asynchronous operation
25. Database Restore : Historical backup record, restore database or single table, statistics backup time and file size
SQL Editor: Powerful SQL Editor, support edit Statement complex query statement, generate dynamic report, export Excel


Understanding Java: Annotations (Annotation) Getting started with custom annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.