Java Annotations Annotation Detailed

Source: Internet
Author: User

Annotations are equivalent to a tag, adding annotations in a program is equivalent to marking the program with some kind of tag, no addition, then there is no tag, later, the Javac compiler, development tools and other programs can use reflection to understand your class and the various elements there is no mark, see what you have tags, to do the corresponding thing.    Tags can be added to packages, classes, fields, methods, method parameters, and local variables. Custom annotations and their Applications 1), define one of the simplest annotations public @interface myannotation {//...}    2), add annotations to a class: @MyAnnotationpublic class annotationtest{//...} The following is a mock case custom annotation @myannotation
1 package com.ljq.test;
2
3 Import Java.lang.annotation.ElementType;
4 Import java.lang.annotation.Retention;
5 Import Java.lang.annotation.RetentionPolicy;
6 Import Java.lang.annotation.Target;
7
8/**
9 * Define an annotation
10 *
11 *
* @author Jiqinlin
13 *
14 */
Four types of meta-annotations are provided in the//java, which are specifically responsible for annotating other annotations, respectively
16
A//@Retention-yuan annotation that indicates the level at which the annotation information needs to be saved (life cycle). The optional retentionpoicy parameters include:
//retentionpolicy.source: Stay in Java source file, compiler is discarded
//retentionpolicy.class: Stays in the CLASS file but is discarded by the VM (default)
//retentionpolicy.runtime: In-memory bytecode, VMS will also retain annotations at run time, so you can read the information of annotations through the reflection mechanism
21st
A//@Target yuan annotation, the default value is any element that indicates where the annotation is used. The available ElementType parameters include
//elementtype.constructor: Constructor declaration
//elementtype.field: Member variables, objects, attributes (including enum instances)
//elementtype.local_variable: local variable declaration
//elementtype.method: Method declaration
//elementtype.package: Package Declaration
//elementtype.parameter: Parameter declaration
//elementtype.type: Classes, interfaces (including annotation types) or enum declarations
30
//@Documented include annotations in Javadoc
32
//@Inheried allow subclasses to inherit annotations from parent class
34
35
@Retention (Retentionpolicy.runtime)
PNs @Target ({elementtype.method, elementtype.type})
@interface Myannotation {
39//Add attributes to annotations
A String of color ();
$ String value () default "I am Lin Yi-chin"; Provide a default value for a property
Int[] Array () default {1, 2, 3};
Gender Gender () default Gender.man; Add an enumeration
Metaannotation metaannotation () Default @MetaAnnotation (birthday= "My date of birth is 1988-2-18");
45//Add enumeration properties
46
47}
Annotation Test class Annotationtest
1 package com.ljq.test;
2
3/**
4 * Annotation Test class
5 *
6 *
7 * @author Jiqinlin
8 *
9 */
10//Invoke annotations and assign values
One @MyAnnotation ([email protected] (Birthday = "My date of birth is 1988-2-18"), color= "Red", Array={23, 26})
public class Annotationtest {
13
public static void Main (string[] args) {
15//Check if class Annotationtest contains @myannotation annotations
if (AnnotationTest.class.isAnnotationPresent (Myannotation.class)) {
17//Get annotations if they exist
Myannotation annotation= (myannotation) AnnotationTest.class.getAnnotation (Myannotation.class);
System.out.println (annotation);
20//Get annotation Properties
System.out.println (Annotation.color ());
System.out.println (Annotation.value ());
23//Array
Int[] Arrs=annotation.array ();
(int arr:arrs) {
System.out.println (arr);
27}
28//Enumeration
Gender Gender=annotation.gender ();
System.out.println ("Gender:" +gender);
31//Get annotation Properties
Metaannotation meta=annotation.metaannotation ();
System.out.println (Meta.birthday ());
34}
35}
36}
Enumeration class gender, adding enumeration properties to mock annotations
1 package com.ljq.test;
2/**
3 * Enumeration, adding enumeration properties to mock annotations
4
5 * @author Jiqinlin
6 *
7 */
8 public enum Gender {
9 man{
Ten public String GetName () {return "male";}
One },
women{
Public String GetName () {return "female";}
+ }; Remember there was ";"
Public abstract String getName ();
16}
Annotation class metaannotation, adding annotation properties to mock annotations
1 package com.ljq.test;

3/**
4 * Define an annotation, add annotation properties to the mock annotation
5
6 * @author Jiqinlin
7 *
8 */
9 Public @interface Metaannotation {
Ten String birthday ();
11}
Transfer from http://www.cnblogs.com/linjiqin/archive/2011/02/16/1956426.html

Java Annotations Annotation Detailed

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.