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.

DecA.java 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import java.lang.annotation.Annotation;
  2. import java.lang.annotation.Retention;
  3. import java.lang.annotation.RetentionPolicy;
  4. import java.lang.reflect.Constructor;
  5. import java.lang.reflect.Field;
  6. import java.lang.reflect.Method;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Iterator;
  10. import java.util.List;
  11. @Retention(RetentionPolicy.RUNTIME) @interface One {}
  12. @Retention(RetentionPolicy.RUNTIME) @interface Two {}
  13. @Retention(RetentionPolicy.RUNTIME) @interface Three {}
  14. @Retention(RetentionPolicy.RUNTIME) @interface Four {}
  15. @Retention(RetentionPolicy.RUNTIME) @interface Five {}
  16. @Retention(RetentionPolicy.RUNTIME) @interface Six {}
  17. class Target {
  18. public void m() {}
  19. public Target(int i) {}
  20. public int x;
  21. }
  22. aspect X {
  23. declare @method: * Target.*(..): @One;
  24. declare @method: * Target.*(..): @Two;
  25. declare @constructor: Target.new(int): @Three;
  26. declare @constructor: Target.new(int): @Four;
  27. declare @field: int Target.*: @Five;
  28. declare @field: int Target.*: @Six;
  29. }
  30. public class DecA {
  31. public static void main(String []argv) {
  32. // Let's do the method then the ctor, then the field
  33. try {
  34. Class c = Target.class;
  35. Method m = c.getDeclaredMethod("m",null);
  36. System.err.println("There are "+m.getAnnotations().length+" annotations on public void m():");
  37. dumpAnnos(m.getAnnotations());
  38. Constructor ctor = c.getDeclaredConstructor(new Class[]{Integer.TYPE});
  39. System.err.println("There are "+ctor.getAnnotations().length+" annotations on public Target(int):");
  40. dumpAnnos(ctor.getAnnotations());
  41. Field f = c.getDeclaredField("x");
  42. System.err.println("There are "+f.getAnnotations().length+" annotations on public int x:");
  43. dumpAnnos(f.getAnnotations());
  44. } catch (Exception e) { e.printStackTrace();}
  45. }
  46. public static void dumpAnnos(Annotation[] anns) {
  47. List l = new ArrayList();
  48. if (anns!=null) {
  49. for (int i = 0; i < anns.length; i++) {
  50. l.add(anns[i].annotationType().getName());
  51. }
  52. }
  53. Collections.sort(l);
  54. int i = 1;
  55. for (Iterator iter = l.iterator(); iter.hasNext();) {
  56. System.err.println((i++)+") "+iter.next());
  57. }
  58. }
  59. }