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: * Super constructor invocation statement AST node type.
017: * For JLS2: * <pre>
018: * SuperConstructorInvocation:
019: * [ Expression <b>.</b> ] <b>super</b>
020: * <b>(</b> [ Expression { <b>,</b> Expression } ] <b>)</b> <b>;</b>
021: * </pre>
022: * For JLS3, type arguments are added:
023: * <pre>
024: * SuperConstructorInvocation:
025: * [ Expression <b>.</b> ]
026: * [ <b><</b> Type { <b>,</b> Type } <b>></b> ]
027: * <b>super</b> <b>(</b> [ Expression { <b>,</b> Expression } ] <b>)</b> <b>;</b>
028: * </pre>
029: *
030: * @since 2.0
031: */
032: public class SuperConstructorInvocation extends Statement {
033:
034: /**
035: * The "expression" structural property of this node type.
036: * @since 3.0
037: */
038: public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = new ChildPropertyDescriptor(
039: SuperConstructorInvocation.class,
040: "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
041:
042: /**
043: * The "typeArguments" structural property of this node type (added in JLS3 API).
044: * @since 3.1
045: */
046: public static final ChildListPropertyDescriptor TYPE_ARGUMENTS_PROPERTY = new ChildListPropertyDescriptor(
047: SuperConstructorInvocation.class,
048: "typeArguments", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$
049:
050: /**
051: * The "arguments" structural property of this node type.
052: * @since 3.0
053: */
054: public static final ChildListPropertyDescriptor ARGUMENTS_PROPERTY = new ChildListPropertyDescriptor(
055: SuperConstructorInvocation.class,
056: "arguments", Expression.class, CYCLE_RISK); //$NON-NLS-1$
057:
058: /**
059: * A list of property descriptors (element type:
060: * {@link StructuralPropertyDescriptor}),
061: * or null if uninitialized.
062: * @since 3.0
063: */
064: private static final List PROPERTY_DESCRIPTORS_2_0;
065:
066: /**
067: * A list of property descriptors (element type:
068: * {@link StructuralPropertyDescriptor}),
069: * or null if uninitialized.
070: * @since 3.1
071: */
072: private static final List PROPERTY_DESCRIPTORS_3_0;
073:
074: static {
075: List propertyList = new ArrayList(3);
076: createPropertyList(SuperConstructorInvocation.class,
077: propertyList);
078: addProperty(EXPRESSION_PROPERTY, propertyList);
079: addProperty(ARGUMENTS_PROPERTY, propertyList);
080: PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(propertyList);
081:
082: propertyList = new ArrayList(4);
083: createPropertyList(SuperConstructorInvocation.class,
084: propertyList);
085: addProperty(EXPRESSION_PROPERTY, propertyList);
086: addProperty(TYPE_ARGUMENTS_PROPERTY, propertyList);
087: addProperty(ARGUMENTS_PROPERTY, propertyList);
088: PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList);
089: }
090:
091: /**
092: * Returns a list of structural property descriptors for this node type.
093: * Clients must not modify the result.
094: *
095: * @param apiLevel the API level; one of the
096: * <code>AST.JLS*</code> constants
097:
098: * @return a list of property descriptors (element type:
099: * {@link StructuralPropertyDescriptor})
100: * @since 3.0
101: */
102: public static List propertyDescriptors(int apiLevel) {
103: if (apiLevel == AST.JLS2_INTERNAL) {
104: return PROPERTY_DESCRIPTORS_2_0;
105: } else {
106: return PROPERTY_DESCRIPTORS_3_0;
107: }
108: }
109:
110: /**
111: * The expression; <code>null</code> for none; defaults to none.
112: */
113: private Expression optionalExpression = null;
114:
115: /**
116: * The type arguments (element type: <code>Type</code>).
117: * Null in JLS2. Added in JLS3; defaults to an empty list
118: * (see constructor).
119: * @since 3.1
120: */
121: private ASTNode.NodeList typeArguments = null;
122:
123: /**
124: * The list of argument expressions (element type:
125: * <code>Expression</code>). Defaults to an empty list.
126: */
127: private ASTNode.NodeList arguments = new ASTNode.NodeList(
128: ARGUMENTS_PROPERTY);
129:
130: /**
131: * Creates a new AST node for an super constructor invocation statement
132: * owned by the given AST. By default, no type arguments, and an empty list
133: * of arguments.
134: *
135: * @param ast the AST that is to own this node
136: */
137: SuperConstructorInvocation(AST ast) {
138: super (ast);
139: if (ast.apiLevel >= AST.JLS3) {
140: this .typeArguments = new ASTNode.NodeList(
141: TYPE_ARGUMENTS_PROPERTY);
142: }
143: }
144:
145: /* (omit javadoc for this method)
146: * Method declared on ASTNode.
147: */
148: final List internalStructuralPropertiesForType(int apiLevel) {
149: return propertyDescriptors(apiLevel);
150: }
151:
152: /* (omit javadoc for this method)
153: * Method declared on ASTNode.
154: */
155: final ASTNode internalGetSetChildProperty(
156: ChildPropertyDescriptor property, boolean get, ASTNode child) {
157: if (property == EXPRESSION_PROPERTY) {
158: if (get) {
159: return getExpression();
160: } else {
161: setExpression((Expression) child);
162: return null;
163: }
164: }
165: // allow default implementation to flag the error
166: return super .internalGetSetChildProperty(property, get, child);
167: }
168:
169: /* (omit javadoc for this method)
170: * Method declared on ASTNode.
171: */
172: final List internalGetChildListProperty(
173: ChildListPropertyDescriptor property) {
174: if (property == ARGUMENTS_PROPERTY) {
175: return arguments();
176: }
177: if (property == TYPE_ARGUMENTS_PROPERTY) {
178: return typeArguments();
179: }
180: // allow default implementation to flag the error
181: return super .internalGetChildListProperty(property);
182: }
183:
184: /* (omit javadoc for this method)
185: * Method declared on ASTNode.
186: */
187: final int getNodeType0() {
188: return SUPER_CONSTRUCTOR_INVOCATION;
189: }
190:
191: /* (omit javadoc for this method)
192: * Method declared on ASTNode.
193: */
194: ASTNode clone0(AST target) {
195: SuperConstructorInvocation result = new SuperConstructorInvocation(
196: target);
197: result
198: .setSourceRange(this .getStartPosition(), this
199: .getLength());
200: result.copyLeadingComment(this );
201: result.setExpression((Expression) ASTNode.copySubtree(target,
202: getExpression()));
203: if (this .ast.apiLevel >= AST.JLS3) {
204: result.typeArguments().addAll(
205: ASTNode.copySubtrees(target, typeArguments()));
206: }
207: result.arguments().addAll(
208: ASTNode.copySubtrees(target, arguments()));
209: return result;
210: }
211:
212: /* (omit javadoc for this method)
213: * Method declared on ASTNode.
214: */
215: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
216: // dispatch to correct overloaded match method
217: return matcher.match(this , other);
218: }
219:
220: /* (omit javadoc for this method)
221: * Method declared on ASTNode.
222: */
223: void accept0(ASTVisitor visitor) {
224: boolean visitChildren = visitor.visit(this );
225: if (visitChildren) {
226: // visit children in normal left to right reading order
227: acceptChild(visitor, getExpression());
228: if (this .ast.apiLevel >= AST.JLS3) {
229: acceptChildren(visitor, this .typeArguments);
230: }
231: acceptChildren(visitor, this .arguments);
232: }
233: visitor.endVisit(this );
234: }
235:
236: /**
237: * Returns the expression of this super constructor invocation statement,
238: * or <code>null</code> if there is none.
239: *
240: * @return the expression node, or <code>null</code> if there is none
241: */
242: public Expression getExpression() {
243: return this .optionalExpression;
244: }
245:
246: /**
247: * Sets or clears the expression of this super constructor invocation
248: * statement.
249: *
250: * @param expression the expression node, or <code>null</code> if
251: * there is none
252: * @exception IllegalArgumentException if:
253: * <ul>
254: * <li>the node belongs to a different AST</li>
255: * <li>the node already has a parent</li>
256: * <li>a cycle in would be created</li>
257: * </ul>
258: */
259: public void setExpression(Expression expression) {
260: ASTNode oldChild = this .optionalExpression;
261: preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
262: this .optionalExpression = expression;
263: postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
264: }
265:
266: /**
267: * Returns the live ordered list of type arguments of this constructor
268: * invocation (added in JLS3 API).
269: *
270: * @return the live list of type arguments
271: * (element type: <code>Type</code>)
272: * @exception UnsupportedOperationException if this operation is used in
273: * a JLS2 AST
274: * @since 3.1
275: */
276: public List typeArguments() {
277: // more efficient than just calling unsupportedIn2() to check
278: if (this .typeArguments == null) {
279: unsupportedIn2();
280: }
281: return this .typeArguments;
282: }
283:
284: /**
285: * Returns the live ordered list of argument expressions in this super
286: * constructor invocation statement.
287: *
288: * @return the live list of argument expressions
289: * (element type: <code>Expression</code>)
290: */
291: public List arguments() {
292: return this .arguments;
293: }
294:
295: /**
296: * Resolves and returns the binding for the constructor invoked by this
297: * expression.
298: * <p>
299: * Note that bindings are generally unavailable unless requested when the
300: * AST is being built.
301: * </p>
302: *
303: * @return the constructor binding, or <code>null</code> if the binding
304: * cannot be resolved
305: */
306: public IMethodBinding resolveConstructorBinding() {
307: return this .ast.getBindingResolver().resolveConstructor(this );
308: }
309:
310: /* (omit javadoc for this method)
311: * Method declared on ASTNode.
312: */
313: int memSize() {
314: // treat Code as free
315: return BASE_NODE_SIZE + 3 * 4;
316: }
317:
318: /* (omit javadoc for this method)
319: * Method declared on ASTNode.
320: */
321: int treeSize() {
322: return memSize()
323: + (this .optionalExpression == null ? 0
324: : getExpression().treeSize())
325: + (this .typeArguments == null ? 0 : this .typeArguments
326: .listSize())
327: + (this .arguments == null ? 0 : this.arguments
328: .listSize());
329: }
330: }
|