1 16 17 package org.springframework.aop.support; 18 19 import java.io.Serializable ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.Set ; 23 24 import org.aopalliance.aop.Advice; 25 26 import org.springframework.aop.ClassFilter; 27 import org.springframework.aop.DynamicIntroductionAdvice; 28 import org.springframework.aop.IntroductionAdvisor; 29 import org.springframework.aop.IntroductionInfo; 30 import org.springframework.core.Ordered; 31 import org.springframework.util.Assert; 32 import org.springframework.util.ClassUtils; 33 34 42 public class DefaultIntroductionAdvisor implements IntroductionAdvisor, ClassFilter, Ordered, Serializable { 43 44 private final Advice advice; 45 46 private final Set interfaces = new HashSet (); 47 48 private int order = Integer.MAX_VALUE; 49 50 51 57 public DefaultIntroductionAdvisor(Advice advice) { 58 this(advice, (advice instanceof IntroductionInfo ? (IntroductionInfo) advice : null)); 59 } 60 61 67 public DefaultIntroductionAdvisor(Advice advice, IntroductionInfo introductionInfo) { 68 Assert.notNull(advice, "Advice must not be null"); 69 this.advice = advice; 70 if (introductionInfo != null) { 71 Class [] introducedInterfaces = introductionInfo.getInterfaces(); 72 if (introducedInterfaces.length == 0) { 73 throw new IllegalArgumentException ("IntroductionAdviceSupport implements no interfaces"); 74 } 75 for (int i = 0; i < introducedInterfaces.length; i++) { 76 addInterface(introducedInterfaces[i]); 77 } 78 } 79 } 80 81 86 public DefaultIntroductionAdvisor(DynamicIntroductionAdvice advice, Class intf) { 87 Assert.notNull(advice, "Advice must not be null"); 88 this.advice = advice; 89 addInterface(intf); 90 } 91 92 93 97 public void addInterface(Class intf) { 98 Assert.notNull(intf, "Interface must not be null"); 99 if (!intf.isInterface()) { 100 throw new IllegalArgumentException ("Specified class [" + intf.getName() + "] must be an interface"); 101 } 102 this.interfaces.add(intf); 103 } 104 105 public Class [] getInterfaces() { 106 return (Class []) this.interfaces.toArray(new Class [this.interfaces.size()]); 107 } 108 109 public void validateInterfaces() throws IllegalArgumentException { 110 for (Iterator it = this.interfaces.iterator(); it.hasNext();) { 111 Class ifc = (Class ) it.next(); 112 if (this.advice instanceof DynamicIntroductionAdvice && 113 !((DynamicIntroductionAdvice) this.advice).implementsInterface(ifc)) { 114 throw new IllegalArgumentException ("DynamicIntroductionAdvice [" + this.advice + "] " + 115 "does not implement interface [" + ifc.getName() + "] specified for introduction"); 116 } 117 } 118 } 119 120 121 public void setOrder(int order) { 122 this.order = order; 123 } 124 125 public int getOrder() { 126 return this.order; 127 } 128 129 130 public Advice getAdvice() { 131 return this.advice; 132 } 133 134 public boolean isPerInstance() { 135 return true; 136 } 137 138 public ClassFilter getClassFilter() { 139 return this; 140 } 141 142 public boolean matches(Class clazz) { 143 return true; 144 } 145 146 147 public boolean equals(Object other) { 148 if (this == other) { 149 return true; 150 } 151 if (!(other instanceof DefaultIntroductionAdvisor)) { 152 return false; 153 } 154 DefaultIntroductionAdvisor otherAdvisor = (DefaultIntroductionAdvisor) other; 155 return (this.advice.equals(otherAdvisor.advice) && this.interfaces.equals(otherAdvisor.interfaces)); 156 } 157 158 public int hashCode() { 159 return this.advice.hashCode() * 13 + this.interfaces.hashCode(); 160 } 161 162 public String toString() { 163 return ClassUtils.getShortName(getClass()) + ": advice [" + this.advice + "]; interfaces " + 164 ClassUtils.classNamesToString(this.interfaces); 165 } 166 167 } 168 | Popular Tags |