Annotation (annotation) of Java high-tech)

Source: Internet
Author: User

1. What is annotation?

Annotation is equivalent to a flag. Adding an annotation in the program is equivalent to marking the program. If it is not added, it means there is no mark. Later, the javac compiler, development tools and other programs can use reflection to understand whether there are any tags on your classes and various elements. If you have any tags, you can do the relevant work. The tag can be added to the package, class, field, method, method parameters, and local variables.

Check the java. lang package to see the most basic annotation provided in JDK.

@ Suppresswarnings // eliminate the warning

@ Deprecated // It is not recommended or outdated.

@ Override // method rewriting note public Boolean equals (objet other) if the parameter is not of the object type, this is a method overload! Don't bother.

II. Application structure of Annotation

Annotation is equivalent to a class to be called in your source program. To apply an annotation to the source program, you must prepare the annotation class. Just as if you want to call a class, you must first develop this class.

3. Custom Annotation

1. Define a simplest annotation: Public @ interface myannotation {}

  • Add it to a class: @ myannotation public class annotationtest {}
  • Use reflection to test whether @ myannotation exists in the definition of annotationtest
  • Based on the issue of the launch test, the meta annotation @ retention meta annotation has three values: retetionpolicy. source, retetionpolicy. class, retetionpolicy. runtime, which corresponds to the bytecode in the memory of the Java source File> class file.

2. Thinking: What are the attribute values of @ override, @ suppresswarnings, and @ deprecated annotations?

[Retetionpolicy. Source, retetionpolicy. Source, retetionpolicy. runtime]
3. @ target meta Annotation
The default value of target is any element, and the value of target is set to elementtype. method. If an error is reported for the annotation added to the class, set {elementtype in array mode. method, elementtype. type.
You do not need to remember the meta annotation and Its enumerated attribute values. You only need to refer to the definition of the API help document provided by JDK or its source code, as shown in the following figure, or directly refer to Java. lang. class under the annotation package.

3. Add attributes to the annotation. 1. What is the annotation attribute? annotation is an identifier. The annotation attribute is the content of the annotation identifier.
Added the property Tag: @ myannotation (color = "red ")
Define the attributes and application attributes of the basic type:
  • Add string color () to the annotation class ();
  • @ Myannotation (color = "red ")

2. Obtain the instance object corresponding to the annotation using the reflection method, and then call the method corresponding to the attribute through this object.
Myannotation A = (myannotation) annotationtest. Class. getannotation (myannotation. Class );
System. Out. println (A. color ());
3. The above @ myannotation is an instance object of the myannotaion class.
Specify the default value for the property:

  • String color () Default "yellow ";
  • Value Attribute: string value () Default "zxx ";

4. if there is an attribute named value in the annotation and you only want to set the value attribute (that is, all other attributes use the default value or you only have one value attribute), you can omit the value = Section. For example: @ myannotation ("lhm "). Note: enumeration and annotation are special classes. You cannot use new to create their instance objects. The instance objects that create enumeration are added to them. How can I create an annotation instance object in a program? Annotate the Code:

Public @ interface metaannotation // This is an annotation class {string nextday ();}

Import Java. lang. annotation. elementtype; import Java. lang. annotation. retention; import Java. lang. annotation. retentionpolicy; import Java. lang. annotation. target; @ retention (retentionpolicy. runtime) // keep it to the runtime -- this is a meta annotation, annotation @ target ({elementtype. method, elementtype. type}) // see the help documentation --> elementtypepublic @ interface myannotation // This is an annotation class {string color () Default "Bule "; // The default value is public abstract string value (); int [] arrayattr () default {1, 2, 3}; // class getcls () default string of the annotation type of the array attribute. class; // The annotation type enumtest of the bytecode attribute. trafficlamp () default enumtest. trafficlamp. green; // annotation type of the enumeration attribute metaannotation annotationattr () default @ metaannotation (nextday = "January 1"); // annotation type of the annotation attribute}

Import Java. util. arrays; @ myannotation (color = "red", value = "3", arrayattr = {6, 7, 8}, annotationattr = @ metaannotation (nextday = "January 15 ")) // This class applies the annotation class myannotationpublic class annotationtest {@ suppresswarnings ("deprecation") // cancels display of the specified compiler warning @ myannotation ("Java") // only one property value, value = can be omitted without writing public static void main (string [] ARGs) throws exception {// @ suppresswarnings ("deprecation") with annotations, the compiler will not warn system. runfinalizersonexit (True); // This is an outdated method if (annotationtest. class. isannotationpresent (myannotation. class) {// obtain the annotationtest class annotation myannotation annotation = (myannotation) annotationtest Using Reflection. class. getannotation (myannotation. class); system. out. println (annotation. color (); system. out. println (annotation. value (); system. out. println (arrays. tostring (annotation. arrayattr (); system. out. println (annotation. lamp (); system. out. println (Notation. annotationattr (). nextday ();} // obtain the annotation myannotation annotation1 = (myannotation) annotationtest of the main method using reflection. class. getmethod ("Main", string []. class ). getannotation (myannotation. class); system. out. println (annotation1.color (); // The default value is bluesystem. out. println (annotation1.value (); system. out. println (arrays. tostring (annotation1.arrayattr (); system. out. println (annotation1.annotationattr (). nextday ();} @ Depre Cated public static void say () {system. Out. println ("Hello world! ");}}

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.