Annotation Knowledge Point Collation

Source: Internet
Author: User

Recently read a blog post on notes written by someone else, all sorted out according to the content provided by others.


Originally from: http://www.cnblogs.com/peida/archive/2013/04/26/3038503.html


1, Yuan annotation
The function is responsible for annotating other annotations. Java5.0 defines 4 standard meta-annotation types, which are used to provide descriptions of other annotation types.
[Email protected],-> @Target illustrates the range of objects modified by annotation: annotation can be used for packages, types (classes, interfaces, enumerations, annotation types)
, type members (methods, construction methods, member variables, enumeration values), method parameters, and local variables (such as loop variables, catch parameters).
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
5.PACKAGE: Used to describe the package
6.PARAMETER: Used to describe parameters
7.TYPE: Used to describe classes, interfaces (including annotation types) or enum declarations
[Email protected],-> defines how long the annotation is retained: Some annotation appear only in the source code and are discarded by the compiler;
Others are compiled in the class file, and the annotation compiled in the class file may be ignored by the virtual machine, while others will be read when the class is loaded
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)
[email protected],-> is used to describe other types of annotation that should be used as public APIs for annotated program members, so it can be documented by tools such as Javadoc.
Documented is a markup annotation with no members.
[Email protected],-> 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.
2. 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
5.Annotation type
6. Arrays of all types above
3. Annotation Application
Java uses the annotation interface to represent annotations in front of the program element, which is the parent interface for all annotation types.
In addition, Java adds a annotatedelement interface under the Java.lang.reflect package,
This interface represents a program element in a program that can accept annotations, and the interface has several implementation classes:


Class: Classes Definition
Constructor: Constructor definition
Field: Tired member variable definition
Method: Methods Definition of Class
Package: Packages definition for Class

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Annotation Knowledge Point Collation

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.