fork(2) download
  1. import java.lang.annotation.*;
  2. import java.lang.reflect.AnnotatedParameterizedType;
  3. import java.lang.reflect.Method;
  4. import java.util.*;
  5.  
  6. public class Main {
  7. public static void main(String[] args) throws NoSuchMethodException {
  8. Method m = Foo.class.getDeclaredMethod("bar", List.class);
  9.  
  10. var at = m.getAnnotatedParameterTypes()[0];
  11. var ata = ((AnnotatedParameterizedType)at).getAnnotatedActualTypeArguments()[0];
  12.  
  13. // get all annotations
  14. for(var a: ata.getAnnotations()) {
  15. System.out.println(a);
  16. }
  17.  
  18. // or check the presence of a known annotation
  19. System.out.println(ata.getAnnotation(Important.class) != null);
  20. }
  21.  
  22. class Foo {
  23. void bar(List<@Important String> b) {}
  24. }
  25. }
  26.  
  27. @Retention(RetentionPolicy.RUNTIME)
  28. @Target(ElementType.TYPE_USE)
  29. @interface Important {}
  30.  
Success #stdin #stdout 0.09s 34264KB
stdin
Standard input is empty
stdout
@Important()
true