KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ast > AnnotationMethodDeclaration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.ast;
12
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.ClassFile;
15 import org.eclipse.jdt.internal.compiler.CompilationResult;
16 import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
17 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
18 import org.eclipse.jdt.internal.compiler.parser.Parser;
19
20 public class AnnotationMethodDeclaration extends MethodDeclaration {
21     
22     public Expression defaultValue;
23     public int extendedDimensions;
24
25     /**
26      * MethodDeclaration constructor comment.
27      */

28     public AnnotationMethodDeclaration(CompilationResult compilationResult) {
29         super(compilationResult);
30     }
31
32     public void generateCode(ClassFile classFile) {
33         classFile.generateMethodInfoHeader(this.binding);
34         int methodAttributeOffset = classFile.contentsOffset;
35         int attributeNumber = classFile.generateMethodInfoAttribute(this.binding, this);
36         classFile.completeMethodInfo(methodAttributeOffset, attributeNumber);
37     }
38     
39     public boolean isAnnotationMethod() {
40
41         return true;
42     }
43     
44     public boolean isMethod() {
45
46         return false;
47     }
48     
49     public void parseStatements(Parser parser, CompilationUnitDeclaration unit) {
50         // nothing to do
51
// annotation type member declaration don't have any body
52
}
53     
54     public StringBuffer JavaDoc print(int tab, StringBuffer JavaDoc output) {
55
56         printIndent(tab, output);
57         printModifiers(this.modifiers, output);
58         if (this.annotations != null) printAnnotations(this.annotations, output);
59         
60         TypeParameter[] typeParams = typeParameters();
61         if (typeParams != null) {
62             output.append('<');
63             int max = typeParams.length - 1;
64             for (int j = 0; j < max; j++) {
65                 typeParams[j].print(0, output);
66                 output.append(", ");//$NON-NLS-1$
67
}
68             typeParams[max].print(0, output);
69             output.append('>');
70         }
71         
72         printReturnType(0, output).append(this.selector).append('(');
73         if (this.arguments != null) {
74             for (int i = 0; i < this.arguments.length; i++) {
75                 if (i > 0) output.append(", "); //$NON-NLS-1$
76
this.arguments[i].print(0, output);
77             }
78         }
79         output.append(')');
80         if (this.thrownExceptions != null) {
81             output.append(" throws "); //$NON-NLS-1$
82
for (int i = 0; i < this.thrownExceptions.length; i++) {
83                 if (i > 0) output.append(", "); //$NON-NLS-1$
84
this.thrownExceptions[i].print(0, output);
85             }
86         }
87         
88         if (this.defaultValue != null) {
89             output.append(" default "); //$NON-NLS-1$
90
this.defaultValue.print(0, output);
91         }
92         
93         printBody(tab + 1, output);
94         return output;
95     }
96     
97     public void resolveStatements() {
98
99         super.resolveStatements();
100         if (this.arguments != null) {
101             scope.problemReporter().annotationMembersCannotHaveParameters(this);
102         }
103         if (this.typeParameters != null) {
104             scope.problemReporter().annotationMembersCannotHaveTypeParameters(this);
105         }
106         if (this.extendedDimensions != 0) {
107             scope.problemReporter().illegalExtendedDimensions(this);
108         }
109         if (this.binding == null) return;
110         TypeBinding returnTypeBinding = this.binding.returnType;
111         if (returnTypeBinding != null) {
112                 
113             // annotation methods can only return base types, String, Class, enum type, annotation types and arrays of these
114
checkAnnotationMethodType: {
115                 TypeBinding leafReturnType = returnTypeBinding.leafComponentType();
116                 if (returnTypeBinding.dimensions() <= 1) { // only 1-dimensional array permitted
117
switch (leafReturnType.erasure().id) {
118                         case T_byte :
119                         case T_short :
120                         case T_char :
121                         case T_int :
122                         case T_long :
123                         case T_float :
124                         case T_double :
125                         case T_boolean :
126                         case T_JavaLangString :
127                         case T_JavaLangClass :
128                             break checkAnnotationMethodType;
129                     }
130                     if (leafReturnType.isEnum() || leafReturnType.isAnnotationType())
131                         break checkAnnotationMethodType;
132                 }
133                 scope.problemReporter().invalidAnnotationMemberType(this);
134             }
135             if (this.defaultValue != null) {
136                 MemberValuePair pair = new MemberValuePair(this.selector, this.sourceStart, this.sourceEnd, this.defaultValue);
137                 pair.binding = this.binding;
138                 pair.resolveTypeExpecting(scope, returnTypeBinding);
139                 this.binding.setDefaultValue(org.eclipse.jdt.internal.compiler.lookup.ElementValuePair.getValue(this.defaultValue));
140             } else { // let it know it does not have a default value so it won't try to find it
141
this.binding.setDefaultValue(null);
142             }
143         }
144     }
145
146     public void traverse(
147         ASTVisitor visitor,
148         ClassScope classScope) {
149
150         if (visitor.visit(this, classScope)) {
151             if (this.annotations != null) {
152                 int annotationsLength = this.annotations.length;
153                 for (int i = 0; i < annotationsLength; i++)
154                     this.annotations[i].traverse(visitor, scope);
155             }
156             if (this.returnType != null) {
157                 this.returnType.traverse(visitor, scope);
158             }
159             if (this.defaultValue != null) {
160                 this.defaultValue.traverse(visitor, scope);
161             }
162         }
163         visitor.endVisit(this, classScope);
164     }
165 }
166
Popular Tags