fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.lang.annotation.*;
  6. import java.lang.reflect.*;
  7. import java.io.*;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. scan(new X());
  15. }
  16.  
  17. private static void scan(Object o) throws IllegalAccessException {
  18. if (o == null) return;
  19.  
  20. for (Field field : o.getClass().getDeclaredFields()) {
  21. if (field.getAnnotation(MyAnn.class) != null) {
  22. System.out.println(field.get(o));
  23. }
  24. if (!field.getType().isPrimitive()) {
  25. scan(field.get(o));
  26. }
  27. }
  28. }
  29. }
  30.  
  31. @Retention(RetentionPolicy.RUNTIME)
  32. @Target({ElementType.FIELD})
  33. @interface MyAnn {
  34.  
  35. }
  36.  
  37. class X {
  38. // A custom annotation on `x`.
  39. @MyAnn
  40. int x = 0;
  41.  
  42. Y y = new Y();
  43. }
  44.  
  45. class Y {
  46. @MyAnn
  47. int y = 1;
  48.  
  49. // ...
  50. }
Success #stdin #stdout 0.09s 44680KB
stdin
Standard input is empty
stdout
0
1