1 package com.tirsen.nanning.samples.prevayler; 2 3 import java.io.Serializable ; 4 import java.lang.reflect.InvocationTargetException ; 5 import java.lang.reflect.Method ; 6 7 import com.tirsen.nanning.Invocation; 8 9 public class Call implements Serializable { 10 static final long serialVersionUID = -3336463259251779539L; 11 12 protected Object target; 13 protected Object [] args; 14 private Object classIdentifier; 15 private Class interfaceClass; 16 private Class [] parameterTypes; 17 private String methodName; 18 19 public Call(Invocation invocation) throws Exception { 20 setInvocation(invocation); 21 } 22 23 public Call() { 24 } 25 26 protected void setInvocation(Invocation invocation) throws Exception { 27 classIdentifier = invocation.getAspectInstance().getClassIdentifier(); 28 interfaceClass = invocation.getTargetInterface(); 29 methodName = invocation.getMethod().getName(); 30 parameterTypes = invocation.getMethod().getParameterTypes(); 31 target = invocation.getProxy(); 32 args = invocation.getArgs(); 33 } 34 35 public Method getMethod() { 36 try { 37 return interfaceClass.getMethod(methodName, parameterTypes); 38 } catch (Exception e) { 39 throw new RuntimeException ("did not find method " + methodName + " returning null", e); 40 } 41 } 42 43 public Class getInterfaceClass() { 44 return interfaceClass; 45 } 46 47 public Object getClassIdentifier() { 48 return classIdentifier; 49 } 50 51 public Object [] getArgs() { 52 return args; 53 } 54 55 public Object getTarget() { 56 return target; 57 } 58 59 public Object invoke() throws Exception { 60 final Method method = getMethod(); 61 final Object target = getTarget(); 62 final Object [] args = getArgs(); 63 64 try { 65 return method.invoke(target, args); 66 } catch (InvocationTargetException e) { 67 if (e.getTargetException() instanceof Exception ) { 68 throw (Exception ) e.getTargetException(); 69 } else if (e.getTargetException() instanceof Error ) { 70 throw (Error ) e.getTargetException(); 71 } else { 72 throw e; 73 } 74 } 75 } 76 } 77 | Popular Tags |