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.

PointcutEvaluationExpenseComparator.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * ******************************************************************/
  10. package org.aspectj.weaver.patterns;
  11. import java.util.Comparator;
  12. import org.aspectj.weaver.Shadow;
  13. public class PointcutEvaluationExpenseComparator implements Comparator<Pointcut> {
  14. private static final int MATCHES_NOTHING = -1;
  15. private static final int WITHIN = 1;
  16. private static final int ATWITHIN = 2;
  17. private static final int STATICINIT = 3;
  18. private static final int ADVICEEXECUTION = 4;
  19. private static final int HANDLER = 5;
  20. private static final int GET_OR_SET = 6;
  21. private static final int WITHINCODE = 7;
  22. private static final int ATWITHINCODE = 8;
  23. private static final int EXE_INIT_PREINIT = 9;
  24. private static final int THIS_OR_TARGET = 10;
  25. private static final int CALL = 11;
  26. private static final int ANNOTATION = 12;
  27. private static final int AT_THIS_OR_TARGET = 13;
  28. private static final int ARGS = 14;
  29. private static final int AT_ARGS = 15;
  30. private static final int CFLOW = 16;
  31. private static final int IF = 17;
  32. private static final int OTHER = 20;
  33. /**
  34. * Compare 2 pointcuts based on an estimate of how expensive they may be to evaluate.
  35. *
  36. * within
  37. *
  38. * @within staticinitialization [make sure this has a fast match method] adviceexecution handler get, set withincode
  39. * @withincode execution, initialization, preinitialization call
  40. * @annotation this, target
  41. * @this, @target args
  42. * @args cflow, cflowbelow if
  43. */
  44. public int compare(Pointcut p1, Pointcut p2) {
  45. // important property for a well-defined comparator
  46. if (p1.equals(p2)) {
  47. return 0;
  48. }
  49. int result = getScore(p1) - getScore(p2);
  50. if (result == 0) {
  51. // they have the same evaluation expense, but are not 'equal'
  52. // sort by hashCode
  53. int p1code = p1.hashCode();
  54. int p2code = p2.hashCode();
  55. if (p1code == p2code) {
  56. return 0;
  57. } else if (p1code < p2code) {
  58. return -1;
  59. } else {
  60. return +1;
  61. }
  62. }
  63. return result;
  64. }
  65. // a higher score means a more expensive evaluation
  66. private int getScore(Pointcut p) {
  67. if (p.couldMatchKinds() == Shadow.NO_SHADOW_KINDS_BITS) {
  68. return MATCHES_NOTHING;
  69. }
  70. if (p instanceof WithinPointcut) {
  71. return WITHIN;
  72. }
  73. if (p instanceof WithinAnnotationPointcut) {
  74. return ATWITHIN;
  75. }
  76. if (p instanceof KindedPointcut) {
  77. KindedPointcut kp = (KindedPointcut) p;
  78. Shadow.Kind kind = kp.getKind();
  79. if (kind == Shadow.AdviceExecution) {
  80. return ADVICEEXECUTION;
  81. } else if ((kind == Shadow.ConstructorCall) || (kind == Shadow.MethodCall)) {
  82. return CALL;
  83. } else if ((kind == Shadow.ConstructorExecution) || (kind == Shadow.MethodExecution) || (kind == Shadow.Initialization)
  84. || (kind == Shadow.PreInitialization)) {
  85. return EXE_INIT_PREINIT;
  86. } else if (kind == Shadow.ExceptionHandler) {
  87. return HANDLER;
  88. } else if ((kind == Shadow.FieldGet) || (kind == Shadow.FieldSet)) {
  89. return GET_OR_SET;
  90. } else if (kind == Shadow.StaticInitialization) {
  91. return STATICINIT;
  92. } else {
  93. return OTHER;
  94. }
  95. }
  96. if (p instanceof AnnotationPointcut) {
  97. return ANNOTATION;
  98. }
  99. if (p instanceof ArgsPointcut) {
  100. return ARGS;
  101. }
  102. if (p instanceof ArgsAnnotationPointcut) {
  103. return AT_ARGS;
  104. }
  105. if (p instanceof CflowPointcut || p instanceof ConcreteCflowPointcut) {
  106. return CFLOW;
  107. }
  108. if (p instanceof HandlerPointcut) {
  109. return HANDLER;
  110. }
  111. if (p instanceof IfPointcut) {
  112. return IF;
  113. }
  114. if (p instanceof ThisOrTargetPointcut) {
  115. return THIS_OR_TARGET;
  116. }
  117. if (p instanceof ThisOrTargetAnnotationPointcut) {
  118. return AT_THIS_OR_TARGET;
  119. }
  120. if (p instanceof WithincodePointcut) {
  121. return WITHINCODE;
  122. }
  123. if (p instanceof WithinCodeAnnotationPointcut) {
  124. return ATWITHINCODE;
  125. }
  126. if (p instanceof NotPointcut) {
  127. return getScore(((NotPointcut) p).getNegatedPointcut());
  128. }
  129. if (p instanceof AndPointcut) {
  130. int leftScore = getScore(((AndPointcut) p).getLeft());
  131. int rightScore = getScore(((AndPointcut) p).getRight());
  132. if (leftScore < rightScore) {
  133. return leftScore;
  134. } else {
  135. return rightScore;
  136. }
  137. }
  138. if (p instanceof OrPointcut) {
  139. int leftScore = getScore(((OrPointcut) p).getLeft());
  140. int rightScore = getScore(((OrPointcut) p).getRight());
  141. if (leftScore > rightScore) {
  142. return leftScore;
  143. } else {
  144. return rightScore;
  145. }
  146. }
  147. return OTHER;
  148. }
  149. }