1 7 package com.tirsen.nanning; 8 9 import java.io.Serializable ; 10 import java.lang.reflect.InvocationTargetException ; 11 import java.lang.reflect.Method ; 12 import java.util.*; 13 14 15 23 public final class MixinInstance implements Serializable { 24 static final long serialVersionUID = 7386027290257587762L; 25 26 private Class interfaceClass; 27 private Object target; 28 29 private transient Map methodInterceptors = new HashMap(); 30 31 public MixinInstance() { 32 } 33 34 public MixinInstance(Class interfaceClass, Object target) { 35 setInterfaceClass(interfaceClass); 36 setTarget(target); 37 } 38 39 public void setInterfaceClass(Class interfaceClass) { 40 this.interfaceClass = interfaceClass; 41 } 42 43 public void setTarget(Object target) { 44 this.target = target; 45 } 46 47 public Class getInterfaceClass() { 48 return interfaceClass; 49 } 50 51 public Set getAllInterceptors() { 52 Set allInterceptors = new HashSet(); 53 if (methodInterceptors != null) { 54 for (Iterator methodIterator = methodInterceptors.values().iterator(); methodIterator.hasNext();) { 55 List interceptors = (List) methodIterator.next(); 56 for (Iterator interceptorIterator = interceptors.iterator(); interceptorIterator.hasNext();) { 57 Interceptor interceptor = (Interceptor) interceptorIterator.next(); 58 allInterceptors.add(interceptor); 59 } 60 } 61 } 62 return allInterceptors; 63 } 64 65 public Object getTarget() { 66 return target; 67 } 68 69 public List getInterceptorsForMethod(Method method) { 70 if (methodInterceptors == null) { 71 methodInterceptors = new HashMap(); 72 } 73 List interceptors = (List) methodInterceptors.get(method); 74 if (interceptors == null) { 75 interceptors = new ArrayList(); 76 methodInterceptors.put(method, interceptors); 77 } 78 return interceptors; 79 } 80 81 class InvocationImpl implements Invocation { 82 private Object proxy; 83 private final Method method; 84 private final Object [] args; 85 private ListIterator interceptors; 86 87 public InvocationImpl(Object proxy, Method method, Object [] args) { 88 this.proxy = proxy; 89 this.method = method; 90 this.args = args; 91 interceptors = getInterceptorsForMethod(method).listIterator(); 92 } 93 94 public Object invokeNext() throws Throwable { 95 if (interceptors.hasNext()) { 96 return ((MethodInterceptor) interceptors.next()).invoke(this); 97 } else { 98 try { 99 return method.invoke(getTarget(), args); 100 } catch (InvocationTargetException e) { 101 throwRealException(e); 102 throw e; 103 } 104 } 105 } 106 107 private void throwRealException(InvocationTargetException e) throws Exception { 108 Throwable realException = e.getTargetException(); 109 if (realException instanceof Error ) { 110 throw (Error ) realException; 111 } else if (realException instanceof RuntimeException ) { 112 throw (RuntimeException ) realException; 113 } else { 114 throw (Exception ) realException; 115 } 116 } 117 118 public Interceptor getInterceptor(int index) { 119 return (Interceptor) getInterceptorsForMethod(method).get(index); 120 } 121 122 public Class getTargetInterface() { 123 return getInterfaceClass(); 124 } 125 126 public AspectInstance getAspectInstance() { 127 return Aspects.getAspectInstance(getProxy()); 128 } 129 130 public Object getArg(int arg) { 131 return args[arg]; 132 } 133 134 public Object getTarget() { 135 return MixinInstance.this.getTarget(); 136 } 137 138 public void setTarget(Object target) { 139 MixinInstance.this.setTarget(target); 140 } 141 142 public Object getProxy() { 143 return proxy; 144 } 145 146 public int getCurrentIndex() { 147 return interceptors.previousIndex(); 148 } 149 150 public int getNumberOfInterceptors() { 151 return getInterceptorsForMethod(method).size(); 152 } 153 154 public Method getMethod() { 155 return method; 156 } 157 158 public Object [] getArgs() { 159 return args; 160 } 161 } 162 163 public Object invokeMethod(Object proxy, 164 Method method, 165 Object [] args) 166 throws Throwable { 167 Invocation invocation = new InvocationImpl(proxy, method, args); 168 return invocation.invokeNext(); 169 } 170 171 175 public void addInterceptor(Interceptor interceptor) { 176 assert !(interceptor instanceof ConstructionInterceptor) : "Construction interceptors are added on the aspect instance"; 177 Method [] methods = getAllMethods(); 178 for (int i = 0; i < methods.length; i++) { 179 Method method = methods[i]; 180 if (interceptor instanceof FilterMethodsInterceptor) { 181 FilterMethodsInterceptor filterMethodsInterceptor = (FilterMethodsInterceptor) interceptor; 182 if (filterMethodsInterceptor.interceptsMethod(method)) { 183 addInterceptor(method, (MethodInterceptor) interceptor); 184 } 185 } else { 186 addInterceptor(method, (MethodInterceptor) interceptor); 187 } 188 } 189 } 190 191 public boolean equals(Object o) { 192 if (this == o) return true; 193 if (!(o instanceof MixinInstance)) return false; 194 195 final MixinInstance mixinInstance = (MixinInstance) o; 196 197 if (interfaceClass != null ? !interfaceClass.equals(mixinInstance.interfaceClass) : mixinInstance.interfaceClass != null) return false; 198 if (target != null ? !target.equals(mixinInstance.target) : mixinInstance.target != null) return false; 199 200 return true; 201 } 202 203 public int hashCode() { 204 int result; 205 result = (interfaceClass != null ? interfaceClass.hashCode() : 0); 206 result = 29 * result + (target != null ? target.hashCode() : 0); 207 return result; 208 } 209 210 215 public void addInterceptor(Method method, MethodInterceptor interceptor) { 216 getInterceptorsForMethod(method).add(interceptor); 217 } 218 219 public Method [] getAllMethods() { 220 return interfaceClass.getMethods(); 221 } 222 223 public String toString() { 224 return "mixin{" + getTarget() + "}"; 225 } 226 } 227 | Popular Tags |