1 16 17 package org.springframework.aop.framework; 18 19 import java.lang.reflect.AccessibleObject ; 20 import java.lang.reflect.Method ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.aopalliance.intercept.MethodInterceptor; 26 import org.aopalliance.intercept.MethodInvocation; 27 28 import org.springframework.aop.ProxyMethodInvocation; 29 import org.springframework.aop.support.AopUtils; 30 31 54 public class ReflectiveMethodInvocation implements ProxyMethodInvocation, Cloneable { 55 56 protected final Object proxy; 57 58 protected final Object target; 59 60 protected final Method method; 61 62 protected Object [] arguments; 63 64 private final Class targetClass; 65 66 69 private Map userAttributes; 70 71 75 protected final List interceptorsAndDynamicMethodMatchers; 76 77 81 private int currentInterceptorIndex = -1; 82 83 84 97 public ReflectiveMethodInvocation( 98 Object proxy, Object target, Method method, Object [] arguments, 99 Class targetClass, List interceptorsAndDynamicMethodMatchers) { 100 101 this.proxy = proxy; 102 this.target = target; 103 this.targetClass = targetClass; 104 this.method = method; 105 this.arguments = arguments; 106 this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers; 107 } 108 109 110 public final Object getProxy() { 111 return this.proxy; 112 } 113 114 public final Object getThis() { 115 return this.target; 116 } 117 118 public final AccessibleObject getStaticPart() { 119 return this.method; 120 } 121 122 127 public final Method getMethod() { 128 return this.method; 129 } 130 131 public final Object [] getArguments() { 132 return (this.arguments != null ? this.arguments : new Object [0]); 133 } 134 135 136 public Object proceed() throws Throwable { 137 if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) { 139 return invokeJoinpoint(); 140 } 141 142 Object interceptorOrInterceptionAdvice = 143 this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex); 144 if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) { 145 InterceptorAndDynamicMethodMatcher dm = 148 (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice; 149 if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) { 150 return dm.interceptor.invoke(this); 151 } 152 else { 153 return proceed(); 156 } 157 } 158 else { 159 return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this); 162 } 163 } 164 165 171 protected Object invokeJoinpoint() throws Throwable { 172 return AopUtils.invokeJoinpointUsingReflection(this.target, this.method, this.arguments); 173 } 174 175 176 184 public MethodInvocation invocableClone() { 185 Object [] cloneArguments = null; 186 if (this.arguments != null) { 187 cloneArguments = new Object [this.arguments.length]; 189 System.arraycopy(this.arguments, 0, cloneArguments, 0, this.arguments.length); 190 } 191 return invocableClone(cloneArguments); 192 } 193 194 202 public MethodInvocation invocableClone(Object [] arguments) { 203 try { 204 ReflectiveMethodInvocation clone = (ReflectiveMethodInvocation) clone(); 205 clone.arguments = arguments; 206 return clone; 207 } 208 catch (CloneNotSupportedException ex) { 209 throw new IllegalStateException ( 210 "Should be able to clone object of type [" + getClass() + "]: " + ex); 211 } 212 } 213 214 215 public void setUserAttribute(String key, Object value) { 216 if (value != null) { 217 if (this.userAttributes == null) { 218 this.userAttributes = new HashMap (); 219 } 220 this.userAttributes.put(key, value); 221 } 222 else { 223 if (this.userAttributes != null) { 224 this.userAttributes.remove(key); 225 } 226 } 227 } 228 229 public Object getUserAttribute(String key) { 230 return (this.userAttributes != null ? this.userAttributes.get(key) : null); 231 } 232 233 240 public Map getUserAttributes() { 241 if (this.userAttributes == null) { 242 this.userAttributes = new HashMap (); 243 } 244 return this.userAttributes; 245 } 246 247 248 public String toString() { 249 StringBuffer sb = new StringBuffer ("ReflectiveMethodInvocation: "); 251 sb.append(this.method).append("; "); 252 if (this.target == null) { 253 sb.append("target is null"); 254 } 255 else { 256 sb.append("target is of class [").append(this.target.getClass().getName()).append(']'); 257 } 258 return sb.toString(); 259 } 260 261 } 262 | Popular Tags |