You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Target.java 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import java.lang.annotation.*;
  2. import java.lang.reflect.*;
  3. @Retention(RetentionPolicy.RUNTIME)
  4. @interface Annot1 {}
  5. @Retention(RetentionPolicy.RUNTIME)
  6. @interface Annot2 {}
  7. @Retention(RetentionPolicy.RUNTIME)
  8. @interface Annot3 {}
  9. public class Target {
  10. public static void main(String[] argv) throws Exception {
  11. System.out.println("Field one");
  12. printAnnotations(A.class.getDeclaredField("one"));
  13. printAnnotations(B.class.getDeclaredField("one"));
  14. System.out.println("Field two");
  15. printAnnotations(A.class.getDeclaredField("two"));
  16. printAnnotations(B.class.getDeclaredField("two"));
  17. System.out.println("Field three");
  18. printAnnotations(A.class.getDeclaredField("three"));
  19. printAnnotations(B.class.getDeclaredField("three"));
  20. }
  21. public static void printAnnotations(Field field) {
  22. Annotation[] annos = field.getAnnotations();
  23. if (annos==null || annos.length==0) {
  24. System.out.println("no annotations");
  25. } else {
  26. for (Annotation anno: annos) {
  27. System.out.print(anno+" ");
  28. }
  29. System.out.println();
  30. }
  31. }
  32. }
  33. class A {
  34. public int one;
  35. public String two;
  36. public float three;
  37. }
  38. class B {
  39. public int one;
  40. public String two;
  41. public float three;
  42. }
  43. aspect DeclareAnnot {
  44. declare @field: (int A.one) || (int B.one): @Annot1;
  45. declare @field: (String two) && !(* B.*): @Annot2;
  46. declare @field: !(* one) && !(* two): @Annot3;
  47. }