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: * Alternate constructor invocation statement AST node type.
017: * For JLS2:
018: * <pre>
019: * ConstructorInvocation:
020: * <b>this</b> <b>(</b> [ Expression { <b>,</b> Expression } ] <b>)</b> <b>;</b>
021: * </pre>
022: * For JLS3, type arguments are added:
023: * <pre>
024: * ConstructorInvocation:
025: * [ <b><</b> Type { <b>,</b> Type } <b>></b> ]
026: * <b>this</b> <b>(</b> [ Expression { <b>,</b> Expression } ] <b>)</b> <b>;</b>
027: * </pre>
028: *
029: * @since 2.0
030: */
031: public class ConstructorInvocation extends Statement {
032:
033: /**
034: * The "typeArguments" structural property of this node type (added in JLS3 API).
035: * @since 3.1
036: */
037: public static final ChildListPropertyDescriptor TYPE_ARGUMENTS_PROPERTY = new ChildListPropertyDescriptor(
038: ConstructorInvocation.class,
039: "typeArguments", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$
040:
041: /**
042: * The "arguments" structural property of this node type.
043: * @since 3.0
044: */
045: public static final ChildListPropertyDescriptor ARGUMENTS_PROPERTY = new ChildListPropertyDescriptor(
046: ConstructorInvocation.class,
047: "arguments", Expression.class, CYCLE_RISK); //$NON-NLS-1$
048:
049: /**
050: * A list of property descriptors (element type:
051: * {@link StructuralPropertyDescriptor}),
052: * or null if uninitialized.
053: * @since 3.0
054: */
055: private static final List PROPERTY_DESCRIPTORS_2_0;
056:
057: /**
058: * A list of property descriptors (element type:
059: * {@link StructuralPropertyDescriptor}),
060: * or null if uninitialized.
061: * @since 3.1
062: */
063: private static final List PROPERTY_DESCRIPTORS_3_0;
064:
065: static {
066: List properyList = new ArrayList(2);
067: createPropertyList(ConstructorInvocation.class, properyList);
068: addProperty(ARGUMENTS_PROPERTY, properyList);
069: PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
070:
071: properyList = new ArrayList(3);
072: createPropertyList(ConstructorInvocation.class, properyList);
073: addProperty(TYPE_ARGUMENTS_PROPERTY, properyList);
074: addProperty(ARGUMENTS_PROPERTY, properyList);
075: PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
076: }
077:
078: /**
079: * Returns a list of structural property descriptors for this node type.
080: * Clients must not modify the result.
081: *
082: * @param apiLevel the API level; one of the
083: * <code>AST.JLS*</code> constants
084:
085: * @return a list of property descriptors (element type:
086: * {@link StructuralPropertyDescriptor})
087: * @since 3.0
088: */
089: public static List propertyDescriptors(int apiLevel) {
090: if (apiLevel == AST.JLS2_INTERNAL) {
091: return PROPERTY_DESCRIPTORS_2_0;
092: } else {
093: return PROPERTY_DESCRIPTORS_3_0;
094: }
095: }
096:
097: /**
098: * The type arguments (element type: <code>Type</code>).
099: * Null in JLS2. Added in JLS3; defaults to an empty list
100: * (see constructor).
101: * @since 3.1
102: */
103: private ASTNode.NodeList typeArguments = null;
104:
105: /**
106: * The list of argument expressions (element type:
107: * <code>Expression</code>). Defaults to an empty list.
108: */
109: private ASTNode.NodeList arguments = new ASTNode.NodeList(
110: ARGUMENTS_PROPERTY);
111:
112: /**
113: * Creates a new AST node for an alternate constructor invocation statement
114: * owned by the given AST. By default, an empty list of arguments.
115: *
116: * @param ast the AST that is to own this node
117: */
118: ConstructorInvocation(AST ast) {
119: super (ast);
120: if (ast.apiLevel >= AST.JLS3) {
121: this .typeArguments = new ASTNode.NodeList(
122: TYPE_ARGUMENTS_PROPERTY);
123: }
124: }
125:
126: /* (omit javadoc for this method)
127: * Method declared on ASTNode.
128: */
129: final List internalStructuralPropertiesForType(int apiLevel) {
130: return propertyDescriptors(apiLevel);
131: }
132:
133: /* (omit javadoc for this method)
134: * Method declared on ASTNode.
135: */
136: final List internalGetChildListProperty(
137: ChildListPropertyDescriptor property) {
138: if (property == ARGUMENTS_PROPERTY) {
139: return arguments();
140: }
141: if (property == TYPE_ARGUMENTS_PROPERTY) {
142: return typeArguments();
143: }
144: // allow default implementation to flag the error
145: return super .internalGetChildListProperty(property);
146: }
147:
148: /* (omit javadoc for this method)
149: * Method declared on ASTNode.
150: */
151: final int getNodeType0() {
152: return CONSTRUCTOR_INVOCATION;
153: }
154:
155: /* (omit javadoc for this method)
156: * Method declared on ASTNode.
157: */
158: ASTNode clone0(AST target) {
159: ConstructorInvocation result = new ConstructorInvocation(target);
160: result
161: .setSourceRange(this .getStartPosition(), this
162: .getLength());
163: result.copyLeadingComment(this );
164: if (this .ast.apiLevel >= AST.JLS3) {
165: result.typeArguments().addAll(
166: ASTNode.copySubtrees(target, typeArguments()));
167: }
168: result.arguments().addAll(
169: ASTNode.copySubtrees(target, arguments()));
170: return result;
171: }
172:
173: /* (omit javadoc for this method)
174: * Method declared on ASTNode.
175: */
176: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
177: // dispatch to correct overloaded match method
178: return matcher.match(this , other);
179: }
180:
181: /* (omit javadoc for this method)
182: * Method declared on ASTNode.
183: */
184: void accept0(ASTVisitor visitor) {
185: boolean visitChildren = visitor.visit(this );
186: if (visitChildren) {
187: if (this .ast.apiLevel >= AST.JLS3) {
188: acceptChildren(visitor, this .typeArguments);
189: }
190: acceptChildren(visitor, this .arguments);
191: }
192: visitor.endVisit(this );
193: }
194:
195: /**
196: * Returns the live ordered list of type arguments of this constructor
197: * invocation (added in JLS3 API).
198: *
199: * @return the live list of type arguments
200: * (element type: <code>Type</code>)
201: * @exception UnsupportedOperationException if this operation is used in
202: * a JLS2 AST
203: * @since 3.1
204: */
205: public List typeArguments() {
206: // more efficient than just calling unsupportedIn2() to check
207: if (this .typeArguments == null) {
208: unsupportedIn2();
209: }
210: return this .typeArguments;
211: }
212:
213: /**
214: * Returns the live ordered list of argument expressions in this alternate
215: * constructor invocation statement.
216: *
217: * @return the live list of argument expressions
218: * (element type: <code>Expression</code>)
219: */
220: public List arguments() {
221: return this .arguments;
222: }
223:
224: /**
225: * Resolves and returns the binding for the constructor invoked by this
226: * expression.
227: * <p>
228: * Note that bindings are generally unavailable unless requested when the
229: * AST is being built.
230: * </p>
231: *
232: * @return the constructor binding, or <code>null</code> if the binding
233: * cannot be resolved
234: */
235: public IMethodBinding resolveConstructorBinding() {
236: return this .ast.getBindingResolver().resolveConstructor(this );
237: }
238:
239: /* (omit javadoc for this method)
240: * Method declared on ASTNode.
241: */
242: int memSize() {
243: // treat Code as free
244: return BASE_NODE_SIZE + 2 * 4;
245: }
246:
247: /* (omit javadoc for this method)
248: * Method declared on ASTNode.
249: */
250: int treeSize() {
251: return memSize()
252: + (this .typeArguments == null ? 0 : this .typeArguments
253: .listSize())
254: + (this .arguments == null ? 0 : this.arguments
255: .listSize());
256: }
257: }
|