KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > AnnotationTypeMemberDeclaration


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.core.dom;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 /**
17  * Annotation type member declaration AST node type (added in JLS3 API).
18  * <pre>
19  * AnnotationTypeMemberDeclaration:
20  * [ Javadoc ] { ExtendedModifier }
21  * Type Identifier <b>(</b> <b>)</b> [ <b>default</b> Expression ] <b>;</b>
22  * </pre>
23  * <p>
24  * Note that annotation type member declarations are only meaningful as
25  * elements of {@link AnnotationTypeDeclaration#bodyDeclarations()}.
26  * </p>
27  * <p>
28  * When a Javadoc comment is present, the source
29  * range begins with the first character of the "/**" comment delimiter.
30  * When there is no Javadoc comment, the source range begins with the first
31  * character of the first modifier keyword (if modifiers),
32  * or the first character of the member type (no modifiers).
33  * The source range extends through the last character of the
34  * ";" token.
35  * </p>
36  *
37  * @since 3.1
38  */

39 public class AnnotationTypeMemberDeclaration extends BodyDeclaration {
40     
41     /**
42      * The "javadoc" structural property of this node type.
43      */

44     public static final ChildPropertyDescriptor JAVADOC_PROPERTY =
45         internalJavadocPropertyFactory(AnnotationTypeMemberDeclaration.class);
46
47     /**
48      * The "modifiers" structural property of this node type.
49      */

50     public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY =
51         internalModifiers2PropertyFactory(AnnotationTypeMemberDeclaration.class);
52     
53     /**
54      * The "name" structural property of this node type.
55      */

56     public static final ChildPropertyDescriptor NAME_PROPERTY =
57         new ChildPropertyDescriptor(AnnotationTypeMemberDeclaration.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
58

59     /**
60      * The "type" structural property of this node type.
61      */

62     public static final ChildPropertyDescriptor TYPE_PROPERTY =
63         new ChildPropertyDescriptor(AnnotationTypeMemberDeclaration.class, "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
64

65     /**
66      * The "default" structural property of this node type.
67      */

68     public static final ChildPropertyDescriptor DEFAULT_PROPERTY =
69         new ChildPropertyDescriptor(AnnotationTypeMemberDeclaration.class, "default", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
70

71     /**
72      * A list of property descriptors (element type:
73      * {@link StructuralPropertyDescriptor}),
74      * or null if uninitialized.
75      */

76     private static final List JavaDoc PROPERTY_DESCRIPTORS;
77     
78     static {
79         List JavaDoc properyList = new ArrayList JavaDoc(6);
80         createPropertyList(AnnotationTypeMemberDeclaration.class, properyList);
81         addProperty(JAVADOC_PROPERTY, properyList);
82         addProperty(MODIFIERS2_PROPERTY, properyList);
83         addProperty(NAME_PROPERTY, properyList);
84         addProperty(TYPE_PROPERTY, properyList);
85         addProperty(DEFAULT_PROPERTY, properyList);
86         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
87     }
88
89     /**
90      * Returns a list of structural property descriptors for this node type.
91      * Clients must not modify the result.
92      *
93      * @param apiLevel the API level; one of the
94      * <code>AST.JLS*</code> constants
95      * @return a list of property descriptors (element type:
96      * {@link StructuralPropertyDescriptor})
97      */

98     public static List JavaDoc propertyDescriptors(int apiLevel) {
99         return PROPERTY_DESCRIPTORS;
100     }
101                         
102     /**
103      * The member name; lazily initialized; defaults to an unspecified,
104      * legal Java identifier.
105      */

106     private SimpleName memberName = null;
107
108     /**
109      * The member type; lazily initialized; defaults to int.
110      */

111     private Type memberType = null;
112     
113     /**
114      * The optional default expression; <code>null</code> for none; defaults to none.
115      */

116     private Expression optionalDefaultValue = null;
117     
118     /**
119      * Creates a new AST node for an annotation type member declaration owned
120      * by the given AST. By default, the declaration is for a member of an
121      * unspecified, but legal, name; no modifiers; no javadoc;
122      * an unspecified value type; and no default value.
123      * <p>
124      * N.B. This constructor is package-private; all subclasses must be
125      * declared in the same package; clients are unable to declare
126      * additional subclasses.
127      * </p>
128      *
129      * @param ast the AST that is to own this node
130      */

131     AnnotationTypeMemberDeclaration(AST ast) {
132         super(ast);
133         unsupportedIn2();
134     }
135
136     /* (omit javadoc for this method)
137      * Method declared on ASTNode.
138      */

139     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
140         return propertyDescriptors(apiLevel);
141     }
142     
143     /* (omit javadoc for this method)
144      * Method declared on ASTNode.
145      */

146     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
147         if (property == JAVADOC_PROPERTY) {
148             if (get) {
149                 return getJavadoc();
150             } else {
151                 setJavadoc((Javadoc) child);
152                 return null;
153             }
154         }
155         if (property == NAME_PROPERTY) {
156             if (get) {
157                 return getName();
158             } else {
159                 setName((SimpleName) child);
160                 return null;
161             }
162         }
163         if (property == NAME_PROPERTY) {
164             if (get) {
165                 return getName();
166             } else {
167                 setName((SimpleName) child);
168                 return null;
169             }
170         }
171         if (property == TYPE_PROPERTY) {
172             if (get) {
173                 return getType();
174             } else {
175                 setType((Type) child);
176                 return null;
177             }
178         }
179         if (property == DEFAULT_PROPERTY) {
180             if (get) {
181                 return getDefault();
182             } else {
183                 setDefault((Expression) child);
184                 return null;
185             }
186         }
187         // allow default implementation to flag the error
188
return super.internalGetSetChildProperty(property, get, child);
189     }
190     
191     /* (omit javadoc for this method)
192      * Method declared on ASTNode.
193      */

194     final List JavaDoc internalGetChildListProperty(ChildListPropertyDescriptor property) {
195         if (property == MODIFIERS2_PROPERTY) {
196             return modifiers();
197         }
198         // allow default implementation to flag the error
199
return super.internalGetChildListProperty(property);
200     }
201     
202     /* (omit javadoc for this method)
203      * Method declared on BodyDeclaration.
204      */

205     final ChildPropertyDescriptor internalJavadocProperty() {
206         return JAVADOC_PROPERTY;
207     }
208
209     /* (omit javadoc for this method)
210      * Method declared on BodyDeclaration.
211      */

212     final ChildListPropertyDescriptor internalModifiers2Property() {
213         return MODIFIERS2_PROPERTY;
214     }
215
216     /* (omit javadoc for this method)
217      * Method declared on BodyDeclaration.
218      */

219     final SimplePropertyDescriptor internalModifiersProperty() {
220         // this property will not be asked for (node type did not exist in JLS2)
221
return null;
222     }
223
224     /* (omit javadoc for this method)
225      * Method declared on ASTNode.
226      */

227     final int getNodeType0() {
228         return ANNOTATION_TYPE_MEMBER_DECLARATION;
229     }
230
231     /* (omit javadoc for this method)
232      * Method declared on ASTNode.
233      */

234     ASTNode clone0(AST target) {
235         AnnotationTypeMemberDeclaration result = new AnnotationTypeMemberDeclaration(target);
236         result.setSourceRange(this.getStartPosition(), this.getLength());
237         result.setJavadoc(
238             (Javadoc) ASTNode.copySubtree(target, getJavadoc()));
239         result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers()));
240         result.setType((Type) ASTNode.copySubtree(target, getType()));
241         result.setName((SimpleName) getName().clone(target));
242         result.setDefault((Expression) ASTNode.copySubtree(target, getDefault()));
243         return result;
244     }
245
246     /* (omit javadoc for this method)
247      * Method declared on ASTNode.
248      */

249     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
250         // dispatch to correct overloaded match method
251
return matcher.match(this, other);
252     }
253     
254     /* (omit javadoc for this method)
255      * Method declared on ASTNode.
256      */

257     void accept0(ASTVisitor visitor) {
258         boolean visitChildren = visitor.visit(this);
259         if (visitChildren) {
260             // visit children in normal left to right reading order
261
acceptChild(visitor, getJavadoc());
262             acceptChildren(visitor, this.modifiers);
263             acceptChild(visitor, getType());
264             acceptChild(visitor, getName());
265             acceptChild(visitor, getDefault());
266         }
267         visitor.endVisit(this);
268     }
269     
270     /**
271      * Returns the name of the annotation type member declared in this declaration.
272      *
273      * @return the member name node
274      */

275     public SimpleName getName() {
276         if (this.memberName == null) {
277             // lazy init must be thread-safe for readers
278
synchronized (this) {
279                 if (this.memberName == null) {
280                     preLazyInit();
281                     this.memberName = new SimpleName(this.ast);
282                     postLazyInit(this.memberName, NAME_PROPERTY);
283                 }
284             }
285         }
286         return this.memberName;
287     }
288     
289     /**
290      * Sets the name of the annotation type member declared in this declaration to the
291      * given name.
292      *
293      * @param memberName the new member name
294      * @exception IllegalArgumentException if:
295      * <ul>
296      * <li>the node belongs to a different AST</li>
297      * <li>the node already has a parent</li>
298      * </ul>
299      */

300     public void setName(SimpleName memberName) {
301         if (memberName == null) {
302             throw new IllegalArgumentException JavaDoc();
303         }
304         ASTNode oldChild = this.memberName;
305         preReplaceChild(oldChild, memberName, NAME_PROPERTY);
306         this.memberName = memberName;
307         postReplaceChild(oldChild, memberName, NAME_PROPERTY);
308     }
309
310     /**
311      * Returns the type of the annotation type member declared in this
312      * declaration.
313      *
314      * @return the type of the member
315      */

316     public Type getType() {
317         if (this.memberType == null) {
318             // lazy init must be thread-safe for readers
319
synchronized (this) {
320                 if (this.memberType == null) {
321                     preLazyInit();
322                     this.memberType = this.ast.newPrimitiveType(PrimitiveType.INT);
323                     postLazyInit(this.memberType, TYPE_PROPERTY);
324                 }
325             }
326         }
327         return this.memberType;
328     }
329
330     /**
331      * Sets the type of the annotation type member declared in this declaration
332      * to the given type.
333      *
334      * @param type the new member type
335      * @exception IllegalArgumentException if:
336      * <ul>
337      * <li>the node belongs to a different AST</li>
338      * <li>the node already has a parent</li>
339      * </ul>
340      */

341     public void setType(Type type) {
342         if (type == null) {
343             throw new IllegalArgumentException JavaDoc();
344         }
345         ASTNode oldChild = this.memberType;
346         preReplaceChild(oldChild, type, TYPE_PROPERTY);
347         this.memberType = type;
348         postReplaceChild(oldChild, type, TYPE_PROPERTY);
349     }
350
351     /**
352      * Returns the default value of this annotation type member, or
353      * <code>null</code> if there is none.
354      *
355      * @return the expression node, or <code>null</code> if there is none
356      */

357     public Expression getDefault() {
358         return this.optionalDefaultValue;
359     }
360     
361     /**
362      * Sets or clears the default value of this annotation type member.
363      *
364      * @param defaultValue the expression node, or <code>null</code> if
365      * there is none
366      * @exception IllegalArgumentException if:
367      * <ul>
368      * <li>the node belongs to a different AST</li>
369      * <li>the node already has a parent</li>
370      * <li>a cycle in would be created</li>
371      * </ul>
372      */

373     public void setDefault(Expression defaultValue) {
374         // a AnnotationTypeMemberDeclaration may occur inside an Expression - must check cycles
375
ASTNode oldChild = this.optionalDefaultValue;
376         preReplaceChild(oldChild, defaultValue, DEFAULT_PROPERTY);
377         this.optionalDefaultValue = defaultValue;
378         postReplaceChild(oldChild, defaultValue, DEFAULT_PROPERTY);
379     }
380     
381     /**
382      * Resolves and returns the binding for the annotation type member declared
383      * in this declaration.
384      * <p>
385      * Note that bindings are generally unavailable unless requested when the
386      * AST is being built.
387      * </p>
388      *
389      * @return the binding, or <code>null</code> if the binding cannot be
390      * resolved
391      */

392     public IMethodBinding resolveBinding() {
393         return this.ast.getBindingResolver().resolveMember(this);
394     }
395
396     /* (omit javadoc for this method)
397      * Method declared on ASTNode.
398      */

399     int memSize() {
400         return super.memSize() + 3 * 4;
401     }
402     
403     /* (omit javadoc for this method)
404      * Method declared on ASTNode.
405      */

406     int treeSize() {
407         return
408             memSize()
409             + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize())
410             + this.modifiers.listSize()
411             + (this.memberName == null ? 0 : getName().treeSize())
412             + (this.memberType == null ? 0 : getType().treeSize())
413             + (this.optionalDefaultValue == null ? 0 : getDefault().treeSize());
414     }
415 }
416
417
Popular Tags