001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.core.dom;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: /**
016: * Enum declaration AST node type (added in JLS3 API).
017: *
018: * <pre>
019: * EnumDeclaration:
020: * [ Javadoc ] { ExtendedModifier } <b>enum</b> Identifier
021: * [ <b>implements</b> Type { <b>,</b> Type } ]
022: * <b>{</b>
023: * [ EnumConstantDeclaration { <b>,</b> EnumConstantDeclaration } ] [ <b>,</b> ]
024: * [ <b>;</b> { ClassBodyDeclaration | <b>;</b> } ]
025: * <b>}</b>
026: * </pre>
027: * The {@link #enumConstants()} list holds the enum constant declarations,
028: * while the {@link #bodyDeclarations()} list holds the class body declarations
029: * that appear after the semicolon.
030: * <p>
031: * When a Javadoc comment is present, the source
032: * range begins with the first character of the "/**" comment delimiter.
033: * When there is no Javadoc comment, the source range begins with the first
034: * character of the first modifier or annotation (if present), or the
035: * first character of the "enum" keyword (if no
036: * modifiers or annotations). The source range extends through the last
037: * character of the "}" token following the body declarations.
038: * </p>
039: *
040: * @since 3.1
041: */
042: public class EnumDeclaration extends AbstractTypeDeclaration {
043:
044: /**
045: * The "javadoc" structural property of this node type.
046: */
047: public static final ChildPropertyDescriptor JAVADOC_PROPERTY = internalJavadocPropertyFactory(EnumDeclaration.class);
048:
049: /**
050: * The "modifiers" structural property of this node type (added in JLS3 API).
051: */
052: public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY = internalModifiers2PropertyFactory(EnumDeclaration.class);
053:
054: /**
055: * The "name" structural property of this node type.
056: */
057: public static final ChildPropertyDescriptor NAME_PROPERTY = internalNamePropertyFactory(EnumDeclaration.class);
058:
059: /**
060: * The "superInterfaceTypes" structural property of this node type.
061: */
062: public static final ChildListPropertyDescriptor SUPER_INTERFACE_TYPES_PROPERTY = new ChildListPropertyDescriptor(
063: EnumDeclaration.class,
064: "superInterfaceTypes", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$
065:
066: /**
067: * The "enumConstants" structural property of this node type.
068: */
069: public static final ChildListPropertyDescriptor ENUM_CONSTANTS_PROPERTY = new ChildListPropertyDescriptor(
070: EnumDeclaration.class,
071: "enumConstants", EnumConstantDeclaration.class, CYCLE_RISK); //$NON-NLS-1$
072:
073: /**
074: * The "bodyDeclarations" structural property of this node type.
075: */
076: public static final ChildListPropertyDescriptor BODY_DECLARATIONS_PROPERTY = internalBodyDeclarationPropertyFactory(EnumDeclaration.class);
077:
078: /**
079: * A list of property descriptors (element type:
080: * {@link StructuralPropertyDescriptor}),
081: * or null if uninitialized.
082: */
083: private static final List PROPERTY_DESCRIPTORS;
084:
085: static {
086: List properyList = new ArrayList(6);
087: createPropertyList(EnumDeclaration.class, properyList);
088: addProperty(JAVADOC_PROPERTY, properyList);
089: addProperty(MODIFIERS2_PROPERTY, properyList);
090: addProperty(NAME_PROPERTY, properyList);
091: addProperty(SUPER_INTERFACE_TYPES_PROPERTY, properyList);
092: addProperty(ENUM_CONSTANTS_PROPERTY, properyList);
093: addProperty(BODY_DECLARATIONS_PROPERTY, properyList);
094: PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
095: }
096:
097: /**
098: * Returns a list of structural property descriptors for this node type.
099: * Clients must not modify the result.
100: *
101: * @param apiLevel the API level; one of the
102: * <code>AST.JLS*</code> constants
103:
104: * @return a list of property descriptors (element type:
105: * {@link StructuralPropertyDescriptor})
106: */
107: public static List propertyDescriptors(int apiLevel) {
108: return PROPERTY_DESCRIPTORS;
109: }
110:
111: /**
112: * The superinterface types (element type: <code>Type</code>).
113: * Defaults to an empty list.
114: */
115: private ASTNode.NodeList super InterfaceTypes = new ASTNode.NodeList(
116: SUPER_INTERFACE_TYPES_PROPERTY);
117:
118: /**
119: * The enum constant declarations
120: * (element type: <code>EnumConstantDeclaration</code>).
121: * Defaults to an empty list.
122: */
123: private ASTNode.NodeList enumConstants = new ASTNode.NodeList(
124: ENUM_CONSTANTS_PROPERTY);
125:
126: /**
127: * Creates a new AST node for an enum declaration owned by the given
128: * AST. By default, the enum declaration has an unspecified, but legal,
129: * name; no modifiers; no javadoc; no superinterfaces;
130: * and empty lists of enum constants and body declarations.
131: * <p>
132: * N.B. This constructor is package-private; all subclasses must be
133: * declared in the same package; clients are unable to declare
134: * additional subclasses.
135: * </p>
136: *
137: * @param ast the AST that is to own this node
138: */
139: EnumDeclaration(AST ast) {
140: super (ast);
141: unsupportedIn2();
142: }
143:
144: /* (omit javadoc for this method)
145: * Method declared on ASTNode.
146: */
147: final List internalStructuralPropertiesForType(int apiLevel) {
148: return propertyDescriptors(apiLevel);
149: }
150:
151: /* (omit javadoc for this method)
152: * Method declared on ASTNode.
153: */
154: final ASTNode internalGetSetChildProperty(
155: ChildPropertyDescriptor property, boolean get, ASTNode child) {
156: if (property == JAVADOC_PROPERTY) {
157: if (get) {
158: return getJavadoc();
159: } else {
160: setJavadoc((Javadoc) child);
161: return null;
162: }
163: }
164: if (property == NAME_PROPERTY) {
165: if (get) {
166: return getName();
167: } else {
168: setName((SimpleName) child);
169: return null;
170: }
171: }
172: // allow default implementation to flag the error
173: return super .internalGetSetChildProperty(property, get, child);
174: }
175:
176: /* (omit javadoc for this method)
177: * Method declared on ASTNode.
178: */
179: final List internalGetChildListProperty(
180: ChildListPropertyDescriptor property) {
181: if (property == MODIFIERS2_PROPERTY) {
182: return modifiers();
183: }
184: if (property == SUPER_INTERFACE_TYPES_PROPERTY) {
185: return super InterfaceTypes();
186: }
187: if (property == ENUM_CONSTANTS_PROPERTY) {
188: return enumConstants();
189: }
190: if (property == BODY_DECLARATIONS_PROPERTY) {
191: return bodyDeclarations();
192: }
193: // allow default implementation to flag the error
194: return super .internalGetChildListProperty(property);
195: }
196:
197: /* (omit javadoc for this method)
198: * Method declared on BodyDeclaration.
199: */
200: final ChildPropertyDescriptor internalJavadocProperty() {
201: return JAVADOC_PROPERTY;
202: }
203:
204: /* (omit javadoc for this method)
205: * Method declared on BodyDeclaration.
206: */
207: final ChildListPropertyDescriptor internalModifiers2Property() {
208: return MODIFIERS2_PROPERTY;
209: }
210:
211: /* (omit javadoc for this method)
212: * Method declared on BodyDeclaration.
213: */
214: final SimplePropertyDescriptor internalModifiersProperty() {
215: // this property will not be asked for (node type did not exist in JLS2)
216: return null;
217: }
218:
219: /* (omit javadoc for this method)
220: * Method declared on AbstractTypeDeclaration.
221: */
222: final ChildPropertyDescriptor internalNameProperty() {
223: return NAME_PROPERTY;
224: }
225:
226: /* (omit javadoc for this method)
227: * Method declared on AbstractTypeDeclaration.
228: */
229: final ChildListPropertyDescriptor internalBodyDeclarationsProperty() {
230: return BODY_DECLARATIONS_PROPERTY;
231: }
232:
233: /* (omit javadoc for this method)
234: * Method declared on ASTNode.
235: */
236: final int getNodeType0() {
237: return ENUM_DECLARATION;
238: }
239:
240: /* (omit javadoc for this method)
241: * Method declared on ASTNode.
242: */
243: ASTNode clone0(AST target) {
244: EnumDeclaration result = new EnumDeclaration(target);
245: result
246: .setSourceRange(this .getStartPosition(), this
247: .getLength());
248: result.setJavadoc((Javadoc) ASTNode.copySubtree(target,
249: getJavadoc()));
250: result.modifiers().addAll(
251: ASTNode.copySubtrees(target, modifiers()));
252: result.setName((SimpleName) getName().clone(target));
253: result.super InterfaceTypes().addAll(
254: ASTNode.copySubtrees(target, super InterfaceTypes()));
255: result.enumConstants().addAll(
256: ASTNode.copySubtrees(target, enumConstants()));
257: result.bodyDeclarations().addAll(
258: ASTNode.copySubtrees(target, bodyDeclarations()));
259: return result;
260: }
261:
262: /* (omit javadoc for this method)
263: * Method declared on ASTNode.
264: */
265: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
266: // dispatch to correct overloaded match method
267: return matcher.match(this , other);
268: }
269:
270: /* (omit javadoc for this method)
271: * Method declared on ASTNode.
272: */
273: void accept0(ASTVisitor visitor) {
274: boolean visitChildren = visitor.visit(this );
275: if (visitChildren) {
276: // visit children in normal left to right reading order
277: acceptChild(visitor, getJavadoc());
278: acceptChildren(visitor, this .modifiers);
279: acceptChild(visitor, getName());
280: acceptChildren(visitor, this .super InterfaceTypes);
281: acceptChildren(visitor, this .enumConstants);
282: acceptChildren(visitor, this .bodyDeclarations);
283: }
284: visitor.endVisit(this );
285: }
286:
287: /**
288: * Returns the live ordered list of superinterfaces of this enum
289: * declaration.
290: *
291: * @return the live list of super interface types
292: * (element type: <code>Type</code>)
293: */
294: public List super InterfaceTypes() {
295: return this .super InterfaceTypes;
296: }
297:
298: /**
299: * Returns the live ordered list of enum constant declarations
300: * of this enum declaration.
301: *
302: * @return the live list of enum constant declarations
303: * (element type: {@link EnumConstantDeclaration})
304: */
305: public List enumConstants() {
306: return enumConstants;
307: }
308:
309: /* (omit javadoc for this method)
310: * Method declared on AsbtractTypeDeclaration.
311: */
312: ITypeBinding internalResolveBinding() {
313: return this .ast.getBindingResolver().resolveType(this );
314: }
315:
316: /* (omit javadoc for this method)
317: * Method declared on ASTNode.
318: */
319: int memSize() {
320: return super .memSize() + 2 * 4;
321: }
322:
323: /* (omit javadoc for this method)
324: * Method declared on ASTNode.
325: */
326: int treeSize() {
327: return memSize()
328: + (this .optionalDocComment == null ? 0 : getJavadoc()
329: .treeSize()) + this .modifiers.listSize()
330: + (this .typeName == null ? 0 : getName().treeSize())
331: + this.superInterfaceTypes.listSize()
332: + this.enumConstants.listSize()
333: + this.bodyDeclarations.listSize();
334: }
335: }
|