Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AnnoBinding.java 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import java.lang.annotation.Retention;
  2. import java.lang.annotation.RetentionPolicy;
  3. import org.aspectj.lang.reflect.FieldSignature;
  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. static int field1;
  32. @Marker(message = "string")
  33. static int field2;
  34. public static void runOne() {
  35. field1 = field1 * 2; // set and get jps
  36. }
  37. public static void runTwo() {
  38. field1 = field1 * 2; // set and get jps
  39. }
  40. }
  41. aspect X {
  42. pointcut pManual(): withincode(* runOne(..)) && get(@Marker * *);
  43. pointcut pWoven(Marker l): withincode(* runTwo(..)) && get(@Marker * * ) && @annotation(l);
  44. public static int a,b;
  45. before(): pManual() {
  46. Marker marker = (Marker) ((FieldSignature) thisJoinPointStaticPart.getSignature()).getField().getAnnotation(Marker.class);
  47. String s = marker.message();
  48. a+=s.length();
  49. }
  50. before(Marker l): pWoven(l) {
  51. String s = l.message();
  52. b+=s.length();
  53. }
  54. }