Java meta annotations

Source: Internet
Author: User

The role of meta annotations is to be responsible for annotating other annotations. Java defines the 4 standard meta-annotation types, which are used to provide descriptions of other annotations.

@target

@Retention

@Documented

@Inherited

These types can be found in the Java.lang.annotation package with the classes they support

The role of each meta-annotation:

@target

Modifies the range of objects modified by annotation, annotation can be used for package,type (classes, interfaces, enumerations, annotation), and type members (methods, constructor methods, member variables, enumeration values), Method parameters and local variables (loop variables and catch parameters) in the annotation type declaration, use target to clarify its decorated target.

Role: Used to describe where annotations are used

Value ElementType

Constructor: Description constructor

Filed: Description Domain

Local_variable: Used to describe local variables

Method: Used to describe methods

Package: Used to describe packages

Parameter: Used to describe parameters

Type: Used to describe classes, interfaces, including annotation types or enumeration types

Usage examples:

@Target (Elementtype.type)

Public @interface table{

Data table name annotations, default value is class name

Public String tableName () default "ClassName";

}

@Target (Elementtype.feild)

Public @interface nodbcolumn{

 

}

@Retention

@Retention defines the time that the annotation is retained, some annotation appear only in the source code, some are compiled in the class file, and the annotation compiled in the class file may be ignored by the virtual machine. Some are read when class is loaded (does not affect the run of class, annotation is separated from class when used), and the life cycle of annotation can be influenced by using retention.

Action: Indicates what level of comment information needs to be saved

Retentionplicy

Source: Valid in the original file

Class is valid in the class file

Runtime is active at run time

Retention has a unique value as a member, and the value is derived from the Java.lang.annotation.retationpolicy enumeration type value

@Target (elementtype.filed)

@Retention (Retentionpolicy.runtime)

Public @interface column{

Public String name () default "FieldName";

Public String setfuncname () default "setfiled";

Public String getfuncname () default "GetField";

public boolean defaultdbvalue () default false;

}

The attribute value for the retentionpolicy of column annotations is runtime, so that the annotation processor can get

The attribute value of the annotation, thus doing some logical processing of the runtime

@Documented

Used to describe other types of annotation that should be used as public APIs for annotated program members, so can be documented by the Javadoc tool, documented is a markup annotation, and no members.

@Target (Elementtype.field)

@Retention (Retentionpolicy.runtime)

@Documented

Public @interface column{

Public String name () default "FieldName";

Public String setfuncname () default "SetField";

Public String getfunname () default "GetField";

public boolean defaultdbvalue () default false;

}

@Inherited

@Inherited is a markup annotation that states that a labeled type 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.

@Inherited the annotation type is inherited by subclasses of the class being labeled, the class does not inherit annotation 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 a @inherited annotation, the code check will work: expand Class and its parent class, and know that the specified annotation is found.

@Inherited

Public @interface greeting{

public enum Fontcolor{blue,red,green}

String name ();

FontColor fontcolor () default fontcolor.green;

}

When you use annotation to define annotations, the Java.lang.annotation interface is automatically inherited and other details are automatically completed by the compiler. When defining annotations, you cannot inherit other annotations or interfaces. @interface is used to declare an annotation in which each method represents a configuration parameter declaration. The name of the method is the name of the parameter, the return value type is the type of the parameter, the return value type can only be the base type, Class,string,enum, and the default value of the parameter is declared by default.

Supported data types for annotation parameters:

Basic data types

String type

Class type

Enum type

Annotation type

Arrays of all types above

Parameters in the annotation type

1. You can only use public or default defaults to modify these two access rights.

such as String value () sets the method to default type,

2. Parameter members can only be used with the above type, string value () where the parameter member is a string

3. If there is only one parameter member, set the parameter name to value, followed by parentheses

@Target (Elementtype.field)

@Retention (Retentionpolicy.runtime)

@Documented

Public @interface fruitname{

String value () default "";

}

Fruit Color Annotations

@Target (Elementtype.field)

@Retention (Retentionpolicy.runtime)

@Documented

Public @interface fruitcolor{

Color Enumeration

public enum Color{blue,red,green};

Color Properties

Color fruitcolor () default color.green;

}

Public Class apple{

@FruitName ("Apple")

Private String Applename;

@FruitColor (fruitcolor=color.red)

Private String AppleColor;

public void Setapplecolor (String applecolor) {

This.applecolor = AppleColor;

}

Public String Getapplecolor () {

return applecolor;

}

public void Setapplename (String applename) {

This.applename=applename;

}

Public String Getapplename () {

return applename;

}

public void DisplayName () {

System.out.println ("The name of the fruit is Apple");

}

}

The annotation element must have a definite value, either specified in the definition annotation default value, or specified when using annotations, non-basic type annotation values are not nullable, often using an empty string or 0 as the default value,

1 package annotation; 2  3 import java.lang.annotation.Documented; 4 import java.lang.annotation.ElementType; 5 import Java.lang.annotation.Retention; 6 Import Java.lang.annotation.RetentionPolicy; 7 Import Java.lang.annotation.Target; 8  9/**10  * Fruit Provider Notes  * @author peida12  *13  */14 @Target (Elementtype.field) @Retention ( Retentionpolicy.runtime) @Documented17 public @interface fruitprovider {     /**19      * Supplier number *      @return21      */22 public     int id () default-1;23     /**25      * Supplier name *      @return27      */28     public string name () default "";     /**31      * Supplier Address:      * @return33 */34 public     string Address () Default ""; 35}

Java meta 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.