自定义注解以及反射得到当前的调用方法

矫情吗;* 2022-08-19 11:11 224阅读 0赞

自定义注解以及反射得到当前的调用方法

下面的这个方法很好的,得到现在被调用的方法

StackTraceElement[] stacks = new Exception().getStackTrace();
String methodName = stacks[1].getMethodName();
System.out.println(“methodName: “+ methodName);

Main.java

  1. package com.paincupid.springmvc.application.annotation.currClass;
  2. import com.paincupid.springmvc.test.domain.Person;
  3. public class Main {
  4. public static void main(String[] args) {
  5. PersonObj po = new PersonObj();
  6. po.add(new Person());
  7. }
  8. }

BaseLog.java

  1. package com.paincupid.springmvc.application.annotation.currClass;
  2. import java.lang.annotation.Annotation;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.lang.reflect.Method;
  5. public class BaseLog {
  6. public void logger(){
  7. Class clazz = this.getClass();
  8. String annId = "";
  9. try {
  10. Object obj = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});
  11. Method[] methods = clazz.getDeclaredMethods();
  12. StackTraceElement[] stacks = new Exception().getStackTrace();
  13. String methodName = stacks[1].getMethodName();
  14. System.out.println("methodName: "+ methodName);
  15. for (Method method : methods) {
  16. if(method.isAnnotationPresent(OpLogger.class) && method.getName().equals(methodName)){
  17. OpLogger logger = method.getAnnotation(OpLogger.class);
  18. annId = logger.id();
  19. String clazzName = clazz.getName();
  20. System.out.println("clazzName: "+clazzName + ", methodName: "+methodName +", annId: "+annId);
  21. }
  22. }
  23. } catch (InstantiationException e) {
  24. // TODO Auto-generated catch block
  25. e.printStackTrace();
  26. } catch (IllegalAccessException e) {
  27. // TODO Auto-generated catch block
  28. e.printStackTrace();
  29. } catch (IllegalArgumentException e) {
  30. // TODO Auto-generated catch block
  31. e.printStackTrace();
  32. } catch (InvocationTargetException e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. } catch (NoSuchMethodException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. } catch (SecurityException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. }
  42. }
  43. }

OpLogger.java

  1. package com.paincupid.springmvc.application.annotation.currClass;
  2. import java.lang.annotation.Documented;
  3. import java.lang.annotation.ElementType;
  4. import java.lang.annotation.Retention;
  5. import java.lang.annotation.RetentionPolicy;
  6. import java.lang.annotation.Target;
  7. @Retention(RetentionPolicy.RUNTIME)//注解会在class中存在,运行时可通过反射获取
  8. @Target(ElementType.METHOD)//目标是方法
  9. @Documented//文档生成时,该注解将被包含在javadoc中,可去掉
  10. public @interface OpLogger {
  11. public String id() default "-1";
  12. }

PersonObj.java

  1. package com.paincupid.springmvc.application.annotation.currClass;
  2. import java.util.List;
  3. import com.paincupid.springmvc.test.domain.Person;
  4. import com.paincupid.springmvc.util.BaseJsonRst;
  5. public class PersonObj extends BaseLog{
  6. public BaseJsonRst<List<Person>> search(String id) {
  7. BaseJsonRst<List<Person>> ret = new BaseJsonRst<List<Person>>();
  8. return ret;
  9. }
  10. @OpLogger(id = "123add")
  11. public BaseJsonRst<List<Person>> add(Person person){
  12. BaseJsonRst<List<Person>> ret = new BaseJsonRst<List<Person>>();
  13. logger();
  14. return ret;
  15. }
  16. @OpLogger(id = "456delete")
  17. public BaseJsonRst<List<Person>> delete(Person person){
  18. BaseJsonRst<List<Person>> ret = new BaseJsonRst<List<Person>>();
  19. return ret;
  20. }
  21. @OpLogger(id = "update789")
  22. public BaseJsonRst<List<Person>> updatge(Person person){
  23. BaseJsonRst<List<Person>> ret = new BaseJsonRst<List<Person>>();
  24. return ret;
  25. }
  26. }

Person.java

  1. package com.paincupid.springmvc.test.domain;
  2. import com.paincupid.springmvc.util.Page;
  3. public class Person extends Page{
  4. private String id;
  5. private String name;
  6. private int age;
  7. private String tel;
  8. private String prov;
  9. private String city;
  10. private String town;
  11. private String sex;
  12. private String location;
  13. private String company;
  14. private String comment;
  15. public int getAge() {
  16. return age;
  17. }
  18. public void setAge(int age) {
  19. this.age = age;
  20. }
  21. public String getProv() {
  22. return prov;
  23. }
  24. public void setProv(String prov) {
  25. this.prov = prov;
  26. }
  27. public String getCity() {
  28. return city;
  29. }
  30. public void setCity(String city) {
  31. this.city = city;
  32. }
  33. public String getTown() {
  34. return town;
  35. }
  36. public void setTown(String town) {
  37. this.town = town;
  38. }
  39. public String getSex() {
  40. return sex;
  41. }
  42. public void setSex(String sex) {
  43. this.sex = sex;
  44. }
  45. public String getLocation() {
  46. return location;
  47. }
  48. public void setLocation(String location) {
  49. this.location = location;
  50. }
  51. public String getCompany() {
  52. return company;
  53. }
  54. public void setCompany(String company) {
  55. this.company = company;
  56. }
  57. public String getId() {
  58. return id;
  59. }
  60. public void setId(String id) {
  61. this.id = id;
  62. }
  63. public String getName() {
  64. return name;
  65. }
  66. public void setName(String name) {
  67. this.name = name;
  68. }
  69. public String getComment() {
  70. return comment;
  71. }
  72. public String getTel() {
  73. return tel;
  74. }
  75. public void setTel(String tel) {
  76. this.tel = tel;
  77. }
  78. public void setComment(String comment) {
  79. this.comment = comment;
  80. }
  81. }

BaseJsonRst.java

  1. package com.paincupid.springmvc.util;
  2. import java.io.Serializable;
  3. import java.util.Map;
  4. public class BaseJsonRst<T> extends Page implements Serializable{
  5. private static final long serialVersionUID = 1L;
  6. private boolean success; //成功或失败的标志
  7. private T result; //结果集
  8. private String message; //成功或失败的信息
  9. public boolean isSuccess() {
  10. return success;
  11. }
  12. public void setSuccess(boolean success) {
  13. this.success = success;
  14. }
  15. public T getResult() {
  16. return result;
  17. }
  18. public void setResult(T result) {
  19. this.result = result;
  20. }
  21. public String getMessage() {
  22. return message;
  23. }
  24. public void setMessage(String message) {
  25. this.message = message;
  26. }
  27. public static <T> BaseJsonRst<T> create() {
  28. return new BaseJsonRst<T>();
  29. }
  30. public BaseJsonRst<T> success(T t){
  31. this.setResult(t);
  32. this.setSuccess(true);
  33. return this;
  34. }
  35. public BaseJsonRst<T> fail(String msg){
  36. this.setMessage(msg);
  37. this.setSuccess(false);
  38. this.setTotalCounts(1);
  39. return this;
  40. }
  41. public static <T> BaseJsonRst<T> newSucc(T t){
  42. BaseJsonRst<T> rst = new BaseJsonRst<T>();
  43. rst.setResult(t);
  44. rst.setSuccess(true);
  45. return rst;
  46. }
  47. public static <T> BaseJsonRst<T> newFail(String msg){
  48. BaseJsonRst<T> rst = new BaseJsonRst<T>();
  49. return rst.fail(msg);
  50. }
  51. public BaseJsonRst<T> debug(String di){
  52. return this;
  53. }
  54. }

Page.java

  1. package com.paincupid.springmvc.util;
  2. public class Page {
  3. private static final long serialVersionUID = 1L;
  4. public int totalCounts=0; //设置分页的总条目数
  5. public int pageSize = 20; //设置每一页的条目数, 默认20条数据
  6. public int currentPage = 1; //设置当前的页码
  7. public int getTotalCounts() {
  8. return totalCounts;
  9. }
  10. public void setTotalCounts(int totalCounts) {
  11. this.totalCounts = totalCounts;
  12. }
  13. public int getPageSize() {
  14. return pageSize;
  15. }
  16. public void setPageSize(int pageSize) {
  17. this.pageSize = pageSize;
  18. }
  19. public int getCurrentPage() {
  20. return currentPage;
  21. }
  22. public void setCurrentPage(int currentPage) {
  23. this.currentPage = currentPage;
  24. }
  25. }

下载地址: http://git.oschina.net/paincupid/springmvc

发表评论

表情:
评论列表 (有 0 条评论,224人围观)

还没有评论,来说两句吧...

相关阅读