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 = FormalBinding.NONE;
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 FormalBinding.NONE;
434 if (mDecl.binding == null)
435 return FormalBinding.NONE;
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 boolean isIfTrueOrFalse = false;
532 int[] pcLocation = new int[2];
533 String pointcutExpression = getStringLiteralFor("value", ajAnnotations.pointcutAnnotation, pcLocation);
535 ISourceContext context = new EclipseSourceContext(unit.compilationResult, pcLocation[0]);
536 Pointcut pc = null;// abstract
537 if (pointcutExpression == null || pointcutExpression.length() == 0) {
538 noValueSupplied = true; // matches nothing pointcut
540 noValueSupplied = false;
541 pc = new PatternParser(pointcutExpression, context).parsePointcut();
542 if (pc instanceof IfPointcut) {
543 if (((IfPointcut)pc).alwaysFalse() || ((IfPointcut)pc).alwaysTrue()) {
544 isIfTrueOrFalse = true;
548 pcDecl.pointcutDesignator = (pc == null) ? null : new PointcutDesignator(pc);
549 pcDecl.setGenerateSyntheticPointcutMethod();
550 TypeDeclaration onType = (TypeDeclaration) typeStack.peek();
551 pcDecl.postParse(onType);
552 // EclipseFactory factory =
553 // EclipseFactory.fromScopeLookupEnvironment
554 // (methodDeclaration.scope);
555 // int argsLength = methodDeclaration.arguments == null ? 0 :
556 // methodDeclaration.arguments.length;
557 FormalBinding[] bindings = buildFormalAdviceBindingsFrom(methodDeclaration);
558 // FormalBinding[] bindings = new FormalBinding[argsLength];
559 // for (int i = 0, len = bindings.length; i < len; i++) {
560 // Argument arg = methodDeclaration.arguments[i];
561 // String name = new String(arg.name);
562 // UnresolvedType type =
563 // factory.fromBinding(methodDeclaration.binding.parameters[i]);
564 // bindings[i] = new FormalBinding(type, name, i, arg.sourceStart,
565 // arg.sourceEnd, "unknown");
567 swap(onType, methodDeclaration, pcDecl);
570 EclipseScope eScope = new EclipseScope(bindings, methodDeclaration.scope);
571 char[] packageName = null;
572 if (typeDecl.binding != null && typeDecl.binding.getPackage() != null) {
573 packageName = typeDecl.binding.getPackage().readableName();
575 eScope.setLimitedImports(packageName);
577 HasIfPCDVisitor ifFinder = new HasIfPCDVisitor();
578 pc.traverse(ifFinder, null);
579 containsIfPcd = ifFinder.containsIfPcd;
581 } catch (ParserException pEx) {
582 methodDeclaration.scope.problemReporter().parseError(pcLocation[0] + pEx.getLocation().getStart(),
583 pcLocation[0] + pEx.getLocation().getEnd(), -1, pointcutExpression.toCharArray(), pointcutExpression,
584 new String[] { pEx.getMessage() });
587 boolean returnsVoid = false;
588 boolean returnsBoolean = false;
589 if ((methodDeclaration.returnType instanceof SingleTypeReference)) {
590 SingleTypeReference retType = (SingleTypeReference) methodDeclaration.returnType;
591 if (CharOperation.equals(voidType, retType.token))
593 if (CharOperation.equals(booleanType, retType.token))
594 returnsBoolean = true;
596 if (!returnsVoid && !containsIfPcd) {
597 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.returnType.sourceStart,
598 methodDeclaration.returnType.sourceEnd,
599 "Methods annotated with @Pointcut must return void unless the pointcut contains an if() expression");
601 if (!returnsBoolean && containsIfPcd && !isIfTrueOrFalse) {
602 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.returnType.sourceStart,
603 methodDeclaration.returnType.sourceEnd,
604 "Methods annotated with @Pointcut must return boolean when the pointcut contains an if() expression unless it is if(false) or if(true)");
607 if (methodDeclaration.statements != null && methodDeclaration.statements.length > 0 && !containsIfPcd) {
608 methodDeclaration.scope.problemReporter()
609 .signalError(methodDeclaration.returnType.sourceStart, methodDeclaration.returnType.sourceEnd,
610 "Pointcuts without an if() expression should have an empty method body");
613 if (pcDecl.pointcutDesignator == null) {
614 if (Modifier.isAbstract(methodDeclaration.modifiers) || noValueSupplied // this
620 // those 2 checks makes sense for aop.xml concretization but NOT for
621 // regular abstraction of pointcut
623 // && (methodDeclaration.arguments == null ||
624 // methodDeclaration.arguments.length == 0)) {
628 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.returnType.sourceStart,
629 methodDeclaration.returnType.sourceEnd,
630 "Method annotated with @Pointcut() for abstract pointcut must be abstract");
632 } else if (Modifier.isAbstract(methodDeclaration.modifiers)) {
633 methodDeclaration.scope.problemReporter().signalError(methodDeclaration.returnType.sourceStart,
634 methodDeclaration.returnType.sourceEnd,
635 "Method annotated with non abstract @Pointcut(\"" + pointcutExpression + "\") is abstract");
639 private void copyAllFields(MethodDeclaration from, MethodDeclaration to) {
640 to.annotations = from.annotations;
641 to.arguments = from.arguments;
642 to.binding = from.binding;
644 to.bodyEnd = from.bodyEnd;
645 to.bodyStart = from.bodyStart;
646 to.declarationSourceEnd = from.declarationSourceEnd;
647 to.declarationSourceStart = from.declarationSourceStart;
648 to.explicitDeclarations = from.explicitDeclarations;
649 to.ignoreFurtherInvestigation = from.ignoreFurtherInvestigation;
650 to.javadoc = from.javadoc;
651 to.modifiers = from.modifiers;
652 to.modifiersSourceStart = from.modifiersSourceStart;
653 to.returnType = from.returnType;
654 to.scope = from.scope;
655 to.selector = from.selector;
656 to.sourceEnd = from.sourceEnd;
657 to.sourceStart = from.sourceStart;
658 to.statements = from.statements;
659 to.thrownExceptions = from.thrownExceptions;
660 to.typeParameters = from.typeParameters;
663 private void swap(TypeDeclaration inType, MethodDeclaration thisDeclaration, MethodDeclaration forThatDeclaration) {
664 for (int i = 0; i < inType.methods.length; i++) {
665 if (inType.methods[i] == thisDeclaration) {
666 inType.methods[i] = forThatDeclaration;
672 private static class AspectJAnnotations {
673 boolean hasAdviceAnnotation = false;
674 boolean hasPointcutAnnotation = false;
675 boolean hasAspectAnnotation = false;
676 boolean hasAdviceNameAnnotation = false;
677 boolean hasDeclareParents = false;
678 boolean hasMultipleAdviceAnnotations = false;
679 boolean hasMultiplePointcutAnnotations = false;
680 boolean hasMultipleAspectAnnotations = false;
682 AdviceKind adviceKind = null;
683 Annotation adviceAnnotation = null;
684 Annotation pointcutAnnotation = null;
685 Annotation aspectAnnotation = null;
686 Annotation adviceNameAnnotation = null;
688 Annotation duplicateAdviceAnnotation = null;
689 Annotation duplicatePointcutAnnotation = null;
690 Annotation duplicateAspectAnnotation = null;
692 public AspectJAnnotations(Annotation[] annotations) {
693 if (annotations == null)
695 for (Annotation annotation : annotations) {
696 if (annotation.resolvedType == null)
697 continue; // user messed up annotation declaration
698 char[] sig = annotation.resolvedType.signature();
699 if (CharOperation.equals(afterAdviceSig, sig)) {
700 adviceKind = AdviceKind.After;
701 addAdviceAnnotation(annotation);
702 } else if (CharOperation.equals(afterReturningAdviceSig, sig)) {
703 adviceKind = AdviceKind.AfterReturning;
704 addAdviceAnnotation(annotation);
705 } else if (CharOperation.equals(afterThrowingAdviceSig, sig)) {
706 adviceKind = AdviceKind.AfterThrowing;
707 addAdviceAnnotation(annotation);
708 } else if (CharOperation.equals(beforeAdviceSig, sig)) {
709 adviceKind = AdviceKind.Before;
710 addAdviceAnnotation(annotation);
711 } else if (CharOperation.equals(aroundAdviceSig, sig)) {
712 adviceKind = AdviceKind.Around;
713 addAdviceAnnotation(annotation);
714 } else if (CharOperation.equals(adviceNameSig, sig)) {
715 hasAdviceNameAnnotation = true;
716 adviceNameAnnotation = annotation;
717 } else if (CharOperation.equals(declareParentsSig, sig)) {
718 hasDeclareParents = true;
719 } else if (CharOperation.equals(aspectSig, sig)) {
720 if (hasAspectAnnotation) {
721 hasMultipleAspectAnnotations = true;
722 duplicateAspectAnnotation = annotation;
724 hasAspectAnnotation = true;
725 aspectAnnotation = annotation;
727 } else if (CharOperation.equals(pointcutSig, sig)) {
728 if (hasPointcutAnnotation) {
729 hasMultiplePointcutAnnotations = true;
730 duplicatePointcutAnnotation = annotation;
732 hasPointcutAnnotation = true;
733 pointcutAnnotation = annotation;
740 public boolean hasAspectJAnnotations() {
741 return hasAdviceAnnotation || hasPointcutAnnotation || hasAdviceNameAnnotation || hasAspectAnnotation;
744 private void addAdviceAnnotation(Annotation annotation) {
745 if (!hasAdviceAnnotation) {
746 hasAdviceAnnotation = true;
747 adviceAnnotation = annotation;
749 hasMultipleAdviceAnnotations = true;
750 duplicateAdviceAnnotation = annotation;
755 private static class HasIfPCDVisitor extends AbstractPatternNodeVisitor {
756 public boolean containsIfPcd = false;
758 public Object visit(IfPointcut node, Object data) {
759 containsIfPcd = true;