Java annotation examples

Source: Internet
Author: User

Reprinted from: http://www.iteye.com/topic/400085

 

Annotation provides a formal method for us to use the information in the Code Zhongtian domain.

You can easily use the data at a specific time (using the data through parsing annotations ).

 

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

Annotation, which is defined in the Java. lang package.

@ OverrideIndicates that the current method overwrites the parent class.

@ DeprecatedIt indicates that the current element is not in favor of use.

@ SuppresswarningsIndicates disabling some improper compiler warning information.

 

The following is an example of defining Annotations:

 

Java code
  1. Package test_annotation;
  2. Import java. Lang. annotation. incluented;
  3. Import java. Lang. annotation. inherited;
  4. Import java. Lang. annotation. retention;
  5. Import java. Lang. annotation. target;
  6. Import java. Lang. annotation. elementtype;
  7. Import java. Lang. annotation. retentionpolicy;
  8. /*
  9. * Meta annotation @ target, @ retention, @ incluented, @ inherited
     
  10. *
  11. * @ Target indicates where the annotation is used. Possible elemenettype parameters include:
     
  12. * Elemenettype. constructor Declaration
     
  13. * Elemenettype. Field field Declaration (including Enum instances)
     
  14. * Elemenettype. local_variable local variable Declaration
     
  15. * Elemenettype. Method method declaration
  16. * Elemenettype. Package package Declaration
  17. * Elemenettype. Parameter parameter Declaration
  18. * Elemenettype. Type class, interface (including annotation type) or enum Declaration
     
  19. *
  20. * @ Retention indicates the level at which the annotation information is saved. Optional retentionpolicy parameters include:
     
  21. * The retentionpolicy. Source annotation will be discarded by the compiler.
     
  22. * The retentionpolicy. Class annotation is available in the class file, but is discarded by the VM.
     
  23. * Retentionpolicy. runtime VM also retains annotations during runtime. Therefore, the annotation information can be read through the reflection mechanism.
     
  24. *
  25. * @ Brief ented include this annotation in javadoc
  26. *
  27. * @ Inherited allows subclass to inherit the annotation in the parent class
  28. *
  29. */
  30. @ Target (elementtype. Method)
  31. @ Retention (retentionpolicy. runtime)
  32. @ Brief ented
  33. @ Inherited
  34. /*
  35. * Define annotation Test
  36. * The annotation contains two elements: ID and description.
  37. * The default value of the description element is "no description"
  38. */
  39. Public @ Interface Test {
  40. Public int ID ();
  41. Public String description () Default "no description ";
  42. }
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 annotation @ target, @ retention, @ incluented, @ inherited ** @ target indicates where the annotation is used. Possible elemenettype parameters include: * elemenettype. constructor Declaration * elemenettyp E. field field Declaration (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 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) @ brief ented @ inherited/** definition annotation test * contains two elements: ID and Description * description. The default value is "no description" */Public @ Interface Test {public int ID (); public String description () Default "no description ";}

The following is an example of using annotation and parsing annotation.

 

Java code
  1. Package test_annotation;
  2. Import java. Lang. Reflect. method;
  3. Public class test_1 {
  4. /*
  5. * Three annotated Methods
  6. */
  7. @ Test (ID = 1, description = "Hello method_1 ")
  8. Public void method_1 (){
  9. }
  10. @ Test (ID = 2)
  11. Public void method_2 (){
  12. }
  13. @ Test (ID = 3, description = "last method ")
  14. Public void method_3 (){
  15. }
  16. /*
  17. * Parse the annotation and print the information of all annotation methods of test_1.
  18. */
  19. Public static void main (string [] ARGs ){
  20. Method [] Methods = test_1.class.getdeclaredmethods ();
  21. For (method: Methods ){
  22. /*
  23. * Determine whether there are annotations of the specified annotation type in the Method
  24. */
  25. Boolean hasannotation = method. isannotationpresent (test. Class );
  26. If (hasannotation ){
  27. /*
  28. * Return the specified type annotation of a method based on the annotation type
  29. */
  30. Test annotation = method. getannotation (test. Class );
  31. System. Out. println ("test (method =" + method. getname ()
  32. + ", Id =" + annotation. ID () + ", description ="
  33. + Annotation. Description () + ")");
  34. }
  35. }
  36. }
  37. }
Package test_annotation; import Java. lang. reflect. method; public class test_1 {/** three methods 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 the annotation and print the information of all annotation methods of test_1 class */public static void main (string [] ARGs) {method [] Methods = test_1.class.getdeclaredmethods (); For (method: Methods) {/** determines whether there are annotations for the specified annotation type in the Method */Boolean hasannotation = method. isannotationpresent (test. class); If (hasannotation) {/** specify the type annotation of the method returned Based on the annotation type */test annotation = method. getannotation (test. class); system. out. println ("test (method =" + method. getname () + ", id =" + annotation. ID () + ", description =" + annotation. description () + ")");}}}}

The output result is 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)
Related Article

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.