Customizing Java Annotation, reading annotation values

Source: Internet
Author: User

1. The first is custom annotations:

Java code
    1. PACKAGE CN.VEJI.HIBERNATE.PO;  
    2.   
    3. import java.lang.annotation.elementtype;  
    4. import java.lang.annotation.retention;  
    5. span class= "keyword" >import java.lang.annotation.retentionpolicy;  
    6. IMPORT JAVA.LANG.ANNOTATION.TARGET;  
    7.   
    8. @Target (Elementtype.method)   
    9. @Retention ( Retentionpolicy.runtime)   
    10. public @ interface privilege {  
    11.     string[] value ( );   
    12.   
    13. }  





2. define the use of the annotation class:

Java code
  1. Package Cn.veji.hibernate.po;
  2. Package Cn.veji.hibernate.po;
  3. Public class Testprivilege {
  4. @Privilege ({ "a"})
  5. public Void A () {
  6. }
  7. @Privilege ({ "B"})
  8. public void B () {
  9. }
  10. @Privilege ({ "C"})
  11. public void C () {
  12. }
  13. }




3. read the annotation value Help class:

Java code
  1. Package Cn.veji.hibernate.po;
  2. Package cn.veji.hibernate.test;
  3. Import java.lang.annotation.Annotation;
  4. Import Java.lang.reflect.Method;
  5. Import Java.util.HashMap;
  6. Import Java.util.Map;
  7. Import Cn.veji.hibernate.po.Privilege;
  8. Import Cn.veji.hibernate.po.TestPrivilege;
  9. Public class Annotationutil {
  10. public static Annotationutil anno = null;
  11. public static Annotationutil getinstance () {
  12. if (anno = = null) {
  13. Anno = new Annotationutil ();
  14. }
  15. return anno;
  16. }
  17. /** 
  18. * Read annotation values
  19. *
  20. * @param annotationclasss Processing Annotation class name
  21. * @param annotationfield Processing Annotation Class Property names
  22. * @param className processing annotation Use class name
  23. * @return
  24. * @throws Exception
  25. */
  26. @SuppressWarnings ("All")
  27. Public map<string, string> Loadvlaue (Class annotationclasss,
  28. String Annotationfield, String className) throws Exception {
  29. SYSTEM.OUT.PRINTLN ("processing annotation class name = = =" +annotationclasss.getname ());
  30. SYSTEM.OUT.PRINTLN ("Processing annotation class Property name = = =" +annotationfield);
  31. SYSTEM.OUT.PRINTLN ("process annotation call Class name = = =" +classname);
  32. map<string, string> map = new hashmap<string, string> ();
  33. Method[] methods = Class.forName (ClassName). Getdeclaredmethods ();
  34. For (Method method:methods) {
  35. if (method.isannotationpresent (annotationclasss)) {
  36. Annotation p = method.getannotation (ANNOTATIONCLASSS);
  37. Method m = P.getclass ()
  38. . Getdeclaredmethod (Annotationfield, null);
  39. String[] values = (string[]) M.invoke (p, null);
  40. For (String key:values) {
  41. System.out.println ("annotated value = = =" + key);
  42. Map.put (key, key);
  43. }
  44. }
  45. }
  46. SYSTEM.OUT.PRINTLN ("map quantity = = =" + map.size ());
  47. return map;
  48. }
  49. public static void Main (string[] args) throws Exception {
  50. Annotationutil.getinstance (). Loadvlaue (Privilege. Class, "value",
  51. Testprivilege.  class.getname ());
  52. }
  53. }




4. Implementation results:

Java code
      1. Process annotation class name = = = Cn.veji.hibernate.po.Privilege
      2. Handling Annotation Class Property names = = = value
      3. Call class name for processing annotation = = = Cn.veji.hibernate.po.TestPrivilege
      4. Note value = = = C
      5. Note value = = = A
      6. Note value = = = B
      7. Number of maps = = 3

Customizing Java Annotation, reading annotation values

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.