1 22 package org.jboss.aop.joinpoint; 23 24 import java.io.IOException ; 25 import java.io.ObjectInput ; 26 import java.io.ObjectOutput ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.lang.reflect.Method ; 29 import java.rmi.MarshalledObject ; 30 import java.util.ArrayList ; 31 32 import org.jboss.aop.Advisor; 33 import org.jboss.aop.MethodInfo; 34 import org.jboss.aop.advice.Interceptor; 35 import org.jboss.aop.metadata.SimpleMetaData; 36 37 41 public class MethodInvocation extends InvocationBase implements java.io.Externalizable 42 { 43 static final long serialVersionUID = -1313717554016611763L; 44 47 protected java.lang.Object [] arguments; 48 protected long methodHash; protected MarshalledObject marshalledArguments; 50 protected Method advisedMethod; 51 protected Method unadvisedMethod; 52 53 public String toString() 54 { 55 StringBuffer sb = new StringBuffer (100); 56 sb.append("["); 57 sb.append("advisedMethod=").append(advisedMethod); 58 sb.append(", unadvisedMethod=").append(unadvisedMethod); 59 sb.append(", metadata=").append(metadata); 60 sb.append(", targetObject=").append(targetObject); 61 sb.append(", arguments=").append(arguments); 62 sb.append("]"); 63 return sb.toString(); 64 } 65 66 67 public MethodInvocation(MethodInfo info, org.jboss.aop.advice.Interceptor[] interceptors) 68 { 69 this(interceptors, info.getHash(), info.getAdvisedMethod(), info.getUnadvisedMethod(), info.getAdvisor()); 70 } 71 72 public MethodInvocation(Interceptor[] interceptors, long methodHash, Method advisedMethod, Method unadvisedMethod, Advisor advisor) 73 { 74 super(interceptors); 75 this.advisor = advisor; 76 this.methodHash = methodHash; 77 this.advisedMethod = advisedMethod; 78 this.unadvisedMethod = unadvisedMethod; 79 } 80 81 protected MethodInvocation(org.jboss.aop.advice.Interceptor[] interceptors) 82 { 83 super(interceptors); 84 } 85 86 public MethodInvocation() 87 { 88 } 89 90 95 public Object invokeNext() throws Throwable 96 { 97 if (interceptors != null && currentInterceptor < interceptors.length) 98 { 99 try 100 { 101 return interceptors[currentInterceptor++].invoke(this); 102 } 103 finally 104 { 105 currentInterceptor--; 107 } 108 } 109 110 return invokeTarget(); 111 } 112 113 117 public Object invokeTarget() throws Throwable 118 { 119 try 120 { 121 return getActualMethod().invoke(getTargetObject(), arguments); 122 } 123 catch (Throwable t) 124 { 125 throw handleErrors(getTargetObject(), getMethod(), arguments, t); 126 } 127 } 128 129 130 140 public static Throwable handleErrors(Object target, Method method, Object [] arguments, Throwable t) throws Throwable 141 { 142 if (t instanceof IllegalArgumentException ) 143 { 144 if (target == null) 145 146 throw new IllegalArgumentException ("Null target for method " + method); 147 Class methodClass = method.getClass(); 148 Class targetClass = target.getClass(); 149 if (methodClass.isAssignableFrom(targetClass) == false) 150 throw new IllegalArgumentException ("Wrong target. " + targetClass + " for " + method); 151 ArrayList expected = new ArrayList (); 152 Class [] parameterTypes = method.getParameterTypes(); 153 for (int i = 0; i < parameterTypes.length; ++i) 154 expected.add(parameterTypes[i].getName()); 155 ArrayList actual = new ArrayList (); 156 if (arguments != null) 157 { 158 for (int i = 0; i < arguments.length; ++i) 159 { 160 if (arguments[i] == null) 161 actual.add(null); 162 else 163 actual.add(arguments[i].getClass().getName()); 164 } 165 } 166 throw new IllegalArgumentException ("Wrong arguments. " + method.getName() + " expected=" + expected + " actual=" + actual); 167 } 168 else if (t instanceof InvocationTargetException ) 169 { 170 throw ((InvocationTargetException ) t).getTargetException(); 171 } 172 throw t; 173 } 174 175 178 public Object resolveAnnotation(Class annotation) 179 { 180 Object val = super.resolveAnnotation(annotation); 181 if (val != null) return val; 182 183 if (getAdvisor() != null) 184 { 185 val = getAdvisor().resolveAnnotation(getMethod(), annotation); 186 if (val != null) return val; 187 } 188 189 return null; 190 } 191 192 195 public Object resolveAnnotation(Class [] annotations) 196 { 197 Object val = super.resolveAnnotation(annotations); 198 if (val != null) return val; 199 200 if (getAdvisor() != null) 201 { 202 val = getAdvisor().resolveAnnotation(getMethod(), annotations); 203 if (val != null) return val; 204 } 205 206 return null; 207 } 208 209 218 public Object getMetaData(Object group, Object attr) 219 { 220 Object val = super.getMetaData(group, attr); 221 if (val != null) return val; 222 223 if (getAdvisor() != null) 224 { 225 val = getAdvisor().getMethodMetaData().resolve(this, group, attr); 226 if (val != null) return val; 227 } 228 229 if (getAdvisor() != null) 230 { 231 val = getAdvisor().getDefaultMetaData().resolve(this, group, attr); 232 if (val != null) return val; 233 } 234 235 return null; 236 } 237 238 247 public Invocation getWrapper(Interceptor[] newchain) 248 { 249 MethodInvocationWrapper wrapper = new MethodInvocationWrapper(this, newchain); 250 return wrapper; 251 } 252 253 258 public Invocation copy() 259 { 260 MethodInvocation wrapper = new MethodInvocation(interceptors, methodHash, advisedMethod, unadvisedMethod, advisor); 261 wrapper.metadata = this.metadata; 262 wrapper.currentInterceptor = this.currentInterceptor; 263 wrapper.instanceResolver = this.instanceResolver; 264 wrapper.setTargetObject(this.getTargetObject()); 265 wrapper.setArguments(this.getArguments()); 266 return wrapper; 267 } 268 269 270 public Object [] getArguments() 271 { 272 if (arguments == null && marshalledArguments != null) 273 { 274 try 275 { 276 arguments = (Object []) marshalledArguments.get(); 277 marshalledArguments = null; 278 } 279 catch (IOException e) 280 { 281 throw new RuntimeException (e); 282 } 283 catch (ClassNotFoundException e) 284 { 285 throw new RuntimeException (e); 286 } 287 } 288 return arguments; 289 } 290 291 292 public void setArguments(Object [] arguments) 293 { 294 this.arguments = arguments; 295 } 296 297 public Method getMethod() 298 { 299 return advisedMethod; 300 } 301 302 public Method getActualMethod() 303 { 304 return unadvisedMethod; 305 } 306 307 public long getMethodHash() 308 { 309 return methodHash; 310 } 311 312 public Advisor getAdvisor() 313 { 314 return advisor; 315 } 316 317 318 public void writeExternal(ObjectOutput out) throws IOException 319 { 320 out.writeLong(methodHash); 321 if (getArguments() == null) 322 { 323 out.writeObject(null); 324 } 325 else 326 { 327 MarshalledObject mo = new MarshalledObject (getArguments()); 328 out.writeObject(mo); 329 } 330 out.writeObject(metadata); 331 } 332 333 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 334 { 335 methodHash = in.readLong(); 336 marshalledArguments = (MarshalledObject ) in.readObject(); 337 metadata = (SimpleMetaData) in.readObject(); 338 } 339 340 } 341 | Popular Tags |