1 22 package org.jboss.aop.advice; 23 24 import org.jboss.aop.instrument.Untransformable; 25 import org.jboss.aop.joinpoint.ConstructorCalledByConstructorInvocation; 26 import org.jboss.aop.joinpoint.ConstructorCalledByMethodInvocation; 27 import org.jboss.aop.joinpoint.ConstructorInvocation; 28 import org.jboss.aop.joinpoint.FieldInvocation; 29 import org.jboss.aop.joinpoint.FieldReadInvocation; 30 import org.jboss.aop.joinpoint.FieldWriteInvocation; 31 import org.jboss.aop.joinpoint.Invocation; 32 import org.jboss.aop.joinpoint.MethodCalledByConstructorInvocation; 33 import org.jboss.aop.joinpoint.MethodCalledByMethodInvocation; 34 import org.jboss.aop.joinpoint.MethodInvocation; 35 36 import java.lang.reflect.Method ; 37 38 44 public abstract class AbstractAdvice implements Interceptor, Untransformable 45 { 46 private static final Class [] INVOCATION_SIGNATURE = {Invocation.class}; 47 private static final Class [] METHOD_SIGNATURE = {MethodInvocation.class}; 48 private static final Class [] CONSTRUCTOR_SIGNATURE = {ConstructorInvocation.class}; 49 private static final Class [] FIELD_SIGNATURE = {FieldInvocation.class}; 50 private static final Class [] FIELD_READ_SIGNATURE = {FieldReadInvocation.class}; 51 private static final Class [] FIELD_WRITE_SIGNATURE = {FieldWriteInvocation.class}; 52 private static final Class [] METHOD_CALLED_BY_METHOD_SIGNATURE = {MethodCalledByMethodInvocation.class}; 53 private static final Class [] METHOD_CALLED_BY_CONSTRUCTOR_SIGNATURE = {MethodCalledByConstructorInvocation.class}; 54 private static final Class [] CON_CALLED_BY_METHOD_SIGNATURE = {ConstructorCalledByMethodInvocation.class}; 55 private static final Class [] CON_CALLED_BY_CONSTRUCTOR_SIGNATURE = {ConstructorCalledByConstructorInvocation.class}; 56 protected Method invocationAdvice; 57 protected Method methodAdvice; 58 protected Method constructorAdvice; 59 protected Method fieldAdvice; 60 protected Method fieldReadAdvice; 61 protected Method fieldWriteAdvice; 62 protected Method methodCalledByMethodAdvice; 63 protected Method methodCalledByConstructorAdvice; 64 protected Method conCalledByMethodAdvice; 65 protected Method conCalledByConstructorAdvice; 66 protected Class aspectClass; 67 protected String adviceName; 68 69 protected void init(String advice, Class aspectClass) 70 { 71 this.aspectClass = aspectClass; 72 this.adviceName = advice; 73 invocationAdvice = findByInvocation(adviceName, aspectClass); 74 if (invocationAdvice == null) 75 { 76 methodAdvice = findByMethodInvocation(adviceName, aspectClass); 77 constructorAdvice = findByConstructorInvocation(adviceName, aspectClass); 78 fieldAdvice = findByFieldInvocation(adviceName, aspectClass); 79 if (fieldAdvice == null) 80 { 81 fieldReadAdvice = findByFieldReadInvocation(adviceName, aspectClass); 82 fieldWriteAdvice = findByFieldWriteInvocation(adviceName, aspectClass); 83 } 84 methodCalledByMethodAdvice = findByMethodCalledByMethodInvocation(adviceName, aspectClass); 85 methodCalledByConstructorAdvice = findByMethodCalledByConstructorInvocation(adviceName, aspectClass); 86 conCalledByMethodAdvice = findByConstructorCalledByMethodInvocation(adviceName, aspectClass); 87 conCalledByConstructorAdvice = findByConstructorCalledByConstructorInvocation(adviceName, aspectClass); 88 } 89 } 90 91 protected static Method findByInvocation(String adviceName, Class clazz) 92 { 93 try 94 { 95 return clazz.getMethod(adviceName, INVOCATION_SIGNATURE); 96 } 97 catch (NoSuchMethodException e) 98 { 99 } 101 return null; 102 } 103 104 protected static Method findByMethodInvocation(String adviceName, Class clazz) 105 { 106 try 107 { 108 return clazz.getMethod(adviceName, METHOD_SIGNATURE); 109 } 110 catch (NoSuchMethodException e) 111 { 112 } 114 return null; 115 } 116 117 protected static Method findByFieldInvocation(String adviceName, Class clazz) 118 { 119 try 120 { 121 return clazz.getMethod(adviceName, FIELD_SIGNATURE); 122 } 123 catch (NoSuchMethodException e) 124 { 125 } 127 return null; 128 } 129 130 protected static Method findByFieldReadInvocation(String adviceName, Class clazz) 131 { 132 try 133 { 134 return clazz.getMethod(adviceName, FIELD_READ_SIGNATURE); 135 } 136 catch (NoSuchMethodException e) 137 { 138 } 140 return null; 141 } 142 143 protected static Method findByFieldWriteInvocation(String adviceName, Class clazz) 144 { 145 try 146 { 147 return clazz.getMethod(adviceName, FIELD_WRITE_SIGNATURE); 148 } 149 catch (NoSuchMethodException e) 150 { 151 } 153 return null; 154 } 155 156 protected static Method findByConstructorInvocation(String adviceName, Class clazz) 157 { 158 try 159 { 160 return clazz.getMethod(adviceName, CONSTRUCTOR_SIGNATURE); 161 } 162 catch (NoSuchMethodException e) 163 { 164 } 166 return null; 167 } 168 169 protected static Method findByMethodCalledByMethodInvocation(String adviceName, Class clazz) 170 { 171 try 172 { 173 return clazz.getMethod(adviceName, METHOD_CALLED_BY_METHOD_SIGNATURE); 174 } 175 catch (NoSuchMethodException e) 176 { 177 } 179 return null; 180 } 181 182 protected static Method findByMethodCalledByConstructorInvocation(String adviceName, Class clazz) 183 { 184 try 185 { 186 return clazz.getMethod(adviceName, METHOD_CALLED_BY_CONSTRUCTOR_SIGNATURE); 187 } 188 catch (NoSuchMethodException e) 189 { 190 } 192 return null; 193 } 194 195 protected static Method findByConstructorCalledByMethodInvocation(String adviceName, Class clazz) 196 { 197 try 198 { 199 return clazz.getMethod(adviceName, CON_CALLED_BY_METHOD_SIGNATURE); 200 } 201 catch (NoSuchMethodException e) 202 { 203 } 205 return null; 206 } 207 208 protected static Method findByConstructorCalledByConstructorInvocation(String adviceName, Class clazz) 209 { 210 try 211 { 212 return clazz.getMethod(adviceName, CON_CALLED_BY_CONSTRUCTOR_SIGNATURE); 213 } 214 catch (NoSuchMethodException e) 215 { 216 } 218 return null; 219 } 220 221 protected Method resolveAdvice(Invocation invocation) 222 { 223 if (invocationAdvice != null) return invocationAdvice; 224 if (invocation instanceof MethodInvocation) 225 { 226 if (methodAdvice == null) 227 { 228 throw new IllegalStateException ("Unable to resolve MethodInvocation advice " + getName()); 229 } 230 return methodAdvice; 231 } 232 if (invocation instanceof FieldInvocation) 233 { 234 if (fieldAdvice != null) return fieldAdvice; 235 if (invocation instanceof FieldReadInvocation) 236 { 237 if (fieldReadAdvice == null) 238 { 239 throw new IllegalStateException ("Unable to resolve FieldReadInvocation advice " + getName()); 240 } 241 return fieldReadAdvice; 242 } 243 if (invocation instanceof FieldWriteInvocation) 244 { 245 if (fieldWriteAdvice == null) 246 { 247 throw new IllegalStateException ("Unable to resolve FieldWriteInvocation advice " + getName()); 248 } 249 return fieldWriteAdvice; 250 } 251 } 252 if (invocation instanceof ConstructorInvocation) 253 { 254 if (constructorAdvice == null) 255 { 256 throw new IllegalStateException ("Unable to resolve ConstructorInvocation advice " + getName()); 257 } 258 return constructorAdvice; 259 } 260 if (invocation instanceof MethodCalledByMethodInvocation) 261 { 262 if (methodCalledByMethodAdvice == null) 263 { 264 throw new IllegalStateException ("Unable to resolve MethodCalledByMethodInvocation advice " + getName()); 265 } 266 return methodCalledByMethodAdvice; 267 } 268 if (invocation instanceof MethodCalledByConstructorInvocation) 269 { 270 if (methodCalledByConstructorAdvice == null) 271 { 272 throw new IllegalStateException ("Unable to resolve MethodCalledByConstructorInvocation advice " + getName()); 273 } 274 return methodCalledByConstructorAdvice; 275 } 276 if (invocation instanceof ConstructorCalledByMethodInvocation) 277 { 278 if (conCalledByMethodAdvice == null) 279 { 280 throw new IllegalStateException ("Unable to resolve ConstructorCalledByMethodInvocation advice " + getName()); 281 } 282 return conCalledByMethodAdvice; 283 } 284 if (invocation instanceof ConstructorCalledByConstructorInvocation) 285 { 286 if (conCalledByMethodAdvice == null) 287 { 288 throw new IllegalStateException ("Unable to resolve ConstructorCalledByConstructorInvocation advice " + getName()); 289 } 290 return conCalledByConstructorAdvice; 291 } 292 throw new RuntimeException ("Should Be Unreachable, but unable to discover Advice"); 293 294 } 295 } 296 | Popular Tags |