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 *******************************************************************************/
11 package net.sourceforge.phpdt.core.dom;
13 import java.util.ArrayList;
14 import java.util.List;
17 * Enum declaration AST node type (added in JLS3 API).
21 * [ Javadoc ] { ExtendedModifier } <b>enum</b> Identifier
22 * [ <b>implements</b> Type { <b>,</b> Type } ]
24 * [ EnumConstantDeclaration { <b>,</b> EnumConstantDeclaration } ] [ <b>,</b> ]
25 * [ <b>;</b> { ClassBodyDeclaration | <b>;</b> } ]
28 * The {@link #enumConstants()} list holds the enum constant declarations,
29 * while the {@link #bodyDeclarations()} list holds the class body declarations
30 * that appear after the semicolon.
32 * When a Javadoc comment is present, the source
33 * range begins with the first character of the "/**" comment delimiter.
34 * When there is no Javadoc comment, the source range begins with the first
35 * character of the first modifier or annotation (if present), or the
36 * first character of the "enum" keyword (if no
37 * modifiers or annotations). The source range extends through the last
38 * character of the "}" token following the body declarations.
42 * @noinstantiate This class is not intended to be instantiated by clients.
44 public class EnumDeclaration extends AbstractTypeDeclaration {
47 * The "javadoc" structural property of this node type.
49 public static final ChildPropertyDescriptor JAVADOC_PROPERTY =
50 internalJavadocPropertyFactory(EnumDeclaration.class);
53 * The "modifiers" structural property of this node type (added in JLS3 API).
55 public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY =
56 internalModifiers2PropertyFactory(EnumDeclaration.class);
59 * The "name" structural property of this node type.
61 public static final ChildPropertyDescriptor NAME_PROPERTY =
62 internalNamePropertyFactory(EnumDeclaration.class);
65 * The "superInterfaceTypes" structural property of this node type.
67 public static final ChildListPropertyDescriptor SUPER_INTERFACE_TYPES_PROPERTY =
68 new ChildListPropertyDescriptor(EnumDeclaration.class, "superInterfaceTypes", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$
71 * The "enumConstants" structural property of this node type.
73 public static final ChildListPropertyDescriptor ENUM_CONSTANTS_PROPERTY =
74 new ChildListPropertyDescriptor(EnumDeclaration.class, "enumConstants", EnumConstantDeclaration.class, CYCLE_RISK); //$NON-NLS-1$
77 * The "bodyDeclarations" structural property of this node type.
79 public static final ChildListPropertyDescriptor BODY_DECLARATIONS_PROPERTY =
80 internalBodyDeclarationPropertyFactory(EnumDeclaration.class);
83 * A list of property descriptors (element type:
84 * {@link StructuralPropertyDescriptor}),
85 * or null if uninitialized.
87 private static final List PROPERTY_DESCRIPTORS;
90 List properyList = new ArrayList(6);
91 createPropertyList(EnumDeclaration.class, properyList);
92 addProperty(JAVADOC_PROPERTY, properyList);
93 addProperty(MODIFIERS2_PROPERTY, properyList);
94 addProperty(NAME_PROPERTY, properyList);
95 addProperty(SUPER_INTERFACE_TYPES_PROPERTY, properyList);
96 addProperty(ENUM_CONSTANTS_PROPERTY, properyList);
97 addProperty(BODY_DECLARATIONS_PROPERTY, properyList);
98 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
102 * Returns a list of structural property descriptors for this node type.
103 * Clients must not modify the result.
105 * @param apiLevel the API level; one of the
106 * <code>AST.JLS*</code> constants
108 * @return a list of property descriptors (element type:
109 * {@link StructuralPropertyDescriptor})
111 public static List propertyDescriptors(int apiLevel) {
112 return PROPERTY_DESCRIPTORS;
116 * The superinterface types (element type: <code>Type</code>).
117 * Defaults to an empty list.
119 private ASTNode.NodeList superInterfaceTypes =
120 new ASTNode.NodeList(SUPER_INTERFACE_TYPES_PROPERTY);
123 * The enum constant declarations
124 * (element type: <code>EnumConstantDeclaration</code>).
125 * Defaults to an empty list.
127 private ASTNode.NodeList enumConstants =
128 new ASTNode.NodeList(ENUM_CONSTANTS_PROPERTY);
131 * Creates a new AST node for an enum declaration owned by the given
132 * AST. By default, the enum declaration has an unspecified, but legal,
133 * name; no modifiers; no javadoc; no superinterfaces;
134 * and empty lists of enum constants and body declarations.
136 * N.B. This constructor is package-private; all subclasses must be
137 * declared in the same package; clients are unable to declare
138 * additional subclasses.
141 * @param ast the AST that is to own this node
143 EnumDeclaration(AST ast) {
148 /* (omit javadoc for this method)
149 * Method declared on ASTNode.
151 final List internalStructuralPropertiesForType(int apiLevel) {
152 return propertyDescriptors(apiLevel);
155 /* (omit javadoc for this method)
156 * Method declared on ASTNode.
158 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
159 if (property == JAVADOC_PROPERTY) {
163 setJavadoc((Javadoc) child);
167 if (property == NAME_PROPERTY) {
171 setName((SimpleName) child);
175 // allow default implementation to flag the error
176 return super.internalGetSetChildProperty(property, get, child);
179 /* (omit javadoc for this method)
180 * Method declared on ASTNode.
182 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
183 if (property == MODIFIERS2_PROPERTY) {
186 if (property == SUPER_INTERFACE_TYPES_PROPERTY) {
187 return superInterfaceTypes();
189 if (property == ENUM_CONSTANTS_PROPERTY) {
190 return enumConstants();
192 if (property == BODY_DECLARATIONS_PROPERTY) {
193 return bodyDeclarations();
195 // allow default implementation to flag the error
196 return super.internalGetChildListProperty(property);
199 /* (omit javadoc for this method)
200 * Method declared on BodyDeclaration.
202 final ChildPropertyDescriptor internalJavadocProperty() {
203 return JAVADOC_PROPERTY;
206 /* (omit javadoc for this method)
207 * Method declared on BodyDeclaration.
209 final ChildListPropertyDescriptor internalModifiers2Property() {
210 return MODIFIERS2_PROPERTY;
213 /* (omit javadoc for this method)
214 * Method declared on BodyDeclaration.
216 final SimplePropertyDescriptor internalModifiersProperty() {
217 // this property will not be asked for (node type did not exist in JLS2)
221 /* (omit javadoc for this method)
222 * Method declared on AbstractTypeDeclaration.
224 final ChildPropertyDescriptor internalNameProperty() {
225 return NAME_PROPERTY;
228 /* (omit javadoc for this method)
229 * Method declared on AbstractTypeDeclaration.
231 final ChildListPropertyDescriptor internalBodyDeclarationsProperty() {
232 return BODY_DECLARATIONS_PROPERTY;
235 /* (omit javadoc for this method)
236 * Method declared on ASTNode.
238 final int getNodeType0() {
239 return ENUM_DECLARATION;
242 /* (omit javadoc for this method)
243 * Method declared on ASTNode.
245 ASTNode clone0(AST target) {
246 EnumDeclaration result = new EnumDeclaration(target);
247 result.setSourceRange(this.getStartPosition(), this.getLength());
249 (Javadoc) ASTNode.copySubtree(target, getJavadoc()));
250 result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers()));
251 result.setName((SimpleName) getName().clone(target));
252 result.superInterfaceTypes().addAll(
253 ASTNode.copySubtrees(target, superInterfaceTypes()));
254 result.enumConstants().addAll(
255 ASTNode.copySubtrees(target, enumConstants()));
256 result.bodyDeclarations().addAll(
257 ASTNode.copySubtrees(target, bodyDeclarations()));
261 /* (omit javadoc for this method)
262 * Method declared on ASTNode.
264 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
265 // dispatch to correct overloaded match method
266 return matcher.match(this, other);
269 /* (omit javadoc for this method)
270 * Method declared on ASTNode.
272 void accept0(ASTVisitor visitor) {
273 boolean visitChildren = visitor.visit(this);
275 // visit children in normal left to right reading order
276 acceptChild(visitor, getJavadoc());
277 acceptChildren(visitor, this.modifiers);
278 acceptChild(visitor, getName());
279 acceptChildren(visitor, this.superInterfaceTypes);
280 acceptChildren(visitor, this.enumConstants);
281 acceptChildren(visitor, this.bodyDeclarations);
283 visitor.endVisit(this);
287 * Returns the live ordered list of superinterfaces of this enum
290 * @return the live list of super interface types
291 * (element type: <code>Type</code>)
293 public List superInterfaceTypes() {
294 return this.superInterfaceTypes;
298 * Returns the live ordered list of enum constant declarations
299 * of this enum declaration.
301 * @return the live list of enum constant declarations
302 * (element type: {@link EnumConstantDeclaration})
304 public List enumConstants() {
305 return enumConstants;
308 /* (omit javadoc for this method)
309 * Method declared on AsbtractTypeDeclaration.
311 ITypeBinding internalResolveBinding() {
312 return this.ast.getBindingResolver().resolveType(this);
315 /* (omit javadoc for this method)
316 * Method declared on ASTNode.
319 return super.memSize() + 2 * 4;
322 /* (omit javadoc for this method)
323 * Method declared on ASTNode.
327 + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize())
328 + this.modifiers.listSize()
329 + (this.typeName == null ? 0 : getName().treeSize())
330 + this.superInterfaceTypes.listSize()
331 + this.enumConstants.listSize()
332 + this.bodyDeclarations.listSize();