Usage of Java annotation @interface

Source: Internet
Author: User
Tags deprecated

Java uses @interface annotation{} to define an annotation @Annotation, and one annotation is a class.
@Override, @Deprecated, @SuppressWarnings 3 common annotations.
Annotations are equivalent to a mark, and adding annotations to a program is tantamount to adding some kind of markup to the program,
Javac compilers, development tools and other programs can use reflection to understand your class and the various elements have no markup, see what you have the mark, go to do the corresponding thing.

Note @override is used on methods, when we want to rewrite a method, add @override to the method, when we approach
When the name is wrong, the compiler will error


Annotation @deprecated, which is used to indicate that a class's properties or methods are obsolete and do not want others to use in properties and methods
Modified with @deprecated,

Note @suppresswarnings is used to suppress warnings from the program, such as when no generics are used or the method is obsolete.

Annotation @retention can be used to modify annotations, which are annotations, called meta-annotations.
The retention annotation has an attribute value, which is of type Retentionpolicy, and enum Retentionpolicy is an enumeration type,
This enumeration determines how retention annotations should be maintained, and can also be understood as rentention with Rententionpolicy. Retentionpolicy has 3 values:CLASS RUNTIME SOURCE
Annotated with @retention (Retentionpolicy.class), which indicates that the information of the annotations is kept in the CLASS file (bytecode file) when the program is compiled, but is not read by the virtual machine at run time;
@retention (Retentionpolicy.source) modified annotations, indicating that the information of the annotations will be discarded by the compiler, will not be left in the class file, the information will only remain in the source file;
Annotated with @retention (Retentionpolicy.runtime), indicating that the information of the annotations is retained in the class file (bytecode file) when the program compiles, the virtual machine is kept at runtime,
So they can be read in a reflective way. Retentionpolicy.runtime allows you to read the annotation annotation information from the JVM so that it can be used when analyzing the program.

    1. Package com.self;
    2. Import java.lang.annotation.Retention;
    3. Import Java.lang.annotation.RetentionPolicy;
    4. @Retention (Retentionpolicy.runtime)
    5. Public @interface Mytarget
    6. { }
    7. Define an annotated @MyTarget and modify it with Retentionpolicy.runtime;
    8. Package com.self;
    9. Import Java.lang.reflect.Method;
    10. Public class Mytargettest
    11. {
    12. @MyTarget
    13. public void DoSomething ()
    14. {
    15. System.out.println ("Hello World");
    16. }
    17. public static void Main (string[] args) throws Exception
    18. {
    19. method = Mytargettest.  Class.getmethod ("dosomething",null);
    20. if (method.isannotationpresent (mytarget. Class)//True if annotation @mytarget is present on the DoSomething method
    21. {
    22. System.out.println (Method.getannotation (mytarget.   Class));
    23. }
    24. }
    25. }
    26. The above program prints:@com. Self.mytarget () and does not print if the Retentionpolicy value is not runtime.
    27. @Retention (Retentionpolicy.source)
    28. Public @interface Override
    29. @Retention (Retentionpolicy.source)
    30. Public @interface Suppresswarnings
    31. @Retention (Retentionpolicy.runtime)
    32. Public @interface Deprecated
    33. As can be seen, only annotations @Deprecated can be read by the JVM at run time
    34. You can define attributes in the annotations to see examples:
    35. @Retention (Retentionpolicy.runtime)
    36. Public @interface Myannotation
    37. {
    38. String Hello () default "Gege";
    39. String World ();
    40. int[] Array () default { 2, 4, 5, 6};
    41. Enumtest.trafficlamp lamp ();
    42. Testannotation lannotation () default @TestAnnotation (value = "ddd");
    43. Class style () default String.   Class
    44. }
    45. In the above program, define an annotation @MyAnnotation, define 6 attributes, their name is:
    46. Hello,world,array,lamp,lannotation,style.
    47. Property Hello type is string, default value is Gege
    48. Property world Type is string, no default value
    49. Property array type is an array with default values of 2,4,5,6
    50. Attribute lamp Type is an enumeration with no default value
    51. The attribute lannotation type is annotated, the default value is @TestAnnotation, and the attribute in the annotation is the annotation
    52. Property style type is class, default value is String type class type
    53. Take a look at the following example: Defines a mytest class, with annotations @MyAnnotation adornments, annotations @MyAnnotation defined properties are assigned values
    54. @MyAnnotation (hello = "Beijing", world="Shanghai", array={},lamp=trafficlamp.red,style=Int.Class)
    55. Public class MyTest
    56. {
    57. @MyAnnotation (lannotation=@TestAnnotation (value="Baby"), world = "Shanghai", array={1, 2,3},lamp=trafficlamp.yellow)
    58. @Deprecated
    59. @SuppressWarnings ("")
    60. public void output ()
    61. {
    62. System.out.println ("Output something!");
    63. }
    64. }
    65. The information of the annotations is then read by reflection:
    66. Public class Myreflection
    67. {
    68. public static void Main (string[] args) throws Exception
    69. {
    70. MyTest MyTest = new MyTest ();
    71. class<mytest> C = MyTest.  class;
    72. method = C.getmethod ("Output", new class[] {});
    73. true if the MyTest class name has annotations @myannotation adornments
    74. if (MyTest. Class.isannotationpresent (myannotation. Class))
    75. {
    76. System.out.println ("have annotation");
    77. }
    78. if (method.isannotationpresent (myannotation. Class))
    79. {
    80. Method.invoke (myTest, null); //Call the output method
    81. //Get information on method annotations @myannotation
    82. Myannotation myannotation = method.getannotation (myannotation.   Class);
    83. String Hello = Myannotation.hello ();
    84. String world = Myannotation.world ();
    85. System.out.println (hello + "," + World); Print properties Hello and world values
    86. System.out.println (Myannotation.array (). length); //Print Properties The length of array arrays
    87. System.out.println (Myannotation.lannotation (). value ()); //Print the value of the attribute Lannotation
    88. System.out.println (Myannotation.style ());
    89. }
    90. //Get all annotations on the output method, of course, are retentionpolicy.runtime modified
    91. annotation[] annotations = method.getannotations ();
    92. For (Annotation annotation:annotations)
    93. {
    94. System.out.println (Annotation.annotationtype (). GetName ());
    95. }
    96. }
    97. }
    98. The above program prints:
    99. have annotation
    100. Output something!
    101. Gege, Shanghai
    102. 3
    103. Baby
    104. Class Java.lang.String
    105. Com.heima.annotation.MyAnnotation
    106. java.lang.Deprecated
    107. If there is an attribute name called value in the annotation, you can omit the property name from being applied.
    108. Visible,in the @Retention (retentionpolicy.runtime) annotation, retentionpolicy.runtime is the annotation attribute value, the property name is value,
    109. The return type of the property is Retentionpolicy, as follows:
    110. Public @interface Mytarget
    111. {
    112. String value ();
    113. }
    114. This can be used:
    115. @MyTarget ("AAA")
    116. public void DoSomething ()
    117. {
    118. System.out.println ("Hello World");
    119. }
    120. Annotation @Target is also a meta-annotation used to modify annotations, which has an attribute ElementType is also an enumeration type.
    121. Value: Annotation_type CONSTRUCTOR FIELD local_variable METHOD Package PARAMETER TYPE
    122. An annotated note such as @Target (Elementtype.method) indicates that the annotation can only be used to decorate the method.
    123. @Target (Elementtype.method)
    124. @Retention (Retentionpolicy.runtime)
    125. Public @interface Mytarget
    126. {
    127. String value () default "hahaha";
    128. }
    129. If the @MyTarget modified on the class, then the program error, such as:
    130. @MyTarget
    131. Public class Mytargettest

Usage of Java annotation @interface

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.