1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
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;
20 public class AnnotationMethodDeclaration extends MethodDeclaration {
22 public Expression defaultValue;
23 public int extendedDimensions;
26 * MethodDeclaration constructor comment.
28 public AnnotationMethodDeclaration(CompilationResult compilationResult) {
29 super(compilationResult);
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);
39 public boolean isAnnotationMethod() {
44 public boolean isMethod() {
49 public void parseStatements(Parser parser, CompilationUnitDeclaration unit) {
51 // annotation type member declaration don't have any body
54 public StringBuffer print(int tab, StringBuffer output) {
56 printIndent(tab, output);
57 printModifiers(this.modifiers, output);
58 if (this.annotations != null) printAnnotations(this.annotations, output);
60 TypeParameter[] typeParams = typeParameters();
61 if (typeParams != null) {
62 output.append('<');//$NON-NLS-1$
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$
68 typeParams[max].print(0, output);
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);
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);
88 if (this.defaultValue != null) {
89 output.append(" default "); //$NON-NLS-1$
90 this.defaultValue.print(0, output);
93 printBody(tab + 1, output);
97 public void resolveStatements() {
99 super.resolveStatements();
100 if (this.arguments != null) {
101 scope.problemReporter().annotationMembersCannotHaveParameters(this);
103 if (this.typeParameters != null) {
104 scope.problemReporter().annotationMembersCannotHaveTypeParameters(this);
106 if (this.extendedDimensions != 0) {
107 scope.problemReporter().illegalExtendedDimensions(this);
109 if (this.binding == null) return;
110 TypeBinding returnTypeBinding = this.binding.returnType;
111 if (returnTypeBinding != null) {
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();
117 switch (leafReturnType.erasure().id) {
126 case T_JavaLangString :
127 case T_JavaLangClass :
128 if (returnTypeBinding.dimensions() <= 1) // only 1-dimensional array permitted
129 break checkAnnotationMethodType;
131 if (leafReturnType.isEnum()) {
132 if (returnTypeBinding.dimensions() <= 1) // only 1-dimensional array permitted
133 break checkAnnotationMethodType;
135 if (leafReturnType.isAnnotationType()) {
136 scope.classScope().detectAnnotationCycle(scope.enclosingSourceType(), leafReturnType, this.returnType);
137 if (returnTypeBinding.dimensions() <= 1) // only 1-dimensional array permitted
138 break checkAnnotationMethodType;
140 scope.problemReporter().invalidAnnotationMemberType(this);
142 if (this.defaultValue != null) {
143 MemberValuePair pair = new MemberValuePair(this.selector, this.sourceStart, this.sourceEnd, this.defaultValue);
144 pair.binding = this.binding;
145 pair.resolveTypeExpecting(scope, returnTypeBinding);
150 public void traverse(
152 ClassScope classScope) {
154 if (visitor.visit(this, classScope)) {
155 if (this.annotations != null) {
156 int annotationsLength = this.annotations.length;
157 for (int i = 0; i < annotationsLength; i++)
158 this.annotations[i].traverse(visitor, scope);
160 if (this.returnType != null) {
161 this.returnType.traverse(visitor, scope);
163 if (this.defaultValue != null) {
164 this.defaultValue.traverse(visitor, scope);
167 visitor.endVisit(this, classScope);