fork(1) download
  1. import java.lang.annotation.*;
  2. import java.util.*;
  3.  
  4. @Inherited
  5. @Repeatable(Container.class)
  6. @Retention(RetentionPolicy.RUNTIME)
  7. @interface Ann {
  8. String value();
  9. }
  10.  
  11. @Inherited
  12. @Retention(RetentionPolicy.RUNTIME)
  13. @interface Container {
  14. Ann[] value();
  15. }
  16.  
  17. // Basic case. Result is that
  18. // only @Ann("2") is present on
  19. // ExhibitASub.
  20. @Ann("1")
  21. class ExhibitASuper {
  22. }
  23. @Ann("2")
  24. class ExhibitASub extends ExhibitASuper {
  25. }
  26.  
  27. // Because this case results in the
  28. // @Container being present on ExhibitBSuper,
  29. // rather than @Ann, all three annotations
  30. // end up appearing on ExhibitBSub.
  31. @Ann("1")
  32. @Ann("2")
  33. class ExhibitBSuper {
  34. }
  35. @Ann("3")
  36. class ExhibitBSub extends ExhibitBSuper {
  37. }
  38.  
  39. // Similar to the preceding case, by
  40. // forcing the use of @Container, both
  41. // annotations are present on ExhibitCSub.
  42. @Container(@Ann("1"))
  43. class ExhibitCSuper {
  44. }
  45. @Ann("2")
  46. class ExhibitCSub extends ExhibitCSuper {
  47. }
  48.  
  49. // Yet when we force both to use @Container,
  50. // only @Container(@Ann("2")) is present on
  51. // ExhibitDSub.
  52. @Container(@Ann("1"))
  53. class ExhibitDSuper {
  54. }
  55. @Container(@Ann("2"))
  56. class ExhibitDSub extends ExhibitDSuper {
  57. }
  58.  
  59. class Main {
  60. public static void main(String[] args) {
  61. for (Class<?> cls : Arrays.asList(ExhibitASub.class,
  62. ExhibitBSub.class,
  63. ExhibitCSub.class,
  64. ExhibitDSub.class)) {
  65. System.out.printf("%s:%n", cls);
  66. for (Annotation ann : cls.getAnnotations()) {
  67. System.out.printf(" %s%n", ann);
  68. }
  69.  
  70. System.out.println();
  71. }
  72. }
  73. }
Success #stdin #stdout 0.07s 4386816KB
stdin
Standard input is empty
stdout
class ExhibitASub:
    @Ann(value=2)

class ExhibitBSub:
    @Container(value=[@Ann(value=1), @Ann(value=2)])
    @Ann(value=3)

class ExhibitCSub:
    @Container(value=[@Ann(value=1)])
    @Ann(value=2)

class ExhibitDSub:
    @Container(value=[@Ann(value=2)])