Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DecA2.java 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. interface Target {
  18. public void m();
  19. }
  20. class A implements Target {
  21. public void m() {
  22. }
  23. }
  24. aspect X {
  25. declare @method: * Target+.*(..): @One;
  26. declare @method: * Target+.*(..): @Two;
  27. }
  28. public class DecA2 {
  29. public static void main(String []argv) {
  30. try {
  31. Class c = Target.class;
  32. Method m = c.getDeclaredMethod("m",null);
  33. System.err.println("There are "+m.getAnnotations().length+" annotations on public void Target.m():");
  34. dumpAnnos(m.getAnnotations());
  35. c = A.class;
  36. m = c.getDeclaredMethod("m",null);
  37. System.err.println("There are "+m.getAnnotations().length+" annotations on public void A.m():");
  38. dumpAnnos(m.getAnnotations());
  39. } catch (Exception e) { e.printStackTrace();}
  40. }
  41. public static void dumpAnnos(Annotation[] anns) {
  42. List l = new ArrayList();
  43. if (anns!=null) {
  44. for (int i = 0; i < anns.length; i++) {
  45. l.add(anns[i].annotationType().getName());
  46. }
  47. }
  48. Collections.sort(l);
  49. int i = 1;
  50. for (Iterator iter = l.iterator(); iter.hasNext();) {
  51. System.err.println((i++)+") "+iter.next());
  52. }
  53. }
  54. }