Based on your example, this code works well:
public class TestS1 { public abstract class Client { protected void synchronize() throws NoSuchMethodException {
// note: stackTraceElements[ 2 ]
Method m = this.getClass().getMethod(lastStackElement.getMethodName(), String.class); Cache annotation = m.getAnnotation(Cache.class); System.out.println("Cache.minute = " + annotation.minute());
also you need to tag annotation @Retention(RetentionPolicy.RUNTIME)
@Retention(RetentionPolicy.RUNTIME) @interface Cache { int minute(); } public class OrderClient extends Client { @Cache(minute = 5) public void synchronizrWithCustomerId(String customerId) throws NoSuchMethodException {
Output : Cache.minute = 5
Andremoniy
source share