JAVA annotation example

Source: Internet
Author: User

Annotation (Annotation) is introduced in JDK5.0 and later versions. It can be used to create documents, track code dependencies, and even perform basic compile-time checks. Annotation exists in the Code as '@ annotation name'. According to the number of annotation parameters, we can divide the annotation into three types: Tag annotation, single value annotation, and complete annotation. They do not directly affect the semantics of the program. They only exist as annotations (identifiers). We can program the reflection mechanism to access these metadata. In addition, you can select whether the annotation in the code exists only at the source code level during compilation, or it can also appear in the class file. The annotation syntax is relatively simple. Besides the use of the @ symbol, it is basically consistent with the inherent Syntax of java. java has three built-in annotations, which are defined in the java. lang package. @ Override indicates that the current method overwrites the parent class. @ Deprecated indicates that the current element is not in favor of use. @ SuppressWarnings indicates that some improper compiler warning information is disabled. The following is an example of [java] package com. annotation. test; import java. lang. annotation. documented; import java. lang. annotation. elementType; import java. lang. annotation. inherited; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target;/*** meta annotation @ Target, @ Retention, @ incluented, @ Inherited ** @ Target indicates where the annotation is used. Possible ElemenetType parameters include: * ElemenetTyp E. CONSTRUCTOR declares * ElemenetType. FIELD Declaration (including enum instances) * ElemenetType. LOCAL_VARIABLE local variable Declaration * ElemenetType. METHOD declaration * ElemenetType. PACKAGE Declaration * ElemenetType. PARAMETER Declaration * ElemenetType. TYPE class, interface (including annotation TYPE) or enum declaration ** @ Retention indicates the level at which the annotation information is saved. Optional RetentionPolicy parameters include: * RetentionPolicy. the SOURCE annotation will be discarded by the compiler * RetentionPolicy. the CLASS annotation is available in the class file, but it will be discarded by the VM * RetentionPolicy. the runtime vm also keeps comments at RUNTIME, so the annotation information can be read through the reflection mechanism. ** @ Brief ented include this annotation in javadoc ** @ Inherited allows the subclass to inherit the annotation in the parent class **/@ Target (ElementType. METHOD) @ Retention (RetentionPolicy. RUNTIME) @ incluented @ Inherited/*** definition annotation Test * contains two elements: id and description * description. The default value is "no description" */public @ interface TestAnnotation {public int id (); public String content () default "no content";} The following is an example of [java] package com. annotation. test; import java. lang. reflect. method; public class Test01 {/*** three methods annotated */@ TestAnnotation (id = 1, content = "this is method1") public void method1 () {}@ TestAnnotation (id = 2) public void method2 () {}@ TestAnnotation (id = 3, content = "this is method3") public void method3 () {}/*** parse the annotation and print the information of all annotation methods in the Test01 class */public static void main (String [] args) {Method [] methods = Test01.class. getMethods (); for (Method m: methods) {/*** determines whether the annotation of the specified annotation type exists in the Method */boolean hasAnnotation = m. isAnnotationPresent (TestAnnotation. class); if (hasAnnotation) {TestAnnotation annotation = m. getAnnotation (TestAnnotation. class); System. out. println (m. getName () + "Annotation (id =" + annotation. id () + ", content =" + annotation. content () + ")") ;}}} the output result is as follows: [plain] method1 Annotation (id = 1, content = this is method1) method2 Annotation (id = 2, content = no content) method3 Annotation (id = 3, content = this is method3)

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.