KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.jdt.core.dom;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * Enumeration constant declaration AST node type (added in JLS3 API).
19  *
20  * <pre>
21  * EnumConstantDeclaration:
22  * [ Javadoc ] { ExtendedModifier } Identifier
23  * [ <b>(</b> [ Expression { <b>,</b> Expression } ] <b>)</b> ]
24  * [ AnonymousClassDeclaration ]
25  * </pre>
26  * <p>
27  * When a Javadoc comment is present, the source
28  * range begins with the first character of the "/**" comment delimiter.
29  * When there is no Javadoc comment, the source range begins with the first
30  * character of the identifier. If there are class body declarations, the
31  * source range extends through the last character of the last character of
32  * the "}" token following the body declarations. If there are arguments but
33  * no class body declarations, the source range extends through the last
34  * character of the ")" token following the arguments. If there are no
35  * arguments and no class body declarations, the source range extends through
36  * the last character of the identifier.
37  * </p>
38  *
39  * @since 3.1
40  */

41 public class EnumConstantDeclaration extends BodyDeclaration {
42     
43     /**
44      * The "javadoc" structural property of this node type.
45      */

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

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

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

61     /**
62      * The "arguments" structural property of this node type.
63      */

64     public static final ChildListPropertyDescriptor ARGUMENTS_PROPERTY =
65         new ChildListPropertyDescriptor(EnumConstantDeclaration.class, "arguments", Expression.class, NO_CYCLE_RISK); //$NON-NLS-1$
66

67     /**
68      * The "anonymousClassDeclaration" structural property of this node type.
69      */

70     public static final ChildPropertyDescriptor ANONYMOUS_CLASS_DECLARATION_PROPERTY =
71         new ChildPropertyDescriptor(EnumConstantDeclaration.class, "anonymousClassDeclaration", AnonymousClassDeclaration.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
72

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

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

101     public static List JavaDoc propertyDescriptors(int apiLevel) {
102         return PROPERTY_DESCRIPTORS;
103     }
104             
105     /**
106      * The constant name; lazily initialized; defaults to a unspecified,
107      * legal Java class identifier.
108      */

109     private SimpleName constantName = null;
110
111     /**
112      * The list of argument expressions (element type:
113      * <code>Expression</code>). Defaults to an empty list.
114      */

115     private ASTNode.NodeList arguments =
116         new ASTNode.NodeList(ARGUMENTS_PROPERTY);
117             
118     /**
119      * The optional anonymous class declaration; <code>null</code> for none;
120      * defaults to none.
121      */

122     private AnonymousClassDeclaration optionalAnonymousClassDeclaration = null;
123     
124     /**
125      * Creates a new AST node for an enumeration constants declaration owned by
126      * the given AST. By default, the enumeration constant has an unspecified,
127      * but legal, name; no javadoc; an empty list of modifiers and annotations;
128      * an empty list of arguments; and does not declare an anonymous class.
129      * <p>
130      * N.B. This constructor is package-private; all subclasses must be
131      * declared in the same package; clients are unable to declare
132      * additional subclasses.
133      * </p>
134      *
135      * @param ast the AST that is to own this node
136      */

137     EnumConstantDeclaration(AST ast) {
138         super(ast);
139         unsupportedIn2();
140     }
141
142     /* (omit javadoc for this method)
143      * Method declared on ASTNode.
144      */

145     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
146         return propertyDescriptors(apiLevel);
147     }
148     
149     /* (omit javadoc for this method)
150      * Method declared on ASTNode.
151      */

152     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
153         if (property == JAVADOC_PROPERTY) {
154             if (get) {
155                 return getJavadoc();
156             } else {
157                 setJavadoc((Javadoc) child);
158                 return null;
159             }
160         }
161         if (property == NAME_PROPERTY) {
162             if (get) {
163                 return getName();
164             } else {
165                 setName((SimpleName) child);
166                 return null;
167             }
168         }
169         if (property == ANONYMOUS_CLASS_DECLARATION_PROPERTY) {
170             if (get) {
171                 return getAnonymousClassDeclaration();
172             } else {
173                 setAnonymousClassDeclaration((AnonymousClassDeclaration) child);
174                 return null;
175             }
176         }
177         // allow default implementation to flag the error
178
return super.internalGetSetChildProperty(property, get, child);
179     }
180     
181     /* (omit javadoc for this method)
182      * Method declared on ASTNode.
183      */

184     final List JavaDoc internalGetChildListProperty(ChildListPropertyDescriptor property) {
185         if (property == MODIFIERS2_PROPERTY) {
186             return modifiers();
187         }
188         if (property == ARGUMENTS_PROPERTY) {
189             return arguments();
190         }
191         // allow default implementation to flag the error
192
return super.internalGetChildListProperty(property);
193     }
194     
195     /* (omit javadoc for this method)
196      * Method declared on BodyDeclaration.
197      */

198     final ChildPropertyDescriptor internalJavadocProperty() {
199         return JAVADOC_PROPERTY;
200     }
201
202     /* (omit javadoc for this method)
203      * Method declared on BodyDeclaration.
204      */

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

212     final SimplePropertyDescriptor internalModifiersProperty() {
213         // this property will not be asked for (node type did not exist in JLS2)
214
return null;
215     }
216
217     /* (omit javadoc for this method)
218      * Method declared on ASTNode.
219      */

220     final int getNodeType0() {
221         return ENUM_CONSTANT_DECLARATION;
222     }
223
224     /* (omit javadoc for this method)
225      * Method declared on ASTNode.
226      */

227     ASTNode clone0(AST target) {
228         EnumConstantDeclaration result = new EnumConstantDeclaration(target);
229         result.setSourceRange(this.getStartPosition(), this.getLength());
230         result.setJavadoc(
231             (Javadoc) ASTNode.copySubtree(target, getJavadoc()));
232         result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers()));
233         result.setName((SimpleName) getName().clone(target));
234         result.arguments().addAll(ASTNode.copySubtrees(target, arguments()));
235         result.setAnonymousClassDeclaration(
236                 (AnonymousClassDeclaration) ASTNode.copySubtree(target, getAnonymousClassDeclaration()));
237         return result;
238     }
239
240     /* (omit javadoc for this method)
241      * Method declared on ASTNode.
242      */

243     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
244         // dispatch to correct overloaded match method
245
return matcher.match(this, other);
246     }
247     
248     /* (omit javadoc for this method)
249      * Method declared on ASTNode.
250      */

251     void accept0(ASTVisitor visitor) {
252         boolean visitChildren = visitor.visit(this);
253         if (visitChildren) {
254             // visit children in normal left to right reading order
255
acceptChild(visitor, getJavadoc());
256             acceptChildren(visitor, this.modifiers);
257             acceptChild(visitor, getName());
258             acceptChildren(visitor, this.arguments);
259             acceptChild(visitor, getAnonymousClassDeclaration());
260         }
261         visitor.endVisit(this);
262     }
263     
264     /**
265      * Returns the name of the constant declared in this enum declaration.
266      *
267      * @return the constant name node
268      */

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

294     public void setName(SimpleName constantName) {
295         if (constantName == null) {
296             throw new IllegalArgumentException JavaDoc();
297         }
298         ASTNode oldChild = this.constantName;
299         preReplaceChild(oldChild, constantName, NAME_PROPERTY);
300         this.constantName = constantName;
301         postReplaceChild(oldChild, constantName, NAME_PROPERTY);
302     }
303
304     /**
305      * Returns the live ordered list of argument expressions in this enumeration
306      * constant declaration. Note that an empty list of arguments is equivalent
307      * to not explicitly specifying arguments.
308      *
309      * @return the live list of argument expressions
310      * (element type: <code>Expression</code>)
311      */

312     public List JavaDoc arguments() {
313         return this.arguments;
314     }
315
316     /**
317      * Returns the anonymous class declaration introduced by this
318      * enum constant declaration, if it has one.
319      *
320      * @return the anonymous class declaration, or <code>null</code> if none
321      */

322     public AnonymousClassDeclaration getAnonymousClassDeclaration() {
323         return this.optionalAnonymousClassDeclaration;
324     }
325     
326     /**
327      * Sets whether this enum constant declaration declares
328      * an anonymous class (that is, has class body declarations).
329      *
330      * @param decl the anonymous class declaration, or <code>null</code>
331      * if none
332      */

333     public void setAnonymousClassDeclaration(AnonymousClassDeclaration decl) {
334         ASTNode oldChild = this.optionalAnonymousClassDeclaration;
335         preReplaceChild(oldChild, decl, ANONYMOUS_CLASS_DECLARATION_PROPERTY);
336         this.optionalAnonymousClassDeclaration = decl;
337         postReplaceChild(oldChild, decl, ANONYMOUS_CLASS_DECLARATION_PROPERTY);
338     }
339     
340     /**
341      * Resolves and returns the binding for the constructor invoked by this
342      * enum constant.
343      * <p>
344      * Note that bindings are generally unavailable unless requested when the
345      * AST is being built.
346      * </p>
347      *
348      * @return the constructor binding, or <code>null</code> if the binding
349      * cannot be resolved
350      */

351     public IMethodBinding resolveConstructorBinding() {
352         return this.ast.getBindingResolver().resolveConstructor(this);
353     }
354
355     /**
356      * Resolves and returns the field binding for this enum constant.
357      * <p>
358      * Note that bindings are generally unavailable unless requested when the
359      * AST is being built.
360      * </p>
361      *
362      * @return the binding, or <code>null</code> if the binding cannot be
363      * resolved
364      */

365     public IVariableBinding resolveVariable() {
366         return this.ast.getBindingResolver().resolveVariable(this);
367     }
368         
369     /* (omit javadoc for this method)
370      * Method declared on ASTNode.
371      */

372     int memSize() {
373         return super.memSize() + 3 * 4;
374     }
375     
376     /* (omit javadoc for this method)
377      * Method declared on ASTNode.
378      */

379     int treeSize() {
380         return
381             memSize()
382             + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize())
383             + this.modifiers.listSize()
384             + (this.constantName == null ? 0 : getName().treeSize())
385             + this.arguments.listSize()
386             + (this.optionalAnonymousClassDeclaration == null ? 0 : getAnonymousClassDeclaration().treeSize());
387     }
388 }
389
390
Popular Tags