package com.logicbig.example.method;
import java.lang.annotation.*;
import java.lang.reflect.Method;
import java.util.Arrays;
public class GetDeclaredAnnotationsExample {
@Async
public void process(){}
public static void main(String... args) throws NoSuchMethodException {
Method m = GetDeclaredAnnotationsExample.class.getDeclaredMethod("process");
Annotation[] annotations = m.getDeclaredAnnotations();
System.out.println(Arrays.toString(annotations));
}
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
private @interface Async {}
}
Output
[@com.logicbig.example.method.GetDeclaredAnnotationsExample$Async()]
package com.logicbig.example.method;
import java.lang.annotation.*;
import java.lang.reflect.Method;
import java.util.Arrays;
public class GetDeclaredAnnotationsExample2 {
public static void main(String... args) throws NoSuchMethodException {
Method m = GetDeclaredAnnotationsExample2.Task.class.getDeclaredMethod("process");
Annotation[] annotations = m.getDeclaredAnnotations();
System.out.println(Arrays.toString(annotations));
}
private static class Processor {
@Async
public void process() {}
}
public static class Task extends Processor {
}
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
private @interface Async {}
}
Output
[@com.logicbig.example.method.GetDeclaredAnnotationsExample2$Async()]