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.

AnnoBinding.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import org.aspectj.lang.*;
  2. import org.aspectj.lang.reflect.*;
  3. import java.lang.annotation.*;
  4. @Retention(RetentionPolicy.RUNTIME)
  5. @interface Marker {
  6. String message();
  7. }
  8. public class AnnoBinding {
  9. public static void main(String []argv) {
  10. long stime = System.currentTimeMillis();
  11. for (int i=0;i<10000;i++) {
  12. runOne();
  13. }
  14. long etime = System.currentTimeMillis();
  15. long manual = (etime-stime);
  16. stime = System.currentTimeMillis();
  17. for (int i=0;i<10000;i++) {
  18. runTwo();
  19. }
  20. etime = System.currentTimeMillis();
  21. long woven = (etime-stime);
  22. System.out.println("woven="+woven+" manual="+manual);
  23. if (woven>manual) {
  24. throw new RuntimeException("woven="+woven+" manual="+manual);
  25. }
  26. if (X.a!=X.b) {
  27. throw new RuntimeException("a="+X.a+" b="+X.b);
  28. }
  29. }
  30. @Marker(message="string")
  31. public static void runOne() {
  32. }
  33. @Marker(message="string")
  34. public static void runTwo() {
  35. }
  36. static Annotation ajc$anno$1;
  37. }
  38. aspect X {
  39. pointcut pManual(): execution(@Marker * runOne(..));
  40. pointcut pWoven(Marker l): execution(@Marker * runTwo(..)) && @annotation(l);
  41. public static int a,b;
  42. before(): pManual() {
  43. Marker marker = (Marker) ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod().getAnnotation(Marker.class);
  44. String s = marker.message();
  45. a+=s.length();
  46. }
  47. before(Marker l): pWoven(l) {
  48. String s = l.message();
  49. b+=s.length();
  50. }
  51. }
  52. //
  53. //0: invokestatic #96; //Method X.aspectOf:()LX;
  54. //3: getstatic #108; //Field ajc$anno$0:Ljava/lang/Annotation;
  55. //6: dup
  56. //7: ifnonnull 30
  57. //10: ldc #1; //class AnnoBinding
  58. //12: ldc #109; //String runTwo
  59. //14: iconst_0
  60. //15: anewarray #111; //class java/lang/Class
  61. //18: invokevirtual #115; //Method java/lang/Class.getDeclaredMethod:(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Me
  62. //d;
  63. //21: ldc #104; //class Marker
  64. //23: invokevirtual #121; //Method java/lang/reflect/Method.getAnnotation:(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
  65. //26: dup
  66. //27: putstatic #108; //Field ajc$anno$0:Ljava/lang/Annotation;
  67. //30: nop
  68. //31: checkcast #104; //class Marker
  69. //34: invokevirtual #125; //Method X.ajc$before$X$2$ea6844ce:(LMarker;)V
  70. //37: return