Java Annotation mechanism

Source: Internet
Author: User

annotation mechanisms in Java

Annotations (Annotation) provide a formalized way for us to add information to our code, which we can later

It is easy to use this data at some point (by parsing annotations to use the data).

The syntax for annotations is simple, except for the use of the @ symbol, which is basically consistent with Java's inherent syntax, with three built-in Java

Annotations, defined in the Java.lang package.

@Override indicates that the current method is a method that overrides the parent class.

@Deprecated indicates that the current element is deprecated.

@SuppressWarnings means to turn off some inappropriate compiler warning messages.

Here is an example of defining annotations

Java code:
Package test_annotation;

Import java.lang.annotation.Documented;
Import java.lang.annotation.Inherited;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.Target;
Import Java.lang.annotation.ElementType;
Import Java.lang.annotation.RetentionPolicy;

/*
* Meta Annotations @target, @Retention, @Documented, @Inherited
*
* @Target indicates where the annotation is used, the possible elemenettype parameters include:
* Elemenettype.constructor Constructor Declaration
* Elemenettype.field domain declarations (including enum instances)
* elemenettype.local_variable local variable declaration
* Elemenettype.method Method declaration
* Elemenettype.package Package Declaration
* Elemenettype.parameter Parameter declaration
* Elemenettype.type class, interface (including annotation type) or enum declaration
*
* @Retention indicates at what level the annotation information is saved. The optional retentionpolicy parameters include:
* Retentionpolicy.source annotations will be discarded by the compiler
* Retentionpolicy.class annotations are available in the CLASS file, but are discarded by the VM
* Retentionpolicy.runtime VMS will also retain annotations at run time, so you can read the information of annotations through the reflection mechanism.
*
* @Documented include this note in Javadoc
*
* @Inherited allow subclasses to inherit annotations from parent class
*
*/
@Target (Elementtype.method)
@Retention (Retentionpolicy.runtime)
@Documented
@Inherited
/*
* Define Annotation Test
* The annotations contain two element IDs and description
* Description element has default value "no description"
*/
Public @interface Test {
public int id ();
Public String description () default "no description";
}

Here is an example of using annotations and parsing annotations

Java code:
Package test_annotation;

Import Java.lang.reflect.Method;

public class Test_1 {
/*
* Three methods to be annotated
*/
@Test (id = 1, Description = "Hello method_1")
public void Method_1 () {
}

@Test (id = 2)
public void Method_2 () {
}

@Test (id = 3, Description = "Last Method")
public void Method_3 () {
}

/*
* Parse annotations to print the information of all annotated methods of the Test_1 class
*/
public static void Main (string[] args) {
Method[] methods = Test_1.class.getDeclaredMethods ();
for (Method method:methods) {
/*
* Determine if there are annotations of the specified annotation type in the method
*/
Boolean hasannotation = Method.isannotationpresent (Test.class);
if (hasannotation) {
/*
* Returns the specified type annotation of the method according to the annotation type
*/
Test annotation = method.getannotation (Test.class);
System.out.println ("Test (method =" + Method.getname ()
+ ", id =" + annotation.id () + ", Description ="
+ annotation.description () + ")");
}
}
}

}


The output results are as follows:


Test (method = method_1, id = 1, description = Hello method_1)
Test (method = method_2, id = 2, Description = no description)
Test (method = method_3, id = 3, Description = Last method)

Java Annotation mechanism

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.