1 4 package com.tc.aspectwerkz.annotation; 5 6 7 import com.tc.aspectwerkz.DeploymentModel; 8 import com.tc.aspectwerkz.reflect.ClassInfo; 9 import com.tc.aspectwerkz.reflect.FieldInfo; 10 import com.tc.aspectwerkz.reflect.ClassInfoHelper; 11 import com.tc.aspectwerkz.reflect.MethodInfo; 12 import com.tc.aspectwerkz.aspect.AdviceType; 13 import com.tc.aspectwerkz.definition.AdviceDefinition; 14 import com.tc.aspectwerkz.definition.AspectDefinition; 15 import com.tc.aspectwerkz.definition.DefinitionParserHelper; 16 import com.tc.aspectwerkz.definition.DeploymentScope; 17 import com.tc.aspectwerkz.exception.DefinitionException; 18 import com.tc.aspectwerkz.util.Strings; 19 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 32 public class AspectAnnotationParser { 33 34 37 private final static AspectAnnotationParser INSTANCE = new AspectAnnotationParser(); 38 39 42 private AspectAnnotationParser() { 43 } 44 45 52 public static void parse(final ClassInfo classInfo, final AspectDefinition aspectDef, final ClassLoader loader) { 53 INSTANCE.doParse(classInfo, aspectDef, loader); 54 } 55 56 63 private void doParse(final ClassInfo classInfo, final AspectDefinition aspectDef, final ClassLoader loader) { 64 if (classInfo == null) { 65 throw new IllegalArgumentException ("class to parse can not be null"); 66 } 67 68 Aspect aspectAnnotation = (Aspect) AsmAnnotations.getAnnotation( 69 AnnotationConstants.ASPECT, 70 classInfo 71 ); 72 73 String aspectName = classInfo.getName(); 74 String deploymentModelAsString = null; 75 76 if (aspectAnnotation != null) { 77 if (aspectAnnotation.value() != null) { 78 deploymentModelAsString = aspectAnnotation.value(); 80 } else { 81 if (aspectAnnotation.name() != null) { 82 aspectName = aspectAnnotation.name(); 84 } 85 if (aspectAnnotation.deploymentModel() != null) { 86 deploymentModelAsString = aspectAnnotation.deploymentModel(); 88 } 89 } 90 } 91 92 aspectDef.setDeploymentModel(DeploymentModel.getDeploymentModelFor(deploymentModelAsString)); 94 String className = classInfo.getName(); 95 parseFieldAttributes(classInfo, aspectDef); 96 parseMethodAttributes(classInfo, className, aspectName, aspectDef); 97 } 98 99 105 private void parseFieldAttributes(final ClassInfo classInfo, final AspectDefinition aspectDef) { 106 if (aspectDef == null) { 107 throw new IllegalArgumentException ("aspect definition can not be null"); 108 } 109 if (classInfo == null) { 110 return; 111 } 112 113 FieldInfo[] fieldList = classInfo.getFields(); 114 for (int i = 0; i < fieldList.length; i++) { 115 FieldInfo field = fieldList[i]; 116 117 Expression expression = (Expression) AsmAnnotations.getAnnotation(AnnotationConstants.EXPRESSION, field); 119 if (expression != null) { 120 if (field.getType().getName().equals(DeploymentScope.class.getName())) { 121 DefinitionParserHelper.createAndAddDeploymentScopeDef( 122 field.getName(), 123 expression.value(), 124 aspectDef.getSystemDefinition() 125 ); 126 } else { 127 DefinitionParserHelper.createAndAddPointcutDefToAspectDef( 128 field.getName(), 129 expression.value(), 130 aspectDef 131 ); 132 } 133 } 134 135 Introduce introduce = (Introduce) AsmAnnotations.getAnnotation(AnnotationConstants.INTRODUCE, field); 137 if (introduce != null) { 138 DefinitionParserHelper.createAndAddInterfaceIntroductionDefToAspectDef( 139 introduce.value(), 140 field.getName(), 141 field.getType().getName(), 142 aspectDef 143 ); 144 } 145 } 146 147 parseFieldAttributes(classInfo.getSuperclass(), aspectDef); 149 } 150 151 159 private void parseMethodAttributes(final ClassInfo classInfo, 160 final String aspectClassName, 161 final String aspectName, 162 final AspectDefinition aspectDef) { 163 if (classInfo == null) { 164 throw new IllegalArgumentException ("class can not be null"); 165 } 166 if (aspectClassName == null) { 167 throw new IllegalArgumentException ("aspect class name can not be null"); 168 } 169 if (aspectName == null) { 170 throw new IllegalArgumentException ("aspect name can not be null " + aspectClassName); 171 } 172 if (aspectDef == null) { 173 throw new IllegalArgumentException ("aspect definition can not be null"); 174 } 175 List methodList = ClassInfoHelper.createMethodList(classInfo); 177 178 parsePointcutAttributes(methodList, aspectDef); 180 181 for (Iterator it = methodList.iterator(); it.hasNext();) { 183 MethodInfo method = (MethodInfo) it.next(); 184 try { 185 parseAroundAttributes(method, aspectName, aspectClassName, aspectDef); 187 parseBeforeAttributes(method, aspectName, aspectClassName, aspectDef); 188 parseAfterAttributes(method, aspectName, aspectClassName, aspectDef); 189 } catch (DefinitionException e) { 190 System.err.println("AW::WARNING - unable to register advice: " + e.toString()); 191 } 193 } 194 } 195 196 202 private void parsePointcutAttributes(final List methodList, final AspectDefinition aspectDef) { 203 for (Iterator it = methodList.iterator(); it.hasNext();) { 204 MethodInfo method = (MethodInfo) it.next(); 205 206 Expression annotation = (Expression) AsmAnnotations.getAnnotation(AnnotationConstants.EXPRESSION, method); 208 if (annotation != null) { 209 DefinitionParserHelper.createAndAddPointcutDefToAspectDef( 210 getAdviceNameAsInSource(method), 211 annotation.value(), aspectDef 212 ); 213 } 214 } 215 } 216 217 225 private void parseAroundAttributes(final MethodInfo method, 226 final String aspectName, 227 final String aspectClassName, 228 final AspectDefinition aspectDef) { 229 Around aroundAnnotation = (Around) AsmAnnotations.getAnnotation(AnnotationConstants.AROUND, method); 230 if (aroundAnnotation != null) { 231 AdviceDefinition adviceDef = DefinitionParserHelper.createAdviceDefinition( 232 getAdviceNameAsInSource(method), 233 AdviceType.AROUND, 234 aroundAnnotation.value(), 235 null, 236 aspectName, 237 aspectClassName, 238 method, 239 aspectDef 240 ); 241 aspectDef.addAroundAdviceDefinition(adviceDef); 242 } 243 } 244 245 253 private void parseBeforeAttributes(final MethodInfo method, 254 final String aspectName, 255 final String aspectClassName, 256 final AspectDefinition aspectDef) { 257 Before beforeAnnotation = (Before) AsmAnnotations.getAnnotation(AnnotationConstants.BEFORE, method); 258 if (beforeAnnotation != null) { 259 AdviceDefinition adviceDef = DefinitionParserHelper.createAdviceDefinition( 260 getAdviceNameAsInSource(method), 261 AdviceType.BEFORE, 262 beforeAnnotation.value(), 263 null, 264 aspectName, 265 aspectClassName, 266 method, 267 aspectDef 268 ); 269 aspectDef.addBeforeAdviceDefinition(adviceDef); 270 } 271 } 272 273 281 private void parseAfterAttributes(final MethodInfo method, 282 final String aspectName, 283 final String aspectClassName, 284 final AspectDefinition aspectDef) { 285 After annotationAft = (After) AsmAnnotations.getAnnotation(AnnotationConstants.AFTER, method); 286 if (annotationAft != null) { 287 AdviceDefinition adviceDef = DefinitionParserHelper.createAdviceDefinition( 288 getAdviceNameAsInSource(method), 289 AdviceType.AFTER, 290 annotationAft.value(), 291 null, 292 aspectName, 293 aspectClassName, 294 method, 295 aspectDef 296 ); 297 aspectDef.addAfterAdviceDefinition(adviceDef); 298 } 299 300 AfterReturning annotationRet = (AfterReturning) AsmAnnotations.getAnnotation(AnnotationConstants.AFTER_RETURNING, method); 301 if (annotationRet != null) { 302 AdviceDefinition adviceDef = DefinitionParserHelper.createAdviceDefinition( 303 getAdviceNameAsInSource(method), 304 AdviceType.AFTER_RETURNING, 305 getExpressionElseValue(annotationRet.value(), annotationRet.pointcut()), 306 annotationRet.type(), 307 aspectName, 308 aspectClassName, 309 method, 310 aspectDef 311 ); 312 aspectDef.addAfterAdviceDefinition(adviceDef); 313 } 314 315 AfterThrowing annotationThr = (AfterThrowing) AsmAnnotations.getAnnotation(AnnotationConstants.AFTER_THROWING, method); 316 if (annotationThr != null) { 317 AdviceDefinition adviceDef = DefinitionParserHelper.createAdviceDefinition( 318 getAdviceNameAsInSource(method), 319 AdviceType.AFTER_THROWING, 320 getExpressionElseValue(annotationThr.value(), annotationThr.pointcut()), 321 annotationThr.type(), 322 aspectName, 323 aspectClassName, 324 method, 325 aspectDef 326 ); 327 aspectDef.addAfterAdviceDefinition(adviceDef); 328 } 329 330 AfterFinally annotationFin = (AfterFinally) AsmAnnotations.getAnnotation(AnnotationConstants.AFTER_FINALLY, method); 331 if (annotationFin != null) { 332 AdviceDefinition adviceDef = DefinitionParserHelper.createAdviceDefinition( 333 getAdviceNameAsInSource(method), 334 AdviceType.AFTER_FINALLY, 335 annotationFin.value(), 336 null, 337 aspectName, 338 aspectClassName, 339 method, 340 aspectDef 341 ); 342 aspectDef.addAfterAdviceDefinition(adviceDef); 343 } 344 } 345 346 353 public static String getAdviceNameAsInSource(final MethodInfo methodInfo) { 354 StringBuffer buffer = new StringBuffer (methodInfo.getName()); 355 if (methodInfo.getParameterNames() == null 356 || methodInfo.getParameterNames().length != methodInfo.getParameterTypes().length 357 || (methodInfo.getParameterNames().length > 0 && methodInfo.getParameterNames()[0] == null)) { 358 return methodInfo.getName(); 359 } 360 if (methodInfo.getParameterNames().length > 0) { 361 buffer.append('('); 362 for (int i = 0; i < methodInfo.getParameterNames().length; i++) { 363 if (i > 0) { 364 buffer.append(", "); 365 } 366 String parameterName = methodInfo.getParameterNames()[i]; 367 buffer.append(methodInfo.getParameterTypes()[i].getName()); 368 buffer.append(' ').append(parameterName); 369 } 370 buffer.append(')'); 371 } 372 return buffer.toString(); 373 } 374 375 383 public static String getExpressionElseValue(String value, String pointcut) { 384 if (!Strings.isNullOrEmpty(pointcut)) { 385 return pointcut; 386 } else if (!Strings.isNullOrEmpty(value)) { 387 return value; 388 } else { 389 throw new DefinitionException("neither expression nor value had a valid value"); 390 } 391 } 392 393 } | Popular Tags |