1 /* *******************************************************************
2 * Copyright (c) 2005 IBM Corporation Ltd
4 * This program and the accompanying materials are made available
5 * under the terms of the Eclipse Public License v 2.0
6 * which accompanies this distribution and is available at
7 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
10 * Adrian Colyer initial implementation
11 * ******************************************************************/
12 package org.aspectj.ajdt.internal.compiler.ast;
14 import java.lang.reflect.Modifier;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Stack;
19 import org.aspectj.ajdt.internal.compiler.lookup.EclipseFactory;
20 import org.aspectj.ajdt.internal.compiler.lookup.EclipseScope;
21 import org.aspectj.ajdt.internal.core.builder.EclipseSourceContext;
22 import org.aspectj.bridge.context.CompilationAndWeavingContext;
23 import org.aspectj.bridge.context.ContextToken;
24 import org.aspectj.org.eclipse.jdt.core.compiler.CharOperation;
25 import org.aspectj.org.eclipse.jdt.internal.compiler.ASTVisitor;
26 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Annotation;
27 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Argument;
28 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
29 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
30 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.MemberValuePair;
31 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
32 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.NameReference;
33 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.NormalAnnotation;
34 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation;
35 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
36 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.StringLiteral;
37 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
38 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference;
39 import org.aspectj.org.eclipse.jdt.internal.compiler.impl.Constant;
40 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.Binding;
41 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BlockScope;
42 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ClassScope;
43 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
44 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
45 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodScope;
46 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
47 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
48 import org.aspectj.weaver.AdviceKind;
49 import org.aspectj.weaver.AjAttribute;
50 import org.aspectj.weaver.ISourceContext;
51 import org.aspectj.weaver.ResolvedPointcutDefinition;
52 import org.aspectj.weaver.UnresolvedType;
53 import org.aspectj.weaver.patterns.AbstractPatternNodeVisitor;
54 import org.aspectj.weaver.patterns.FormalBinding;
55 import org.aspectj.weaver.patterns.IfPointcut;
56 import org.aspectj.weaver.patterns.ParserException;
57 import org.aspectj.weaver.patterns.PatternParser;
58 import org.aspectj.weaver.patterns.Pointcut;
60 public class ValidateAtAspectJAnnotationsVisitor extends ASTVisitor {
62 private static final char[] beforeAdviceSig = "Lorg/aspectj/lang/annotation/Before;".toCharArray();
63 private static final char[] afterAdviceSig = "Lorg/aspectj/lang/annotation/After;".toCharArray();
64 private static final char[] afterReturningAdviceSig = "Lorg/aspectj/lang/annotation/AfterReturning;".toCharArray();
65 private static final char[] afterThrowingAdviceSig = "Lorg/aspectj/lang/annotation/AfterThrowing;".toCharArray();
66 private static final char[] aroundAdviceSig = "Lorg/aspectj/lang/annotation/Around;".toCharArray();
67 private static final char[] pointcutSig = "Lorg/aspectj/lang/annotation/Pointcut;".toCharArray();
68 private static final char[] aspectSig = "Lorg/aspectj/lang/annotation/Aspect;".toCharArray();
69 private static final char[] declareParentsSig = "Lorg/aspectj/lang/annotation/DeclareParents;".toCharArray();
70 private static final char[] adviceNameSig = "Lorg/aspectj/lang/annotation/AdviceName;".toCharArray();
71 // private static final char[] orgAspectJLangAnnotation =
72 // "org/aspectj/lang/annotation/".toCharArray();
73 private static final char[] voidType = "void".toCharArray();
74 private static final char[] booleanType = "boolean".toCharArray();
75 private static final char[] joinPoint = "Lorg/aspectj/lang/JoinPoint;".toCharArray();
76 private static final char[] joinPointStaticPart = "Lorg/aspectj/lang/JoinPoint$StaticPart;".toCharArray();
77 private static final char[] joinPointEnclosingStaticPart = "Lorg/aspectj/lang/JoinPoint$EnclosingStaticPart;".toCharArray();
78 private static final char[] proceedingJoinPoint = "Lorg/aspectj/lang/ProceedingJoinPoint;".toCharArray();
79 // private static final char[][] adviceSigs = new char[][] {
80 // beforeAdviceSig, afterAdviceSig, afterReturningAdviceSig,
81 // afterThrowingAdviceSig, aroundAdviceSig };
83 private final CompilationUnitDeclaration unit;
84 private final Stack typeStack = new Stack();
85 private AspectJAnnotations ajAnnotations;
87 public ValidateAtAspectJAnnotationsVisitor(CompilationUnitDeclaration unit) {
91 public boolean visit(TypeDeclaration localTypeDeclaration, BlockScope scope) {
92 typeStack.push(localTypeDeclaration);
93 ajAnnotations = new AspectJAnnotations(localTypeDeclaration.annotations);
94 checkTypeDeclaration(localTypeDeclaration);
98 public void endVisit(TypeDeclaration localTypeDeclaration, BlockScope scope) {
102 public boolean visit(TypeDeclaration memberTypeDeclaration, ClassScope scope) {
103 typeStack.push(memberTypeDeclaration);
104 ajAnnotations = new AspectJAnnotations(memberTypeDeclaration.annotations);
105 checkTypeDeclaration(memberTypeDeclaration);
109 public void endVisit(TypeDeclaration memberTypeDeclaration, ClassScope scope) {
113 public boolean visit(TypeDeclaration typeDeclaration, CompilationUnitScope scope) {
114 typeStack.push(typeDeclaration);
115 ajAnnotations = new AspectJAnnotations(typeDeclaration.annotations);
116 checkTypeDeclaration(typeDeclaration);
120 public void endVisit(TypeDeclaration typeDeclaration, CompilationUnitScope scope) {
124 private void checkTypeDeclaration(TypeDeclaration typeDecl) {
125 ContextToken tok = CompilationAndWeavingContext.enteringPhase(
126 CompilationAndWeavingContext.VALIDATING_AT_ASPECTJ_ANNOTATIONS, typeDecl.name);
127 if (!(typeDecl instanceof AspectDeclaration)) {
128 if (ajAnnotations.hasAspectAnnotation) {
129 validateAspectDeclaration(typeDecl);
131 // check that class doesn't extend aspect
132 TypeReference parentRef = typeDecl.superclass;
133 if (parentRef != null) {
134 TypeBinding parentBinding = parentRef.resolvedType;
135 if (parentBinding instanceof SourceTypeBinding) {
136 SourceTypeBinding parentSTB = (SourceTypeBinding) parentBinding;
137 if (parentSTB.scope != null) {
138 TypeDeclaration parentDecl = parentSTB.scope.referenceContext;
139 if (isAspect(parentDecl)) {
140 typeDecl.scope.problemReporter().signalError(typeDecl.sourceStart, typeDecl.sourceEnd,
141 "a class cannot extend an aspect");
148 // check that aspect doesn't have @Aspect annotation, we've already
149 // added on ourselves.
150 if (ajAnnotations.hasMultipleAspectAnnotations) {
151 typeDecl.scope.problemReporter().signalError(typeDecl.sourceStart, typeDecl.sourceEnd,
152 "aspects cannot have @Aspect annotation");
155 CompilationAndWeavingContext.leavingPhase(tok);
158 public boolean visit(FieldDeclaration fieldDeclaration, MethodScope scope) {
159 ajAnnotations = new AspectJAnnotations(fieldDeclaration.annotations);
160 if (ajAnnotations.hasDeclareParents && !insideAspect()) {
161 scope.problemReporter().signalError(fieldDeclaration.sourceStart, fieldDeclaration.sourceEnd,
162 "DeclareParents can only be used inside an aspect type");
167 public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
168 if (methodDeclaration.hasErrors()) {
171 ContextToken tok = CompilationAndWeavingContext.enteringPhase(
172 CompilationAndWeavingContext.VALIDATING_AT_ASPECTJ_ANNOTATIONS, methodDeclaration.selector);
173 ajAnnotations = new AspectJAnnotations(methodDeclaration.annotations);
174 if (!methodDeclaration.getClass().equals(AjMethodDeclaration.class)) {
175 // simply test for innapropriate use of annotations on code-style
177 if (methodDeclaration instanceof PointcutDeclaration) {
178 if (ajAnnotations.hasMultiplePointcutAnnotations || ajAnnotations.hasAdviceAnnotation
179 || ajAnnotations.hasAspectAnnotation || ajAnnotations.hasAdviceNameAnnotation) {
180 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.sourceStart,
181 methodDeclaration.sourceEnd, "@AspectJ annotations cannot be declared on this aspect member");
183 } else if (methodDeclaration instanceof AdviceDeclaration) {
184 if (ajAnnotations.hasMultipleAdviceAnnotations || ajAnnotations.hasAspectAnnotation
185 || ajAnnotations.hasPointcutAnnotation) {
186 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.sourceStart,
187 methodDeclaration.sourceEnd, "Only @AdviceName AspectJ annotation allowed on advice");
190 if (ajAnnotations.hasAspectJAnnotations()) {
191 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.sourceStart,
192 methodDeclaration.sourceEnd, "@AspectJ annotations cannot be declared on this aspect member");
195 CompilationAndWeavingContext.leavingPhase(tok);
199 if (ajAnnotations.hasAdviceAnnotation) {
200 validateAdvice(methodDeclaration);
201 } else if (ajAnnotations.hasPointcutAnnotation) {
202 convertToPointcutDeclaration(methodDeclaration, scope);
204 CompilationAndWeavingContext.leavingPhase(tok);
208 // private boolean isAspectJAnnotation(Annotation ann) {
209 // if (ann.resolvedType == null) return false;
210 // char[] sig = ann.resolvedType.signature();
211 // return CharOperation.contains(orgAspectJLangAnnotation, sig);
214 private boolean insideAspect() {
215 if (typeStack.empty())
217 TypeDeclaration typeDecl = (TypeDeclaration) typeStack.peek();
218 return isAspect(typeDecl);
221 private boolean isAspect(TypeDeclaration typeDecl) {
222 if (typeDecl instanceof AspectDeclaration)
224 return new AspectJAnnotations(typeDecl.annotations).hasAspectAnnotation;
228 * aspect must be public nested aspect must be static cannot extend a concrete aspect pointcut in perclause must be good.
230 private void validateAspectDeclaration(TypeDeclaration typeDecl) {
231 if (typeStack.size() > 1) {
232 // it's a nested aspect
233 if (!Modifier.isStatic(typeDecl.modifiers)) {
234 typeDecl.scope.problemReporter().signalError(typeDecl.sourceStart, typeDecl.sourceEnd,
235 "inner aspects must be static");
240 SourceTypeBinding binding = typeDecl.binding;
241 if (binding != null) {
242 if (binding.isEnum() || binding.isInterface() || binding.isAnnotationType()) {
243 typeDecl.scope.problemReporter().signalError(typeDecl.sourceStart, typeDecl.sourceEnd,
244 "only classes can have an @Aspect annotation");
248 // FIXME AV - do we really want that
249 // if (!Modifier.isPublic(typeDecl.modifiers)) {
250 // typeDecl.scope.problemReporter().signalError(typeDecl.sourceStart,
251 // typeDecl.sourceEnd,"@Aspect class must be public");
254 TypeReference parentRef = typeDecl.superclass;
255 if (parentRef != null) {
256 TypeBinding parentBinding = parentRef.resolvedType;
257 if (parentBinding instanceof SourceTypeBinding) {
258 SourceTypeBinding parentSTB = (SourceTypeBinding) parentBinding;
259 if (parentSTB.scope != null) { // scope is null if its a
260 // binarytypebinding (in AJ
261 // world, thats a subclass of
262 // SourceTypeBinding)
263 TypeDeclaration parentDecl = parentSTB.scope.referenceContext;
264 if (isAspect(parentDecl) && !Modifier.isAbstract(parentDecl.modifiers)) {
265 typeDecl.scope.problemReporter().signalError(typeDecl.sourceStart, typeDecl.sourceEnd,
266 "cannot extend a concrete aspect");
272 Annotation aspectAnnotation = ajAnnotations.aspectAnnotation;
274 int[] pcLoc = new int[2];
275 String perClause = getStringLiteralFor("value", aspectAnnotation, pcLoc);
276 // AspectDeclaration aspectDecl = new
277 // AspectDeclaration(typeDecl.compilationResult);
280 if (perClause != null && !perClause.equals("")) {
281 ISourceContext context = new EclipseSourceContext(unit.compilationResult, pcLoc[0]);
282 Pointcut pc = new PatternParser(perClause, context).maybeParsePerClause();
283 FormalBinding[] bindings = new FormalBinding[0];
285 pc.resolve(new EclipseScope(bindings, typeDecl.scope));
287 } catch (ParserException pEx) {
288 typeDecl.scope.problemReporter().parseError(pcLoc[0] + pEx.getLocation().getStart(),
289 pcLoc[0] + pEx.getLocation().getEnd(), -1, perClause.toCharArray(), perClause,
290 new String[] { pEx.getMessage() });
295 * 1) Advice must be public 2) Advice must have a void return type if not around advice 3) Advice must not have any other @AspectJ
296 * annotations 4) After throwing advice must declare the thrown formal 5) After returning advice must declare the returning
297 * formal 6) Advice must not be static
299 private void validateAdvice(MethodDeclaration methodDeclaration) {
301 if (!insideAspect()) {
302 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.sourceStart, methodDeclaration.sourceEnd,
303 "Advice must be declared inside an aspect type");
306 if (!Modifier.isPublic(methodDeclaration.modifiers)) {
307 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.sourceStart, methodDeclaration.sourceEnd,
308 "advice must be public");
311 if (Modifier.isStatic(methodDeclaration.modifiers)) {
312 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.sourceStart, methodDeclaration.sourceEnd,
313 "advice can not be declared static");
316 if (ajAnnotations.hasMultipleAdviceAnnotations) {
317 methodDeclaration.scope.problemReporter().disallowedTargetForAnnotation(ajAnnotations.duplicateAdviceAnnotation);
319 if (ajAnnotations.hasPointcutAnnotation) {
320 methodDeclaration.scope.problemReporter().disallowedTargetForAnnotation(ajAnnotations.pointcutAnnotation);
322 if (ajAnnotations.hasAspectAnnotation) {
323 methodDeclaration.scope.problemReporter().disallowedTargetForAnnotation(ajAnnotations.aspectAnnotation);
325 if (ajAnnotations.hasAdviceNameAnnotation) {
326 methodDeclaration.scope.problemReporter().disallowedTargetForAnnotation(ajAnnotations.adviceNameAnnotation);
329 if (ajAnnotations.adviceKind != AdviceKind.Around) {
330 ensureVoidReturnType(methodDeclaration);
333 if (ajAnnotations.adviceKind == AdviceKind.AfterThrowing) {
334 int[] throwingLocation = new int[2];
335 String thrownFormal = getStringLiteralFor("throwing", ajAnnotations.adviceAnnotation, throwingLocation);
336 if (thrownFormal != null) {
337 // Argument[] arguments = methodDeclaration.arguments;
338 if (!toArgumentNames(methodDeclaration.arguments).contains(thrownFormal)) {
339 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.sourceStart,
340 methodDeclaration.sourceEnd,
341 "throwing formal '" + thrownFormal + "' must be declared as a parameter in the advice signature");
346 if (ajAnnotations.adviceKind == AdviceKind.AfterReturning) {
347 int[] throwingLocation = new int[2];
348 String returningFormal = getStringLiteralFor("returning", ajAnnotations.adviceAnnotation, throwingLocation);
349 if (returningFormal != null) {
350 if (!toArgumentNames(methodDeclaration.arguments).contains(returningFormal)) {
351 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.sourceStart,
352 methodDeclaration.sourceEnd,
353 "returning formal '" + returningFormal + "' must be declared as a parameter in the advice signature");
358 resolveAndSetPointcut(methodDeclaration, ajAnnotations.adviceAnnotation);
363 * Get the argument names as a string list
366 * @return argument names (possibly empty)
368 private List toArgumentNames(Argument[] arguments) {
369 List names = new ArrayList();
370 if (arguments == null) {
373 for (Argument argument : arguments) {
374 names.add(new String(argument.name));
380 private void resolveAndSetPointcut(MethodDeclaration methodDeclaration, Annotation adviceAnn) {
381 int[] pcLocation = new int[2];
382 String pointcutExpression = getStringLiteralFor("pointcut", adviceAnn, pcLocation);
383 if (pointcutExpression == null)
384 pointcutExpression = getStringLiteralFor("value", adviceAnn, pcLocation);
386 // +1 to give first char of pointcut string
387 ISourceContext context = new EclipseSourceContext(unit.compilationResult, pcLocation[0] + 1);
388 if (pointcutExpression == null) {
389 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.sourceStart,
390 methodDeclaration.sourceEnd, "the advice annotation must specify a pointcut value");
393 PatternParser pp = new PatternParser(pointcutExpression, context);
394 Pointcut pc = pp.parsePointcut();
396 FormalBinding[] bindings = buildFormalAdviceBindingsFrom(methodDeclaration);
397 pc.resolve(new EclipseScope(bindings, methodDeclaration.scope));
398 EclipseFactory factory = EclipseFactory.fromScopeLookupEnvironment(methodDeclaration.scope);
399 // now create a ResolvedPointcutDefinition,make an attribute out of
400 // it, and add it to the method
401 UnresolvedType[] paramTypes = new UnresolvedType[bindings.length];
402 for (int i = 0; i < paramTypes.length; i++)
403 paramTypes[i] = bindings[i].getType();
404 ResolvedPointcutDefinition resPcutDef = new ResolvedPointcutDefinition(factory.fromBinding(((TypeDeclaration) typeStack
405 .peek()).binding), methodDeclaration.modifiers, "anonymous", paramTypes, pc);
406 AjAttribute attr = new AjAttribute.PointcutDeclarationAttribute(resPcutDef);
407 ((AjMethodDeclaration) methodDeclaration).addAttribute(new EclipseAttributeAdapter(attr));
408 } catch (ParserException pEx) {
409 methodDeclaration.scope.problemReporter().parseError(pcLocation[0] + pEx.getLocation().getStart(),
410 pcLocation[0] + pEx.getLocation().getEnd(), -1, pointcutExpression.toCharArray(), pointcutExpression,
411 new String[] { pEx.getMessage() });
415 private void ensureVoidReturnType(MethodDeclaration methodDeclaration) {
416 boolean returnsVoid = true;
417 if ((methodDeclaration.returnType instanceof SingleTypeReference)) {
418 SingleTypeReference retType = (SingleTypeReference) methodDeclaration.returnType;
419 if (!CharOperation.equals(voidType, retType.token)) {
426 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.returnType.sourceStart,
427 methodDeclaration.returnType.sourceEnd, "This advice must return void");
431 private FormalBinding[] buildFormalAdviceBindingsFrom(MethodDeclaration mDecl) {
432 if (mDecl.arguments == null)
433 return new FormalBinding[0];
434 if (mDecl.binding == null)
435 return new FormalBinding[0];
436 EclipseFactory factory = EclipseFactory.fromScopeLookupEnvironment(mDecl.scope);
437 String extraArgName = maybeGetExtraArgName();
438 if (extraArgName == null)
440 FormalBinding[] ret = new FormalBinding[mDecl.arguments.length];
441 for (int i = 0; i < mDecl.arguments.length; i++) {
442 Argument arg = mDecl.arguments[i];
443 String name = new String(arg.name);
444 TypeBinding argTypeBinding = mDecl.binding.parameters[i];
445 UnresolvedType type = factory.fromBinding(argTypeBinding);
446 if (CharOperation.equals(joinPoint, argTypeBinding.signature())
447 || CharOperation.equals(joinPointStaticPart, argTypeBinding.signature())
448 || CharOperation.equals(joinPointEnclosingStaticPart, argTypeBinding.signature())
449 || CharOperation.equals(proceedingJoinPoint, argTypeBinding.signature()) || name.equals(extraArgName)) {
450 ret[i] = new FormalBinding.ImplicitFormalBinding(type, name, i);
452 ret[i] = new FormalBinding(type, name, i, arg.sourceStart, arg.sourceEnd);
458 private String maybeGetExtraArgName() {
459 String argName = null;
460 if (ajAnnotations.adviceKind == AdviceKind.AfterReturning) {
461 argName = getStringLiteralFor("returning", ajAnnotations.adviceAnnotation, new int[2]);
462 } else if (ajAnnotations.adviceKind == AdviceKind.AfterThrowing) {
463 argName = getStringLiteralFor("throwing", ajAnnotations.adviceAnnotation, new int[2]);
468 private String getStringLiteralFor(String memberName, Annotation inAnnotation, int[] location) {
469 if (inAnnotation instanceof SingleMemberAnnotation && memberName.equals("value")) {
470 SingleMemberAnnotation sma = (SingleMemberAnnotation) inAnnotation;
471 if (sma.memberValue instanceof StringLiteral) {
472 StringLiteral sv = (StringLiteral) sma.memberValue;
473 location[0] = sv.sourceStart;
474 location[1] = sv.sourceEnd;
475 return new String(sv.source());
476 } else if (sma.memberValue instanceof NameReference
477 && (((NameReference) sma.memberValue).binding instanceof FieldBinding)) {
478 Binding b = ((NameReference) sma.memberValue).binding;
479 Constant c = ((FieldBinding) b).constant();
480 return c.stringValue();
483 if (!(inAnnotation instanceof NormalAnnotation))
485 NormalAnnotation ann = (NormalAnnotation) inAnnotation;
486 MemberValuePair[] mvps = ann.memberValuePairs;
489 for (MemberValuePair mvp : mvps) {
490 if (CharOperation.equals(memberName.toCharArray(), mvp.name)) {
491 if (mvp.value instanceof StringLiteral) {
492 StringLiteral sv = (StringLiteral) mvp.value;
493 location[0] = sv.sourceStart;
494 location[1] = sv.sourceEnd;
495 return new String(sv.source());
502 private void convertToPointcutDeclaration(MethodDeclaration methodDeclaration, ClassScope scope) {
503 TypeDeclaration typeDecl = (TypeDeclaration) typeStack.peek();
504 if (typeDecl.binding != null) {
505 if (!typeDecl.binding.isClass()) {
506 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.sourceStart, methodDeclaration.sourceEnd,
507 "pointcuts can only be declared in a class or an aspect");
511 if (methodDeclaration.thrownExceptions != null && methodDeclaration.thrownExceptions.length > 0) {
512 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.sourceStart, methodDeclaration.sourceEnd,
513 "pointcuts cannot throw exceptions!");
516 PointcutDeclaration pcDecl = new PointcutDeclaration(unit.compilationResult);
517 copyAllFields(methodDeclaration, pcDecl);
519 if (ajAnnotations.hasAdviceAnnotation) {
520 methodDeclaration.scope.problemReporter().disallowedTargetForAnnotation(ajAnnotations.adviceAnnotation);
522 if (ajAnnotations.hasAspectAnnotation) {
523 methodDeclaration.scope.problemReporter().disallowedTargetForAnnotation(ajAnnotations.aspectAnnotation);
525 if (ajAnnotations.hasAdviceNameAnnotation) {
526 methodDeclaration.scope.problemReporter().disallowedTargetForAnnotation(ajAnnotations.adviceNameAnnotation);
529 boolean noValueSupplied = true;
530 boolean containsIfPcd = false;
531 int[] pcLocation = new int[2];
532 String pointcutExpression = getStringLiteralFor("value", ajAnnotations.pointcutAnnotation, pcLocation);
534 ISourceContext context = new EclipseSourceContext(unit.compilationResult, pcLocation[0]);
535 Pointcut pc = null;// abstract
536 if (pointcutExpression == null || pointcutExpression.length() == 0) {
537 noValueSupplied = true; // matches nothing pointcut
539 noValueSupplied = false;
540 pc = new PatternParser(pointcutExpression, context).parsePointcut();
542 pcDecl.pointcutDesignator = (pc == null) ? null : new PointcutDesignator(pc);
543 pcDecl.setGenerateSyntheticPointcutMethod();
544 TypeDeclaration onType = (TypeDeclaration) typeStack.peek();
545 pcDecl.postParse(onType);
546 // EclipseFactory factory =
547 // EclipseFactory.fromScopeLookupEnvironment
548 // (methodDeclaration.scope);
549 // int argsLength = methodDeclaration.arguments == null ? 0 :
550 // methodDeclaration.arguments.length;
551 FormalBinding[] bindings = buildFormalAdviceBindingsFrom(methodDeclaration);
552 // FormalBinding[] bindings = new FormalBinding[argsLength];
553 // for (int i = 0, len = bindings.length; i < len; i++) {
554 // Argument arg = methodDeclaration.arguments[i];
555 // String name = new String(arg.name);
556 // UnresolvedType type =
557 // factory.fromBinding(methodDeclaration.binding.parameters[i]);
558 // bindings[i] = new FormalBinding(type, name, i, arg.sourceStart,
559 // arg.sourceEnd, "unknown");
561 swap(onType, methodDeclaration, pcDecl);
564 EclipseScope eScope = new EclipseScope(bindings, methodDeclaration.scope);
565 char[] packageName = null;
566 if (typeDecl.binding != null && typeDecl.binding.getPackage() != null) {
567 packageName = typeDecl.binding.getPackage().readableName();
569 eScope.setLimitedImports(packageName);
571 HasIfPCDVisitor ifFinder = new HasIfPCDVisitor();
572 pc.traverse(ifFinder, null);
573 containsIfPcd = ifFinder.containsIfPcd;
575 } catch (ParserException pEx) {
576 methodDeclaration.scope.problemReporter().parseError(pcLocation[0] + pEx.getLocation().getStart(),
577 pcLocation[0] + pEx.getLocation().getEnd(), -1, pointcutExpression.toCharArray(), pointcutExpression,
578 new String[] { pEx.getMessage() });
581 boolean returnsVoid = false;
582 boolean returnsBoolean = false;
583 if ((methodDeclaration.returnType instanceof SingleTypeReference)) {
584 SingleTypeReference retType = (SingleTypeReference) methodDeclaration.returnType;
585 if (CharOperation.equals(voidType, retType.token))
587 if (CharOperation.equals(booleanType, retType.token))
588 returnsBoolean = true;
590 if (!returnsVoid && !containsIfPcd) {
591 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.returnType.sourceStart,
592 methodDeclaration.returnType.sourceEnd,
593 "Methods annotated with @Pointcut must return void unless the pointcut contains an if() expression");
595 if (!returnsBoolean && containsIfPcd) {
596 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.returnType.sourceStart,
597 methodDeclaration.returnType.sourceEnd,
598 "Methods annotated with @Pointcut must return boolean when the pointcut contains an if() expression");
601 if (methodDeclaration.statements != null && methodDeclaration.statements.length > 0 && !containsIfPcd) {
602 methodDeclaration.scope.problemReporter()
603 .signalError(methodDeclaration.returnType.sourceStart, methodDeclaration.returnType.sourceEnd,
604 "Pointcuts without an if() expression should have an empty method body");
607 if (pcDecl.pointcutDesignator == null) {
608 if (Modifier.isAbstract(methodDeclaration.modifiers) || noValueSupplied // this
614 // those 2 checks makes sense for aop.xml concretization but NOT for
615 // regular abstraction of pointcut
617 // && (methodDeclaration.arguments == null ||
618 // methodDeclaration.arguments.length == 0)) {
622 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.returnType.sourceStart,
623 methodDeclaration.returnType.sourceEnd,
624 "Method annotated with @Pointcut() for abstract pointcut must be abstract");
626 } else if (Modifier.isAbstract(methodDeclaration.modifiers)) {
627 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.returnType.sourceStart,
628 methodDeclaration.returnType.sourceEnd,
629 "Method annotated with non abstract @Pointcut(\"" + pointcutExpression + "\") is abstract");
633 private void copyAllFields(MethodDeclaration from, MethodDeclaration to) {
634 to.annotations = from.annotations;
635 to.arguments = from.arguments;
636 to.binding = from.binding;
638 to.bodyEnd = from.bodyEnd;
639 to.bodyStart = from.bodyStart;
640 to.declarationSourceEnd = from.declarationSourceEnd;
641 to.declarationSourceStart = from.declarationSourceStart;
642 to.explicitDeclarations = from.explicitDeclarations;
643 to.ignoreFurtherInvestigation = from.ignoreFurtherInvestigation;
644 to.javadoc = from.javadoc;
645 to.modifiers = from.modifiers;
646 to.modifiersSourceStart = from.modifiersSourceStart;
647 to.returnType = from.returnType;
648 to.scope = from.scope;
649 to.selector = from.selector;
650 to.sourceEnd = from.sourceEnd;
651 to.sourceStart = from.sourceStart;
652 to.statements = from.statements;
653 to.thrownExceptions = from.thrownExceptions;
654 to.typeParameters = from.typeParameters;
657 private void swap(TypeDeclaration inType, MethodDeclaration thisDeclaration, MethodDeclaration forThatDeclaration) {
658 for (int i = 0; i < inType.methods.length; i++) {
659 if (inType.methods[i] == thisDeclaration) {
660 inType.methods[i] = forThatDeclaration;
666 private static class AspectJAnnotations {
667 boolean hasAdviceAnnotation = false;
668 boolean hasPointcutAnnotation = false;
669 boolean hasAspectAnnotation = false;
670 boolean hasAdviceNameAnnotation = false;
671 boolean hasDeclareParents = false;
672 boolean hasMultipleAdviceAnnotations = false;
673 boolean hasMultiplePointcutAnnotations = false;
674 boolean hasMultipleAspectAnnotations = false;
676 AdviceKind adviceKind = null;
677 Annotation adviceAnnotation = null;
678 Annotation pointcutAnnotation = null;
679 Annotation aspectAnnotation = null;
680 Annotation adviceNameAnnotation = null;
682 Annotation duplicateAdviceAnnotation = null;
683 Annotation duplicatePointcutAnnotation = null;
684 Annotation duplicateAspectAnnotation = null;
686 public AspectJAnnotations(Annotation[] annotations) {
687 if (annotations == null)
689 for (Annotation annotation : annotations) {
690 if (annotation.resolvedType == null)
691 continue; // user messed up annotation declaration
692 char[] sig = annotation.resolvedType.signature();
693 if (CharOperation.equals(afterAdviceSig, sig)) {
694 adviceKind = AdviceKind.After;
695 addAdviceAnnotation(annotation);
696 } else if (CharOperation.equals(afterReturningAdviceSig, sig)) {
697 adviceKind = AdviceKind.AfterReturning;
698 addAdviceAnnotation(annotation);
699 } else if (CharOperation.equals(afterThrowingAdviceSig, sig)) {
700 adviceKind = AdviceKind.AfterThrowing;
701 addAdviceAnnotation(annotation);
702 } else if (CharOperation.equals(beforeAdviceSig, sig)) {
703 adviceKind = AdviceKind.Before;
704 addAdviceAnnotation(annotation);
705 } else if (CharOperation.equals(aroundAdviceSig, sig)) {
706 adviceKind = AdviceKind.Around;
707 addAdviceAnnotation(annotation);
708 } else if (CharOperation.equals(adviceNameSig, sig)) {
709 hasAdviceNameAnnotation = true;
710 adviceNameAnnotation = annotation;
711 } else if (CharOperation.equals(declareParentsSig, sig)) {
712 hasDeclareParents = true;
713 } else if (CharOperation.equals(aspectSig, sig)) {
714 if (hasAspectAnnotation) {
715 hasMultipleAspectAnnotations = true;
716 duplicateAspectAnnotation = annotation;
718 hasAspectAnnotation = true;
719 aspectAnnotation = annotation;
721 } else if (CharOperation.equals(pointcutSig, sig)) {
722 if (hasPointcutAnnotation) {
723 hasMultiplePointcutAnnotations = true;
724 duplicatePointcutAnnotation = annotation;
726 hasPointcutAnnotation = true;
727 pointcutAnnotation = annotation;
734 public boolean hasAspectJAnnotations() {
735 return hasAdviceAnnotation || hasPointcutAnnotation || hasAdviceNameAnnotation || hasAspectAnnotation;
738 private void addAdviceAnnotation(Annotation annotation) {
739 if (!hasAdviceAnnotation) {
740 hasAdviceAnnotation = true;
741 adviceAnnotation = annotation;
743 hasMultipleAdviceAnnotations = true;
744 duplicateAdviceAnnotation = annotation;
749 private static class HasIfPCDVisitor extends AbstractPatternNodeVisitor {
750 public boolean containsIfPcd = false;
752 public Object visit(IfPointcut node, Object data) {
753 containsIfPcd = true;