1 package dynaop; 2 3 import java.lang.reflect.Method ; 4 5 import net.sf.cglib.MethodProxy; 6 7 import dynaop.util.ArrayObject; 8 import dynaop.util.Classes; 9 10 14 class InvocationImpl implements Invocation { 15 16 Method method; 17 Object [] arguments; 18 Proxy proxy; 19 Object target; 20 Interceptor[] interceptors; 21 int interceptorCount; 22 int index = -1; 23 boolean classProxy; 24 25 public Object proceed() throws Throwable { 26 try { 27 this.index++; 28 29 if (index < interceptorCount) 31 return interceptors[index].intercept(this); 32 33 if (index > interceptorCount) 35 throw new IllegalStateException ("Proceeded too far."); 36 37 return invokeTarget(proxy, target, method, arguments, classProxy); 38 } 39 finally { 40 this.index--; 41 } 42 } 43 44 static Object invokeTarget(Object proxy, Object target, Method method, 45 Object [] arguments, boolean classProxy) throws Throwable { 46 if (classProxy) 47 return ((MethodProxy) target).invokeSuper(proxy, arguments); 48 49 return Classes.invoke(target, method, arguments); 51 } 52 53 public Object [] getArguments() { 54 return arguments; 55 } 56 57 public Method getMethod() { 58 return method; 59 } 60 61 public Object getTarget() { 62 return target; 63 } 64 65 public Proxy getProxy() { 66 return this.proxy; 67 } 68 69 void setProxy(Proxy proxy) { 70 this.proxy = proxy; 71 } 72 73 void setArguments(Object [] objects) { 74 arguments = objects; 75 } 76 77 void setMethod(Method method) { 78 this.method = method; 79 } 80 81 void setTarget(Object object) { 82 target = object; 83 } 84 85 void setInterceptors(Interceptor[] interceptors) { 86 this.interceptors = interceptors; 87 this.interceptorCount = this.interceptors.length; 88 } 89 90 void clearInterceptors() { 91 this.interceptors = null; 92 } 93 94 public String toString() { 95 StringBuffer buffer = new StringBuffer (); 96 97 buffer.append("dynaop.Invocation@"); 98 buffer.append(Integer.toHexString(System.identityHashCode(this))); 99 buffer.append("["); 100 101 buffer.append("target: "); 102 buffer.append(target); 103 buffer.append(", "); 104 105 buffer.append("method: "); 106 buffer.append(method); 107 buffer.append(", "); 108 109 buffer.append("arguments: "); 110 buffer.append((arguments == null) ? null : 111 new ArrayObject(arguments)); 112 buffer.append(", "); 113 114 buffer.append("interceptors: "); 115 buffer.append((interceptors == null) ? null : 116 new ArrayObject(interceptors)); 117 buffer.append(", "); 118 119 buffer.append("index: "); 120 buffer.append(index); 121 buffer.append(", "); 122 123 buffer.append("]"); 124 125 return buffer.toString(); 126 } 127 128 void setClassProxy(boolean isClassProxy) { 129 this.classProxy = isClassProxy; 130 } 131 } 132 | Popular Tags |