001: /*******************************************************************************
002: * Copyright (c) 2004, 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: * AST node for a parameter within a method reference ({@link MethodRef}).
017: * These nodes only occur within doc comments ({@link Javadoc}).
018: * For JLS2:
019: * <pre>
020: * MethodRefParameter:
021: * Type [ Identifier ]
022: * </pre>
023: * For JLS3, the variable arity indicator was added:
024: * <pre>
025: * MethodRefParameter:
026: * Type [ <b>...</b> ] [ Identifier ]
027: * </pre>
028: * <p>
029: * Note: The 1.5 spec for the Javadoc tool does not mention the possibility
030: * of a variable arity indicator in method references. However, the 1.5
031: * Javadoc tool itself does indeed support it. Since it makes sense to have
032: * a way to explicitly refer to variable arity methods, it seems more likely
033: * that the Javadoc spec is wrong in this case.
034: * </p>
035: *
036: * @see Javadoc
037: * @since 3.0
038: */
039: public class MethodRefParameter extends ASTNode {
040:
041: /**
042: * The "type" structural property of this node type.
043: * @since 3.0
044: */
045: public static final ChildPropertyDescriptor TYPE_PROPERTY = new ChildPropertyDescriptor(
046: MethodRefParameter.class,
047: "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
048:
049: /**
050: * The "varargs" structural property of this node type (added in JLS3 API).
051: * @since 3.1
052: */
053: public static final SimplePropertyDescriptor VARARGS_PROPERTY = new SimplePropertyDescriptor(
054: MethodRefParameter.class,
055: "varargs", boolean.class, MANDATORY); //$NON-NLS-1$
056:
057: /**
058: * The "name" structural property of this node type.
059: * @since 3.0
060: */
061: public static final ChildPropertyDescriptor NAME_PROPERTY = new ChildPropertyDescriptor(
062: MethodRefParameter.class,
063: "name", SimpleName.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$
064:
065: /**
066: * A list of property descriptors (element type:
067: * {@link StructuralPropertyDescriptor}),
068: * or null if uninitialized.
069: * @since 3.0
070: */
071: private static final List PROPERTY_DESCRIPTORS_2_0;
072:
073: /**
074: * A list of property descriptors (element type:
075: * {@link StructuralPropertyDescriptor}),
076: * or null if uninitialized.
077: * @since 3.1
078: */
079: private static final List PROPERTY_DESCRIPTORS_3_0;
080:
081: static {
082: List properyList = new ArrayList(3);
083: createPropertyList(MethodRefParameter.class, properyList);
084: addProperty(TYPE_PROPERTY, properyList);
085: addProperty(NAME_PROPERTY, properyList);
086: PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
087:
088: properyList = new ArrayList(3);
089: createPropertyList(MethodRefParameter.class, properyList);
090: addProperty(TYPE_PROPERTY, properyList);
091: addProperty(VARARGS_PROPERTY, properyList);
092: addProperty(NAME_PROPERTY, properyList);
093: PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
094: }
095:
096: /**
097: * Returns a list of structural property descriptors for this node type.
098: * Clients must not modify the result.
099: *
100: * @param apiLevel the API level; one of the AST.JLS* constants
101: * @return a list of property descriptors (element type:
102: * {@link StructuralPropertyDescriptor})
103: * @since 3.0
104: */
105: public static List propertyDescriptors(int apiLevel) {
106: if (apiLevel == AST.JLS2_INTERNAL) {
107: return PROPERTY_DESCRIPTORS_2_0;
108: } else {
109: return PROPERTY_DESCRIPTORS_3_0;
110: }
111: }
112:
113: /**
114: * The type; lazily initialized; defaults to a unspecified,
115: * legal type.
116: */
117: private Type type = null;
118:
119: /**
120: * Indicates the last parameter of a variable arity method;
121: * defaults to false.
122: *
123: * @since 3.1
124: */
125: private boolean variableArity = false;
126:
127: /**
128: * The parameter name, or <code>null</code> if none; none by
129: * default.
130: */
131: private SimpleName optionalParameterName = null;
132:
133: /**
134: * Creates a new AST node for a method referenece parameter owned by the given
135: * AST. By default, the node has an unspecified (but legal) type,
136: * not variable arity, and no parameter name.
137: * <p>
138: * N.B. This constructor is package-private.
139: * </p>
140: *
141: * @param ast the AST that is to own this node
142: */
143: MethodRefParameter(AST ast) {
144: super (ast);
145: }
146:
147: /* (omit javadoc for this method)
148: * Method declared on ASTNode.
149: */
150: final List internalStructuralPropertiesForType(int apiLevel) {
151: return propertyDescriptors(apiLevel);
152: }
153:
154: /* (omit javadoc for this method)
155: * Method declared on ASTNode.
156: */
157: final ASTNode internalGetSetChildProperty(
158: ChildPropertyDescriptor property, boolean get, ASTNode child) {
159: if (property == TYPE_PROPERTY) {
160: if (get) {
161: return getType();
162: } else {
163: setType((Type) child);
164: return null;
165: }
166: }
167: if (property == NAME_PROPERTY) {
168: if (get) {
169: return getName();
170: } else {
171: setName((SimpleName) child);
172: return null;
173: }
174: }
175: // allow default implementation to flag the error
176: return super .internalGetSetChildProperty(property, get, child);
177: }
178:
179: /* (omit javadoc for this method)
180: * Method declared on ASTNode.
181: */
182: final boolean internalGetSetBooleanProperty(
183: SimplePropertyDescriptor property, boolean get,
184: boolean value) {
185: if (property == VARARGS_PROPERTY) {
186: if (get) {
187: return isVarargs();
188: } else {
189: setVarargs(value);
190: return false;
191: }
192: }
193: // allow default implementation to flag the error
194: return super
195: .internalGetSetBooleanProperty(property, get, value);
196: }
197:
198: /* (omit javadoc for this method)
199: * Method declared on ASTNode.
200: */
201: final int getNodeType0() {
202: return METHOD_REF_PARAMETER;
203: }
204:
205: /* (omit javadoc for this method)
206: * Method declared on ASTNode.
207: */
208: ASTNode clone0(AST target) {
209: MethodRefParameter result = new MethodRefParameter(target);
210: result
211: .setSourceRange(this .getStartPosition(), this
212: .getLength());
213: result.setType((Type) ASTNode.copySubtree(target, getType()));
214: if (this .ast.apiLevel >= AST.JLS3) {
215: result.setVarargs(isVarargs());
216: }
217: result.setName((SimpleName) ASTNode.copySubtree(target,
218: getName()));
219: return result;
220: }
221:
222: /* (omit javadoc for this method)
223: * Method declared on ASTNode.
224: */
225: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
226: // dispatch to correct overloaded match method
227: return matcher.match(this , other);
228: }
229:
230: /* (omit javadoc for this method)
231: * Method declared on ASTNode.
232: */
233: void accept0(ASTVisitor visitor) {
234: boolean visitChildren = visitor.visit(this );
235: if (visitChildren) {
236: // visit children in normal left to right reading order
237: acceptChild(visitor, getType());
238: acceptChild(visitor, getName());
239: }
240: visitor.endVisit(this );
241: }
242:
243: /**
244: * Returns the paramter type.
245: *
246: * @return the parameter type
247: */
248: public Type getType() {
249: if (this .type == null) {
250: // lazy init must be thread-safe for readers
251: synchronized (this ) {
252: if (this .type == null) {
253: preLazyInit();
254: this .type = this .ast
255: .newPrimitiveType(PrimitiveType.INT);
256: postLazyInit(this .type, TYPE_PROPERTY);
257: }
258: }
259: }
260: return this .type;
261: }
262:
263: /**
264: * Sets the paramter type to the given type.
265: *
266: * @param type the new type
267: * @exception IllegalArgumentException if:
268: * <ul>
269: * <li>the type is <code>null</code></li>
270: * <li>the node belongs to a different AST</li>
271: * <li>the node already has a parent</li>
272: * </ul>
273: */
274: public void setType(Type type) {
275: if (type == null) {
276: throw new IllegalArgumentException();
277: }
278: ASTNode oldChild = this .type;
279: preReplaceChild(oldChild, type, TYPE_PROPERTY);
280: this .type = type;
281: postReplaceChild(oldChild, type, TYPE_PROPERTY);
282: }
283:
284: /**
285: * Returns whether this method reference parameter is for
286: * the last parameter of a variable arity method (added in JLS3 API).
287: * <p>
288: * Note that the binding for the type <code>Foo</code>in the vararg method
289: * reference <code>#fun(Foo...)</code> is always for the type as
290: * written; i.e., the type binding for <code>Foo</code>. However, if you
291: * navigate from the MethodRef to its method binding to the
292: * type binding for its last parameter, the type binding for the vararg
293: * parameter is always an array type (i.e., <code>Foo[]</code>) reflecting
294: * the way vararg methods get compiled.
295: * </p>
296: *
297: * @return <code>true</code> if this is a variable arity parameter,
298: * and <code>false</code> otherwise
299: * @exception UnsupportedOperationException if this operation is used in
300: * a JLS2 AST
301: * @since 3.1
302: */
303: public boolean isVarargs() {
304: unsupportedIn2();
305: return this .variableArity;
306: }
307:
308: /**
309: * Sets whether this method reference parameter is for the last parameter of
310: * a variable arity method (added in JLS3 API).
311: *
312: * @param variableArity <code>true</code> if this is a variable arity
313: * parameter, and <code>false</code> otherwise
314: * @since 3.1
315: */
316: public void setVarargs(boolean variableArity) {
317: unsupportedIn2();
318: preValueChange(VARARGS_PROPERTY);
319: this .variableArity = variableArity;
320: postValueChange(VARARGS_PROPERTY);
321: }
322:
323: /**
324: * Returns the parameter name, or <code>null</code> if there is none.
325: *
326: * @return the parameter name node, or <code>null</code> if there is none
327: */
328: public SimpleName getName() {
329: return this .optionalParameterName;
330: }
331:
332: /**
333: * Sets or clears the parameter name.
334: *
335: * @param name the parameter name node, or <code>null</code> if
336: * there is none
337: * @exception IllegalArgumentException if:
338: * <ul>
339: * <li>the node belongs to a different AST</li>
340: * <li>the node already has a parent</li>
341: * </ul>
342: */
343: public void setName(SimpleName name) {
344: ASTNode oldChild = this .optionalParameterName;
345: preReplaceChild(oldChild, name, NAME_PROPERTY);
346: this .optionalParameterName = name;
347: postReplaceChild(oldChild, name, NAME_PROPERTY);
348: }
349:
350: /* (omit javadoc for this method)
351: * Method declared on ASTNode.
352: */
353: int memSize() {
354: return BASE_NODE_SIZE + 2 * 5;
355: }
356:
357: /* (omit javadoc for this method)
358: * Method declared on ASTNode.
359: */
360: int treeSize() {
361: return memSize()
362: + (this .type == null ? 0 : getType().treeSize())
363: + (this .optionalParameterName == null ? 0 : getName()
364: .treeSize());
365: }
366: }
|