1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
18 * Enumeration constant declaration AST node type (added in JLS3 API).
21 * EnumConstantDeclaration:
22 * [ Javadoc ] { ExtendedModifier } Identifier
23 * [ <b>(</b> [ Expression { <b>,</b> Expression } ] <b>)</b> ]
24 * [ AnonymousClassDeclaration ]
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.
40 * @noinstantiate This class is not intended to be instantiated by clients.
42 public class EnumConstantDeclaration extends BodyDeclaration {
45 * The "javadoc" structural property of this node type.
47 public static final ChildPropertyDescriptor JAVADOC_PROPERTY =
48 internalJavadocPropertyFactory(EnumConstantDeclaration.class);
51 * The "modifiers" structural property of this node type).
53 public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY =
54 internalModifiers2PropertyFactory(EnumConstantDeclaration.class);
57 * The "name" structural property of this node type.
59 public static final ChildPropertyDescriptor NAME_PROPERTY =
60 new ChildPropertyDescriptor(EnumConstantDeclaration.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
63 * The "arguments" structural property of this node type.
65 public static final ChildListPropertyDescriptor ARGUMENTS_PROPERTY =
66 new ChildListPropertyDescriptor(EnumConstantDeclaration.class, "arguments", Expression.class, NO_CYCLE_RISK); //$NON-NLS-1$
69 * The "anonymousClassDeclaration" structural property of this node type.
71 public static final ChildPropertyDescriptor ANONYMOUS_CLASS_DECLARATION_PROPERTY =
72 new ChildPropertyDescriptor(EnumConstantDeclaration.class, "anonymousClassDeclaration", AnonymousClassDeclaration.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
75 * A list of property descriptors (element type:
76 * {@link StructuralPropertyDescriptor}),
77 * or null if uninitialized.
79 private static final List PROPERTY_DESCRIPTORS;
82 List properyList = new ArrayList(6);
83 createPropertyList(EnumConstantDeclaration.class, properyList);
84 addProperty(JAVADOC_PROPERTY, properyList);
85 addProperty(MODIFIERS2_PROPERTY, properyList);
86 addProperty(NAME_PROPERTY, properyList);
87 addProperty(ARGUMENTS_PROPERTY, properyList);
88 addProperty(ANONYMOUS_CLASS_DECLARATION_PROPERTY, properyList);
89 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
93 * Returns a list of structural property descriptors for this node type.
94 * Clients must not modify the result.
96 * @param apiLevel the API level; one of the
97 * <code>AST.JLS*</code> constants
99 * @return a list of property descriptors (element type:
100 * {@link StructuralPropertyDescriptor})
102 public static List propertyDescriptors(int apiLevel) {
103 return PROPERTY_DESCRIPTORS;
107 * The constant name; lazily initialized; defaults to a unspecified,
108 * legal Java class identifier.
110 private SimpleName constantName = null;
113 * The list of argument expressions (element type:
114 * <code>Expression</code>). Defaults to an empty list.
116 private ASTNode.NodeList arguments =
117 new ASTNode.NodeList(ARGUMENTS_PROPERTY);
120 * The optional anonymous class declaration; <code>null</code> for none;
123 private AnonymousClassDeclaration optionalAnonymousClassDeclaration = null;
126 * Creates a new AST node for an enumeration constants declaration owned by
127 * the given AST. By default, the enumeration constant has an unspecified,
128 * but legal, name; no javadoc; an empty list of modifiers and annotations;
129 * an empty list of arguments; and does not declare an anonymous class.
131 * N.B. This constructor is package-private; all subclasses must be
132 * declared in the same package; clients are unable to declare
133 * additional subclasses.
136 * @param ast the AST that is to own this node
138 EnumConstantDeclaration(AST ast) {
143 /* (omit javadoc for this method)
144 * Method declared on ASTNode.
146 final List internalStructuralPropertiesForType(int apiLevel) {
147 return propertyDescriptors(apiLevel);
150 /* (omit javadoc for this method)
151 * Method declared on ASTNode.
153 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
154 if (property == JAVADOC_PROPERTY) {
158 setJavadoc((Javadoc) child);
162 if (property == NAME_PROPERTY) {
166 setName((SimpleName) child);
170 if (property == ANONYMOUS_CLASS_DECLARATION_PROPERTY) {
172 return getAnonymousClassDeclaration();
174 setAnonymousClassDeclaration((AnonymousClassDeclaration) child);
178 // allow default implementation to flag the error
179 return super.internalGetSetChildProperty(property, get, child);
182 /* (omit javadoc for this method)
183 * Method declared on ASTNode.
185 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
186 if (property == MODIFIERS2_PROPERTY) {
189 if (property == ARGUMENTS_PROPERTY) {
192 // allow default implementation to flag the error
193 return super.internalGetChildListProperty(property);
196 /* (omit javadoc for this method)
197 * Method declared on BodyDeclaration.
199 final ChildPropertyDescriptor internalJavadocProperty() {
200 return JAVADOC_PROPERTY;
203 /* (omit javadoc for this method)
204 * Method declared on BodyDeclaration.
206 final ChildListPropertyDescriptor internalModifiers2Property() {
207 return MODIFIERS2_PROPERTY;
210 /* (omit javadoc for this method)
211 * Method declared on BodyDeclaration.
213 final SimplePropertyDescriptor internalModifiersProperty() {
214 // this property will not be asked for (node type did not exist in JLS2)
218 /* (omit javadoc for this method)
219 * Method declared on ASTNode.
221 final int getNodeType0() {
222 return ENUM_CONSTANT_DECLARATION;
225 /* (omit javadoc for this method)
226 * Method declared on ASTNode.
228 ASTNode clone0(AST target) {
229 EnumConstantDeclaration result = new EnumConstantDeclaration(target);
230 result.setSourceRange(this.getStartPosition(), this.getLength());
232 (Javadoc) ASTNode.copySubtree(target, getJavadoc()));
233 result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers()));
234 result.setName((SimpleName) getName().clone(target));
235 result.arguments().addAll(ASTNode.copySubtrees(target, arguments()));
236 result.setAnonymousClassDeclaration(
237 (AnonymousClassDeclaration) ASTNode.copySubtree(target, getAnonymousClassDeclaration()));
241 /* (omit javadoc for this method)
242 * Method declared on ASTNode.
244 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
245 // dispatch to correct overloaded match method
246 return matcher.match(this, other);
249 /* (omit javadoc for this method)
250 * Method declared on ASTNode.
252 void accept0(ASTVisitor visitor) {
253 boolean visitChildren = visitor.visit(this);
255 // visit children in normal left to right reading order
256 acceptChild(visitor, getJavadoc());
257 acceptChildren(visitor, this.modifiers);
258 acceptChild(visitor, getName());
259 acceptChildren(visitor, this.arguments);
260 acceptChild(visitor, getAnonymousClassDeclaration());
262 visitor.endVisit(this);
266 * Returns the name of the constant declared in this enum declaration.
268 * @return the constant name node
270 public SimpleName getName() {
271 if (this.constantName == null) {
272 // lazy init must be thread-safe for readers
273 synchronized (this) {
274 if (this.constantName == null) {
276 this.constantName = new SimpleName(this.ast);
277 postLazyInit(this.constantName, NAME_PROPERTY);
281 return this.constantName;
285 * Sets the name of the constant declared in this enum declaration to the
288 * @param constantName the new constant name
289 * @exception IllegalArgumentException if:
291 * <li>the node belongs to a different AST</li>
292 * <li>the node already has a parent</li>
295 public void setName(SimpleName constantName) {
296 if (constantName == null) {
297 throw new IllegalArgumentException();
299 ASTNode oldChild = this.constantName;
300 preReplaceChild(oldChild, constantName, NAME_PROPERTY);
301 this.constantName = constantName;
302 postReplaceChild(oldChild, constantName, NAME_PROPERTY);
306 * Returns the live ordered list of argument expressions in this enumeration
307 * constant declaration. Note that an empty list of arguments is equivalent
308 * to not explicitly specifying arguments.
310 * @return the live list of argument expressions
311 * (element type: <code>Expression</code>)
313 public List arguments() {
314 return this.arguments;
318 * Returns the anonymous class declaration introduced by this
319 * enum constant declaration, if it has one.
321 * @return the anonymous class declaration, or <code>null</code> if none
323 public AnonymousClassDeclaration getAnonymousClassDeclaration() {
324 return this.optionalAnonymousClassDeclaration;
328 * Sets whether this enum constant declaration declares
329 * an anonymous class (that is, has class body declarations).
331 * @param decl the anonymous class declaration, or <code>null</code>
334 public void setAnonymousClassDeclaration(AnonymousClassDeclaration decl) {
335 ASTNode oldChild = this.optionalAnonymousClassDeclaration;
336 preReplaceChild(oldChild, decl, ANONYMOUS_CLASS_DECLARATION_PROPERTY);
337 this.optionalAnonymousClassDeclaration = decl;
338 postReplaceChild(oldChild, decl, ANONYMOUS_CLASS_DECLARATION_PROPERTY);
342 * Resolves and returns the binding for the constructor invoked by this
345 * Note that bindings are generally unavailable unless requested when the
346 * AST is being built.
349 * @return the constructor binding, or <code>null</code> if the binding
352 public IMethodBinding resolveConstructorBinding() {
353 return this.ast.getBindingResolver().resolveConstructor(this);
357 * Resolves and returns the field binding for this enum constant.
359 * Note that bindings are generally unavailable unless requested when the
360 * AST is being built.
363 * @return the binding, or <code>null</code> if the binding cannot be
366 public IVariableBinding resolveVariable() {
367 return this.ast.getBindingResolver().resolveVariable(this);
370 /* (omit javadoc for this method)
371 * Method declared on ASTNode.
374 return super.memSize() + 3 * 4;
377 /* (omit javadoc for this method)
378 * Method declared on ASTNode.
383 + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize())
384 + this.modifiers.listSize()
385 + (this.constantName == null ? 0 : getName().treeSize())
386 + this.arguments.listSize()
387 + (this.optionalAnonymousClassDeclaration == null ? 0 : getAnonymousClassDeclaration().treeSize());