当前位置:   article > 正文

注解解析工具类_注释解析工具

注释解析工具
  1. package anotention;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. /**
  5. * 注解解析的工具类
  6. *
  7. * @author Administrator
  8. *
  9. */
  10. public class AnotentionParseUtils {
  11. /**
  12. * 獲取類,主要先獲取类名
  13. *
  14. * @param className
  15. * @return
  16. * @throws ClassNotFoundException
  17. */
  18. public static Class getClass(String className)
  19. throws ClassNotFoundException {
  20. if (className.equals(null) || className == null) {
  21. return null;
  22. }
  23. return Class.forName(className);
  24. }
  25. /**
  26. * 解析类注解
  27. *
  28. * @throws ClassNotFoundException
  29. */
  30. public static void parseClassAnotention(Class<?> cls)throws ClassNotFoundException {
  31. /**
  32. * 判断是否存在指定的注解类型--这里判断是否存在类注解
  33. */
  34. if (cls.isAnnotationPresent(Anotention.class)) {
  35. Anotention des = (Anotention) cls.getAnnotation(Anotention.class);
  36. System.out.println("解析注解类型值为:" + des.value());
  37. TestAnnotation test = new TestAnnotation();
  38. test.test();
  39. }
  40. }
  41. /**
  42. * 解析方法上的注解
  43. *
  44. * @throws ClassNotFoundException
  45. */
  46. public static void parseMethodAnotention(Class<?> cls) throws ClassNotFoundException {
  47. final Method[] method = cls.getMethods();
  48. for (Method ms : method) {
  49. if (ms.isAnnotationPresent(Anotention.class)) {
  50. Anotention methodDes = (Anotention) ms
  51. .getAnnotation(Anotention.class);
  52. System.out.println("解析方法上的注解:" + methodDes.value());
  53. System.out.println("解析方法上的注解:desc" + methodDes.desc());
  54. }
  55. }
  56. }
  57. /**
  58. * 解析属性注解
  59. *
  60. * @throws ClassNotFoundException
  61. */
  62. public static void parseAttributeAnotention(Class<?> cls)throws ClassNotFoundException {
  63. Field[] fileds = cls.getDeclaredFields();
  64. for (Field f : fileds) {
  65. if (f.isAnnotationPresent(Anotention.class)) {
  66. Anotention methodDes = (Anotention) f
  67. .getAnnotation(Anotention.class);
  68. System.out.println("解析属性上的注解:" + methodDes.value());
  69. System.out.println("解析属性上的注解:desc" + methodDes.desc());
  70. }
  71. }
  72. }
  73. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/240335
推荐阅读
相关标签
  

闽ICP备14008679号