我有一个名为 Client 的抽象类。如何访问在子类的调用方法上声明的注释?处理这个问题的最佳方法是什么?
public abstract class Client {
protected void synchronize() {
// How can I get the Annotation defined on inheriting class?
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
StackTraceElement lastStackElement = stackTraceElements[stackTraceElements.length-1] ;
Method m = this.getClass().getMethod(lastStackElement.getMethodName(), String.class, int.class);
m.getAnnotation(Cache.class);
// synchronize data from server
}
}
.
public class OrderClient extends Client {
@Cache(minute = 5)
public void synchronizrWithCustomerId(String customerId) {
// So some stuff setup body and header
super.synchronize();
}
}