]> source.dussan.org Git - aspectj.git/blob
a2448f3141d1fb44540075f150f7dd9624b848b9
[aspectj.git] /
1 /* *******************************************************************
2  * Copyright (c) 2005 IBM Corporation.
3  * All rights reserved. 
4  * This program and the accompanying materials are made available 
5  * under the terms of the Eclipse Public License v1.0 
6  * which accompanies this distribution and is available at 
7  * http://www.eclipse.org/legal/epl-v10.html 
8  *  
9  * Contributors: 
10  *     Adrian Colyer initial implementation 
11  *      Andy Clement wired up to back end
12  * ******************************************************************/
13
14 package org.aspectj.ajdt.internal.compiler.ast;
15
16 import org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult;
17 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Annotation;
18 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation;
19 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
20 import org.aspectj.org.eclipse.jdt.internal.compiler.flow.FlowContext;
21 import org.aspectj.org.eclipse.jdt.internal.compiler.flow.FlowInfo;
22 import org.aspectj.org.eclipse.jdt.internal.compiler.flow.InitializationFlowContext;
23 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ClassScope;
24 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TagBits;
25 import org.aspectj.weaver.patterns.DeclareAnnotation;
26
27 public class DeclareAnnotationDeclaration extends DeclareDeclaration {
28
29         private Annotation annotation;
30         private boolean isRemover = false;
31
32         public DeclareAnnotationDeclaration(CompilationResult result, DeclareAnnotation symbolicDeclare, Annotation annotation) {
33                 super(result, symbolicDeclare);
34                 this.annotation = annotation;
35
36                 addAnnotation(annotation);
37                 if (symbolicDeclare == null) {
38                         return; // there is an error that will already be getting reported (e.g. incorrect pattern on decaf/decac)
39                 }
40                 this.isRemover = symbolicDeclare.isRemover();
41                 symbolicDeclare.setAnnotationString(annotation.toString());
42                 symbolicDeclare.setAnnotationLocation(annotation.sourceStart, annotation.sourceEnd);
43         }
44
45         @Override
46         public void analyseCode(ClassScope classScope, FlowContext flowContext, FlowInfo flowInfo) {
47                 super.analyseCode(classScope, flowContext, flowInfo);
48
49                 if (isRemover) {
50                         if (((DeclareAnnotation) declareDecl).getKind() != DeclareAnnotation.AT_FIELD) {
51                                 classScope.problemReporter().signalError(this.sourceStart(), this.sourceEnd,
52                                                 "Annotation removal only supported for declare @field (compiler limitation)");
53                         }
54                         else if (isRemover && !(annotation instanceof MarkerAnnotation)) {
55                                 classScope.problemReporter().signalError(this.sourceStart(), this.sourceEnd,
56                                                 "Annotation removal does not allow values to be specified for the annotation (compiler limitation)");
57                         }
58                 }
59                 long bits = annotation.resolvedType.getAnnotationTagBits();
60
61                 if ((bits & TagBits.AnnotationTarget) != 0) {
62                         // The annotation is stored against a method. For declare @type we need to
63                         // confirm the annotation targets the right types. Earlier checking will
64                         // have not found this problem because an annotation for target METHOD will
65                         // not be reported on as we *do* store it against a method in this case
66                         DeclareAnnotation.Kind k = ((DeclareAnnotation) declareDecl).getKind();
67                         if (k.equals(DeclareAnnotation.AT_TYPE)) {
68                                 if ((bits & TagBits.AnnotationForMethod) != 0) {
69                                         classScope.problemReporter().disallowedTargetForAnnotation(annotation);
70                                 }
71                         }
72                         if (k.equals(DeclareAnnotation.AT_FIELD)) {
73                                 if ((bits & TagBits.AnnotationForMethod) != 0) {
74                                         classScope.problemReporter().disallowedTargetForAnnotation(annotation);
75                                 }
76                         }
77                 }
78
79         }
80
81         public Annotation getDeclaredAnnotation() {
82                 return annotation;
83         }
84
85         protected boolean shouldDelegateCodeGeneration() {
86                 return true; // declare annotation needs a method to be written out.
87         }
88
89         protected boolean shouldBeSynthetic() {
90                 return false;
91         }
92
93         private void addAnnotation(Annotation ann) {
94                 if (this.annotations == null) {
95                         this.annotations = new Annotation[1];
96                 }
97                 else {
98                         Annotation[] old = this.annotations;
99                         this.annotations = new Annotation[old.length + 1];
100                         System.arraycopy(old, 0, this.annotations, 1, old.length);
101                 }
102                 this.annotations[0] = ann;
103         }
104
105         public void postParse(TypeDeclaration typeDec) {
106                 super.postParse(typeDec);
107                 if (declareDecl != null) {
108                         ((DeclareAnnotation) declareDecl).setAnnotationMethod(new String(selector));
109                 }
110         }
111
112         public boolean isRemover() {
113                 return isRemover;
114         }
115 }