1 16 17 package org.springframework.aop.aspectj.autoproxy; 18 19 import java.util.Comparator ; 20 21 import org.springframework.aop.Advisor; 22 import org.springframework.aop.aspectj.AspectJAopUtils; 23 import org.springframework.aop.aspectj.AspectJPrecedenceInformation; 24 import org.springframework.core.OrderComparator; 25 import org.springframework.util.Assert; 26 27 51 class AspectJPrecedenceComparator implements Comparator { 52 53 private static final int HIGHER_PRECEDENCE = -1; 54 private static final int SAME_PRECEDENCE = 0; 55 private static final int LOWER_PRECEDENCE = 1; 56 private static final int NOT_COMPARABLE = 0; 57 58 private final Comparator advisorComparator; 59 60 61 64 public AspectJPrecedenceComparator() { 65 this.advisorComparator = new OrderComparator(); 66 } 67 68 73 public AspectJPrecedenceComparator(Comparator advisorComparator) { 74 Assert.notNull(advisorComparator, "Advisor comparator must not be null"); 75 this.advisorComparator = advisorComparator; 76 } 77 78 79 public int compare(Object o1, Object o2) { 80 if (!(o1 instanceof Advisor && o2 instanceof Advisor)) { 81 throw new IllegalArgumentException ( 82 "AspectJPrecedenceComparator can only compare the order of Advisors, " + 83 "but was passed [" + o1 + "] and [" + o2 + "]"); 84 } 85 86 Advisor advisor1 = (Advisor) o1; 87 Advisor advisor2 = (Advisor) o2; 88 89 boolean oneOrOtherIsAfterAdvice = 90 (AspectJAopUtils.isAfterAdvice(advisor1) || AspectJAopUtils.isAfterAdvice(advisor2)); 91 boolean oneOrOtherIsBeforeAdvice = 92 (AspectJAopUtils.isBeforeAdvice(advisor1) || AspectJAopUtils.isBeforeAdvice(advisor2)); 93 if (oneOrOtherIsAfterAdvice && oneOrOtherIsBeforeAdvice) { 94 return NOT_COMPARABLE; 95 } 96 else { 97 int advisorPrecedence = this.advisorComparator.compare(advisor1, advisor2); 98 if (advisorPrecedence == SAME_PRECEDENCE && declaredInSameAspect(advisor1, advisor2)) { 99 advisorPrecedence = comparePrecedenceWithinAspect(advisor1, advisor2); 100 } 101 return advisorPrecedence; 102 } 103 } 104 105 private int comparePrecedenceWithinAspect(Advisor advisor1, Advisor advisor2) { 106 boolean oneOrOtherIsAfterAdvice = 107 (AspectJAopUtils.isAfterAdvice(advisor1) || AspectJAopUtils.isAfterAdvice(advisor2)); 108 int adviceDeclarationOrderDelta = getAspectDeclarationOrder(advisor1) - getAspectDeclarationOrder(advisor2); 109 110 if (oneOrOtherIsAfterAdvice) { 111 if (adviceDeclarationOrderDelta < 0) { 113 return LOWER_PRECEDENCE; 116 } 117 else if (adviceDeclarationOrderDelta == 0) { 118 return SAME_PRECEDENCE; 119 } 120 else { 121 return HIGHER_PRECEDENCE; 122 } 123 } 124 else { 125 if (adviceDeclarationOrderDelta < 0) { 127 return HIGHER_PRECEDENCE; 130 } 131 else if (adviceDeclarationOrderDelta == 0) { 132 return SAME_PRECEDENCE; 133 } 134 else { 135 return LOWER_PRECEDENCE; 136 } 137 } 138 } 139 140 private boolean declaredInSameAspect(Advisor advisor1, Advisor advisor2) { 141 if (!(hasAspectName(advisor1) && hasAspectName(advisor2))) { 142 return false; 143 } 144 else { 145 return getAspectName(advisor1).equals(getAspectName(advisor2)); 146 } 147 } 148 149 private boolean hasAspectName(Advisor anAdvisor) { 150 return (anAdvisor instanceof AspectJPrecedenceInformation || 151 anAdvisor.getAdvice() instanceof AspectJPrecedenceInformation); 152 } 153 154 private String getAspectName(Advisor anAdvisor) { 156 return AspectJAopUtils.getAspectJPrecedenceInformationFor(anAdvisor).getAspectName(); 157 } 158 159 private int getAspectDeclarationOrder(Advisor anAdvisor) { 160 AspectJPrecedenceInformation precedenceInfo = 161 AspectJAopUtils.getAspectJPrecedenceInformationFor(anAdvisor); 162 if (precedenceInfo != null) { 163 return precedenceInfo.getDeclarationOrder(); 164 } 165 else { 166 return 0; 167 } 168 } 169 170 } 171 | Popular Tags |