1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2000, 2013 IBM Corporation and others. |
3 | * |
4 | * This program and the accompanying materials |
5 | * are made available under the terms of the Eclipse Public License 2.0 |
6 | * which accompanies this distribution, and is available at |
7 | * https://www.eclipse.org/legal/epl-2.0/ |
8 | * |
9 | * SPDX-License-Identifier: EPL-2.0 |
10 | * |
11 | * Contributors: |
12 | * IBM Corporation - initial API and implementation |
13 | *******************************************************************************/ |
14 | package org.eclipse.jdt.core.dom; |
15 | |
16 | import java.util.ArrayList; |
17 | import java.util.List; |
18 | |
19 | /** |
20 | * Enum declaration AST node type (added in JLS3 API). |
21 | * |
22 | * <pre> |
23 | * EnumDeclaration: |
24 | * [ Javadoc ] { ExtendedModifier } <b>enum</b> Identifier |
25 | * [ <b>implements</b> Type { <b>,</b> Type } ] |
26 | * <b>{</b> |
27 | * [ EnumConstantDeclaration { <b>,</b> EnumConstantDeclaration } ] [ <b>,</b> ] |
28 | * [ <b>;</b> { ClassBodyDeclaration | <b>;</b> } ] |
29 | * <b>}</b> |
30 | * </pre> |
31 | * |
32 | * The {@link #enumConstants()} list holds the enum constant declarations, |
33 | * while the {@link #bodyDeclarations()} list holds the class body declarations |
34 | * that appear after the semicolon. |
35 | * <p> |
36 | * When a Javadoc comment is present, the source |
37 | * range begins with the first character of the "/**" comment delimiter. |
38 | * When there is no Javadoc comment, the source range begins with the first |
39 | * character of the first modifier or annotation (if present), or the |
40 | * first character of the "enum" keyword (if no |
41 | * modifiers or annotations). The source range extends through the last |
42 | * character of the "}" token following the body declarations. |
43 | * </p> |
44 | * |
45 | * @since 3.1 |
46 | * @noinstantiate This class is not intended to be instantiated by clients. |
47 | */ |
48 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
49 | public class EnumDeclaration extends AbstractTypeDeclaration { |
50 | |
51 | /** |
52 | * The "javadoc" structural property of this node type (child type: |
53 | * {@link Javadoc}). |
54 | */ |
55 | public static final ChildPropertyDescriptor JAVADOC_PROPERTY = internalJavadocPropertyFactory( |
56 | EnumDeclaration.class); |
57 | |
58 | /** |
59 | * The "modifiers" structural property of this node type (element type: |
60 | * {@link IExtendedModifier}) (added in JLS3 API). |
61 | */ |
62 | public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY = internalModifiers2PropertyFactory( |
63 | EnumDeclaration.class); |
64 | |
65 | /** |
66 | * The "name" structural property of this node type (child type: |
67 | * {@link SimpleName}). |
68 | */ |
69 | public static final ChildPropertyDescriptor NAME_PROPERTY = internalNamePropertyFactory(EnumDeclaration.class); |
70 | |
71 | /** |
72 | * The "superInterfaceTypes" structural property of this node type (element |
73 | * type: {@link Type}). |
74 | */ |
75 | public static final ChildListPropertyDescriptor SUPER_INTERFACE_TYPES_PROPERTY = new ChildListPropertyDescriptor( |
76 | EnumDeclaration.class, "superInterfaceTypes", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$ |
77 | |
78 | /** |
79 | * The "enumConstants" structural property of this node type (element type: |
80 | * {@link EnumConstantDeclaration}). |
81 | */ |
82 | public static final ChildListPropertyDescriptor ENUM_CONSTANTS_PROPERTY = new ChildListPropertyDescriptor( |
83 | EnumDeclaration.class, "enumConstants", EnumConstantDeclaration.class, CYCLE_RISK); //$NON-NLS-1$ |
84 | |
85 | /** |
86 | * The "bodyDeclarations" structural property of this node type (element type: |
87 | * {@link BodyDeclaration}). |
88 | */ |
89 | public static final ChildListPropertyDescriptor BODY_DECLARATIONS_PROPERTY = internalBodyDeclarationPropertyFactory( |
90 | EnumDeclaration.class); |
91 | |
92 | /** |
93 | * A list of property descriptors (element type: |
94 | * {@link StructuralPropertyDescriptor}), |
95 | * or null if uninitialized. |
96 | */ |
97 | private static final List PROPERTY_DESCRIPTORS; |
98 | |
99 | static { |
100 | List properyList = new ArrayList(6); |
101 | createPropertyList(EnumDeclaration.class, properyList); |
102 | addProperty(JAVADOC_PROPERTY, properyList); |
103 | addProperty(MODIFIERS2_PROPERTY, properyList); |
104 | addProperty(NAME_PROPERTY, properyList); |
105 | addProperty(SUPER_INTERFACE_TYPES_PROPERTY, properyList); |
106 | addProperty(ENUM_CONSTANTS_PROPERTY, properyList); |
107 | addProperty(BODY_DECLARATIONS_PROPERTY, properyList); |
108 | PROPERTY_DESCRIPTORS = reapPropertyList(properyList); |
109 | } |
110 | |
111 | /** |
112 | * Returns a list of structural property descriptors for this node type. |
113 | * Clients must not modify the result. |
114 | * |
115 | * @param apiLevel the API level; one of the |
116 | * <code>AST.JLS*</code> constants |
117 | * |
118 | * @return a list of property descriptors (element type: |
119 | * {@link StructuralPropertyDescriptor}) |
120 | */ |
121 | public static List propertyDescriptors(int apiLevel) { |
122 | return PROPERTY_DESCRIPTORS; |
123 | } |
124 | |
125 | /** |
126 | * The superinterface types (element type: {@link Type}). |
127 | * Defaults to an empty list. |
128 | */ |
129 | private ASTNode.NodeList superInterfaceTypes = new ASTNode.NodeList(SUPER_INTERFACE_TYPES_PROPERTY); |
130 | |
131 | /** |
132 | * The enum constant declarations |
133 | * (element type: {@link EnumConstantDeclaration}). |
134 | * Defaults to an empty list. |
135 | */ |
136 | private ASTNode.NodeList enumConstants = new ASTNode.NodeList(ENUM_CONSTANTS_PROPERTY); |
137 | |
138 | /** |
139 | * Creates a new AST node for an enum declaration owned by the given |
140 | * AST. By default, the enum declaration has an unspecified, but legal, |
141 | * name; no modifiers; no javadoc; no superinterfaces; |
142 | * and empty lists of enum constants and body declarations. |
143 | * <p> |
144 | * N.B. This constructor is package-private; all subclasses must be |
145 | * declared in the same package; clients are unable to declare |
146 | * additional subclasses. |
147 | * </p> |
148 | * |
149 | * @param ast the AST that is to own this node |
150 | */ |
151 | EnumDeclaration(AST ast) { |
152 | super(ast); |
153 | unsupportedIn2(); |
154 | } |
155 | |
156 | @Override |
157 | final List internalStructuralPropertiesForType(int apiLevel) { |
158 | return propertyDescriptors(apiLevel); |
159 | } |
160 | |
161 | @Override |
162 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
163 | if (property == JAVADOC_PROPERTY) { |
164 | if (get) { |
165 | return getJavadoc(); |
166 | } else { |
167 | setJavadoc((Javadoc) child); |
168 | return null; |
169 | } |
170 | } |
171 | if (property == NAME_PROPERTY) { |
172 | if (get) { |
173 | return getName(); |
174 | } else { |
175 | setName((SimpleName) child); |
176 | return null; |
177 | } |
178 | } |
179 | // allow default implementation to flag the error |
180 | return super.internalGetSetChildProperty(property, get, child); |
181 | } |
182 | |
183 | @Override |
184 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
185 | if (property == MODIFIERS2_PROPERTY) { |
186 | return modifiers(); |
187 | } |
188 | if (property == SUPER_INTERFACE_TYPES_PROPERTY) { |
189 | return superInterfaceTypes(); |
190 | } |
191 | if (property == ENUM_CONSTANTS_PROPERTY) { |
192 | return enumConstants(); |
193 | } |
194 | if (property == BODY_DECLARATIONS_PROPERTY) { |
195 | return bodyDeclarations(); |
196 | } |
197 | // allow default implementation to flag the error |
198 | return super.internalGetChildListProperty(property); |
199 | } |
200 | |
201 | @Override |
202 | final ChildPropertyDescriptor internalJavadocProperty() { |
203 | return JAVADOC_PROPERTY; |
204 | } |
205 | |
206 | @Override |
207 | final ChildListPropertyDescriptor internalModifiers2Property() { |
208 | return MODIFIERS2_PROPERTY; |
209 | } |
210 | |
211 | @Override |
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 | @Override |
218 | final ChildPropertyDescriptor internalNameProperty() { |
219 | return NAME_PROPERTY; |
220 | } |
221 | |
222 | @Override |
223 | final ChildListPropertyDescriptor internalBodyDeclarationsProperty() { |
224 | return BODY_DECLARATIONS_PROPERTY; |
225 | } |
226 | |
227 | @Override |
228 | final int getNodeType0() { |
229 | return ENUM_DECLARATION; |
230 | } |
231 | |
232 | @Override |
233 | ASTNode clone0(AST target) { |
234 | EnumDeclaration result = new EnumDeclaration(target); |
235 | result.setSourceRange(getStartPosition(), getLength()); |
236 | result.setJavadoc( |
237 | (Javadoc) ASTNode.copySubtree(target, getJavadoc())); |
238 | result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers())); |
239 | result.setName((SimpleName) getName().clone(target)); |
240 | result.superInterfaceTypes().addAll( |
241 | ASTNode.copySubtrees(target, superInterfaceTypes())); |
242 | result.enumConstants().addAll( |
243 | ASTNode.copySubtrees(target, enumConstants())); |
244 | result.bodyDeclarations().addAll( |
245 | ASTNode.copySubtrees(target, bodyDeclarations())); |
246 | return result; |
247 | } |
248 | |
249 | @Override |
250 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
251 | // dispatch to correct overloaded match method |
252 | return matcher.match(this, other); |
253 | } |
254 | |
255 | @Override |
256 | void accept0(ASTVisitor visitor) { |
257 | boolean visitChildren = visitor.visit(this); |
258 | if (visitChildren) { |
259 | visitor.pushScope(this.getName().getIdentifier()); |
260 | // visit children in normal left to right reading order |
261 | acceptChild(visitor, getJavadoc()); |
262 | acceptChildren(visitor, this.modifiers); |
263 | acceptChild(visitor, getName()); |
264 | acceptChildren(visitor, this.superInterfaceTypes); |
265 | acceptChildren(visitor, this.enumConstants); |
266 | acceptChildren(visitor, this.bodyDeclarations); |
267 | visitor.popScope(); |
268 | } |
269 | visitor.endVisit(this); |
270 | } |
271 | |
272 | /** |
273 | * Returns the live ordered list of superinterfaces of this enum |
274 | * declaration. |
275 | * |
276 | * @return the live list of super interface types |
277 | * (element type: {@link Type}) |
278 | */ |
279 | public List superInterfaceTypes() { |
280 | return this.superInterfaceTypes; |
281 | } |
282 | |
283 | /** |
284 | * Returns the live ordered list of enum constant declarations |
285 | * of this enum declaration. |
286 | * |
287 | * @return the live list of enum constant declarations |
288 | * (element type: {@link EnumConstantDeclaration}) |
289 | */ |
290 | public List enumConstants() { |
291 | return this.enumConstants; |
292 | } |
293 | |
294 | @Override |
295 | ITypeBinding internalResolveBinding() { |
296 | return this.ast.getBindingResolver().resolveType(this); |
297 | } |
298 | |
299 | @Override |
300 | int memSize() { |
301 | return super.memSize() + 2 * 4; |
302 | } |
303 | |
304 | @Override |
305 | int treeSize() { |
306 | return memSize() |
307 | + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize()) |
308 | + this.modifiers.listSize() |
309 | + (this.typeName == null ? 0 : getName().treeSize()) |
310 | + this.superInterfaceTypes.listSize() |
311 | + this.enumConstants.listSize() |
312 | + this.bodyDeclarations.listSize(); |
313 | } |
314 | } |
315 |
Members