1 /* *******************************************************************
2 * Copyright (c) 2005 IBM Corporation.
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
10 * Adrian Colyer initial implementation
11 * Andy Clement wired up to back end
12 * ******************************************************************/
14 package org.aspectj.ajdt.internal.compiler.ast;
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;
27 public class DeclareAnnotationDeclaration extends DeclareDeclaration {
29 private Annotation annotation;
30 private boolean isRemover = false;
32 public DeclareAnnotationDeclaration(CompilationResult result, DeclareAnnotation symbolicDeclare, Annotation annotation) {
33 super(result, symbolicDeclare);
34 this.annotation = annotation;
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)
40 this.isRemover = symbolicDeclare.isRemover();
41 symbolicDeclare.setAnnotationString(annotation.toString());
42 symbolicDeclare.setAnnotationLocation(annotation.sourceStart, annotation.sourceEnd);
46 public void analyseCode(ClassScope classScope, FlowContext flowContext, FlowInfo flowInfo) {
47 super.analyseCode(classScope, flowContext, flowInfo);
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)");
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)");
59 long bits = annotation.resolvedType.getAnnotationTagBits();
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);
72 if (k.equals(DeclareAnnotation.AT_FIELD)) {
73 if ((bits & TagBits.AnnotationForMethod) != 0) {
74 classScope.problemReporter().disallowedTargetForAnnotation(annotation);
81 public Annotation getDeclaredAnnotation() {
85 protected boolean shouldDelegateCodeGeneration() {
86 return true; // declare annotation needs a method to be written out.
89 protected boolean shouldBeSynthetic() {
93 private void addAnnotation(Annotation ann) {
94 if (this.annotations == null) {
95 this.annotations = new Annotation[1];
98 Annotation[] old = this.annotations;
99 this.annotations = new Annotation[old.length + 1];
100 System.arraycopy(old, 0, this.annotations, 1, old.length);
102 this.annotations[0] = ann;
105 public void postParse(TypeDeclaration typeDec) {
106 super.postParse(typeDec);
107 if (declareDecl != null) {
108 ((DeclareAnnotation) declareDecl).setAnnotationMethod(new String(selector));
112 public boolean isRemover() {