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.Iterator;
014: import java.util.List;
015:
016: /**
017: * Single variable declaration AST node type. Single variable
018: * declaration nodes are used in a limited number of places, including formal
019: * parameter lists and catch clauses. They are not used for field declarations
020: * and regular variable declaration statements.
021: * For JLS2:
022: * <pre>
023: * SingleVariableDeclaration:
024: * { Modifier } Type Identifier { <b>[</b><b>]</b> } [ <b>=</b> Expression ]
025: * </pre>
026: * For JLS3, the modifier flags were replaced by
027: * a list of modifier nodes (intermixed with annotations), and the variable arity
028: * indicator was added:
029: * <pre>
030: * SingleVariableDeclaration:
031: * { ExtendedModifier } Type [ <b>...</b> ] Identifier { <b>[</b><b>]</b> } [ <b>=</b> Expression ]
032: * </pre>
033: *
034: * @since 2.0
035: */
036: public class SingleVariableDeclaration extends VariableDeclaration {
037:
038: /**
039: * The "modifiers" structural property of this node type (JLS2 API only).
040: * @since 3.0
041: */
042: public static final SimplePropertyDescriptor MODIFIERS_PROPERTY = new SimplePropertyDescriptor(
043: SingleVariableDeclaration.class,
044: "modifiers", int.class, MANDATORY); //$NON-NLS-1$
045:
046: /**
047: * The "modifiers" structural property of this node type (added in JLS3 API).
048: * @since 3.1
049: */
050: public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY = new ChildListPropertyDescriptor(
051: SingleVariableDeclaration.class,
052: "modifiers", IExtendedModifier.class, CYCLE_RISK); //$NON-NLS-1$
053:
054: /**
055: * The "name" structural property of this node type.
056: * @since 3.0
057: */
058: public static final ChildPropertyDescriptor NAME_PROPERTY = new ChildPropertyDescriptor(
059: SingleVariableDeclaration.class,
060: "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
061:
062: /**
063: * The "type" structural property of this node type.
064: * @since 3.0
065: */
066: public static final ChildPropertyDescriptor TYPE_PROPERTY = new ChildPropertyDescriptor(
067: SingleVariableDeclaration.class,
068: "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
069:
070: /**
071: * The "varargs" structural property of this node type (added in JLS3 API).
072: * @since 3.1
073: */
074: public static final SimplePropertyDescriptor VARARGS_PROPERTY = new SimplePropertyDescriptor(
075: SingleVariableDeclaration.class,
076: "varargs", boolean.class, MANDATORY); //$NON-NLS-1$
077:
078: /**
079: * The "extraDimensions" structural property of this node type.
080: * @since 3.0
081: */
082: public static final SimplePropertyDescriptor EXTRA_DIMENSIONS_PROPERTY = new SimplePropertyDescriptor(
083: SingleVariableDeclaration.class,
084: "extraDimensions", int.class, MANDATORY); //$NON-NLS-1$
085:
086: /**
087: * The "initializer" structural property of this node type.
088: * @since 3.0
089: */
090: public static final ChildPropertyDescriptor INITIALIZER_PROPERTY = new ChildPropertyDescriptor(
091: SingleVariableDeclaration.class,
092: "initializer", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
093:
094: /**
095: * A list of property descriptors (element type:
096: * {@link StructuralPropertyDescriptor}),
097: * or null if uninitialized.
098: * @since 3.0
099: */
100: private static final List PROPERTY_DESCRIPTORS_2_0;
101:
102: /**
103: * A list of property descriptors (element type:
104: * {@link StructuralPropertyDescriptor}),
105: * or null if uninitialized.
106: * @since 3.1
107: */
108: private static final List PROPERTY_DESCRIPTORS_3_0;
109:
110: static {
111: List propertyList = new ArrayList(6);
112: createPropertyList(SingleVariableDeclaration.class,
113: propertyList);
114: addProperty(MODIFIERS_PROPERTY, propertyList);
115: addProperty(TYPE_PROPERTY, propertyList);
116: addProperty(NAME_PROPERTY, propertyList);
117: addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList);
118: addProperty(INITIALIZER_PROPERTY, propertyList);
119: PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(propertyList);
120:
121: propertyList = new ArrayList(7);
122: createPropertyList(SingleVariableDeclaration.class,
123: propertyList);
124: addProperty(MODIFIERS2_PROPERTY, propertyList);
125: addProperty(TYPE_PROPERTY, propertyList);
126: addProperty(VARARGS_PROPERTY, propertyList);
127: addProperty(NAME_PROPERTY, propertyList);
128: addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList);
129: addProperty(INITIALIZER_PROPERTY, propertyList);
130: PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList);
131: }
132:
133: /**
134: * Returns a list of structural property descriptors for this node type.
135: * Clients must not modify the result.
136: *
137: * @param apiLevel the API level; one of the
138: * <code>AST.JLS*</code> constants
139: * @return a list of property descriptors (element type:
140: * {@link StructuralPropertyDescriptor})
141: * @since 3.0
142: */
143: public static List propertyDescriptors(int apiLevel) {
144: if (apiLevel == AST.JLS2_INTERNAL) {
145: return PROPERTY_DESCRIPTORS_2_0;
146: } else {
147: return PROPERTY_DESCRIPTORS_3_0;
148: }
149: }
150:
151: /**
152: * The extended modifiers (element type: <code>IExtendedModifier</code>).
153: * Null in JLS2. Added in JLS3; defaults to an empty list
154: * (see constructor).
155: *
156: * @since 3.1
157: */
158: private ASTNode.NodeList modifiers = null;
159:
160: /**
161: * The modifiers; bit-wise or of Modifier flags.
162: * Defaults to none. Not used in 3.0.
163: */
164: private int modifierFlags = Modifier.NONE;
165:
166: /**
167: * The variable name; lazily initialized; defaults to a unspecified,
168: * legal Java identifier.
169: */
170: private SimpleName variableName = null;
171:
172: /**
173: * The type; lazily initialized; defaults to a unspecified,
174: * legal type.
175: */
176: private Type type = null;
177:
178: /**
179: * Indicates the last parameter of a variable arity method;
180: * defaults to false.
181: *
182: * @since 3.1
183: */
184: private boolean variableArity = false;
185:
186: /**
187: * The number of extra array dimensions that appear after the variable;
188: * defaults to 0.
189: *
190: * @since 2.1
191: */
192: private int extraArrayDimensions = 0;
193:
194: /**
195: * The initializer expression, or <code>null</code> if none;
196: * defaults to none.
197: */
198: private Expression optionalInitializer = null;
199:
200: /**
201: * Creates a new AST node for a variable declaration owned by the given
202: * AST. By default, the variable declaration has: no modifiers, an
203: * unspecified (but legal) type, an unspecified (but legal) variable name,
204: * 0 dimensions after the variable; no initializer; not variable arity.
205: * <p>
206: * N.B. This constructor is package-private.
207: * </p>
208: *
209: * @param ast the AST that is to own this node
210: */
211: SingleVariableDeclaration(AST ast) {
212: super (ast);
213: if (ast.apiLevel >= AST.JLS3) {
214: this .modifiers = new ASTNode.NodeList(MODIFIERS2_PROPERTY);
215: }
216: }
217:
218: /* (omit javadoc for this method)
219: * Method declared on VariableDeclaration.
220: * @since 3.1
221: */
222: final SimplePropertyDescriptor internalExtraDimensionsProperty() {
223: return EXTRA_DIMENSIONS_PROPERTY;
224: }
225:
226: /* (omit javadoc for this method)
227: * Method declared on VariableDeclaration.
228: * @since 3.1
229: */
230: final ChildPropertyDescriptor internalInitializerProperty() {
231: return INITIALIZER_PROPERTY;
232: }
233:
234: /* (omit javadoc for this method)
235: * Method declared on VariableDeclaration.
236: * @since 3.1
237: */
238: final ChildPropertyDescriptor internalNameProperty() {
239: return NAME_PROPERTY;
240: }
241:
242: /* (omit javadoc for this method)
243: * Method declared on ASTNode.
244: */
245: final List internalStructuralPropertiesForType(int apiLevel) {
246: return propertyDescriptors(apiLevel);
247: }
248:
249: /* (omit javadoc for this method)
250: * Method declared on ASTNode.
251: */
252: final int internalGetSetIntProperty(
253: SimplePropertyDescriptor property, boolean get, int value) {
254: if (property == MODIFIERS_PROPERTY) {
255: if (get) {
256: return getModifiers();
257: } else {
258: setModifiers(value);
259: return 0;
260: }
261: }
262: if (property == EXTRA_DIMENSIONS_PROPERTY) {
263: if (get) {
264: return getExtraDimensions();
265: } else {
266: setExtraDimensions(value);
267: return 0;
268: }
269: }
270: // allow default implementation to flag the error
271: return super .internalGetSetIntProperty(property, get, value);
272: }
273:
274: /* (omit javadoc for this method)
275: * Method declared on ASTNode.
276: */
277: final boolean internalGetSetBooleanProperty(
278: SimplePropertyDescriptor property, boolean get,
279: boolean value) {
280: if (property == VARARGS_PROPERTY) {
281: if (get) {
282: return isVarargs();
283: } else {
284: setVarargs(value);
285: return false;
286: }
287: }
288: // allow default implementation to flag the error
289: return super
290: .internalGetSetBooleanProperty(property, get, value);
291: }
292:
293: /* (omit javadoc for this method)
294: * Method declared on ASTNode.
295: */
296: final ASTNode internalGetSetChildProperty(
297: ChildPropertyDescriptor property, boolean get, ASTNode child) {
298: if (property == NAME_PROPERTY) {
299: if (get) {
300: return getName();
301: } else {
302: setName((SimpleName) child);
303: return null;
304: }
305: }
306: if (property == TYPE_PROPERTY) {
307: if (get) {
308: return getType();
309: } else {
310: setType((Type) child);
311: return null;
312: }
313: }
314: if (property == INITIALIZER_PROPERTY) {
315: if (get) {
316: return getInitializer();
317: } else {
318: setInitializer((Expression) child);
319: return null;
320: }
321: }
322: // allow default implementation to flag the error
323: return super .internalGetSetChildProperty(property, get, child);
324: }
325:
326: /* (omit javadoc for this method)
327: * Method declared on ASTNode.
328: */
329: final List internalGetChildListProperty(
330: ChildListPropertyDescriptor property) {
331: if (property == MODIFIERS2_PROPERTY) {
332: return modifiers();
333: }
334: // allow default implementation to flag the error
335: return super .internalGetChildListProperty(property);
336: }
337:
338: /* (omit javadoc for this method)
339: * Method declared on ASTNode.
340: */
341: final int getNodeType0() {
342: return SINGLE_VARIABLE_DECLARATION;
343: }
344:
345: /* (omit javadoc for this method)
346: * Method declared on ASTNode.
347: */
348: ASTNode clone0(AST target) {
349: SingleVariableDeclaration result = new SingleVariableDeclaration(
350: target);
351: result
352: .setSourceRange(this .getStartPosition(), this
353: .getLength());
354: if (this .ast.apiLevel == AST.JLS2_INTERNAL) {
355: result.setModifiers(getModifiers());
356: } else {
357: result.modifiers().addAll(
358: ASTNode.copySubtrees(target, modifiers()));
359: result.setVarargs(isVarargs());
360: }
361: result.setType((Type) getType().clone(target));
362: result.setExtraDimensions(getExtraDimensions());
363: result.setName((SimpleName) getName().clone(target));
364: result.setInitializer((Expression) ASTNode.copySubtree(target,
365: getInitializer()));
366: return result;
367: }
368:
369: /* (omit javadoc for this method)
370: * Method declared on ASTNode.
371: */
372: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
373: // dispatch to correct overloaded match method
374: return matcher.match(this , other);
375: }
376:
377: /* (omit javadoc for this method)
378: * Method declared on ASTNode.
379: */
380: void accept0(ASTVisitor visitor) {
381: boolean visitChildren = visitor.visit(this );
382: if (visitChildren) {
383: // visit children in normal left to right reading order
384: if (this .ast.apiLevel >= AST.JLS3) {
385: acceptChildren(visitor, this .modifiers);
386: }
387: acceptChild(visitor, getType());
388: acceptChild(visitor, getName());
389: acceptChild(visitor, getInitializer());
390: }
391: visitor.endVisit(this );
392: }
393:
394: /**
395: * Returns the live ordered list of modifiers and annotations
396: * of this declaration (added in JLS3 API).
397: * <p>
398: * Note that the final modifier is the only meaningful modifier for local
399: * variable and formal parameter declarations.
400: * </p>
401: *
402: * @return the live list of modifiers and annotations
403: * (element type: <code>IExtendedModifier</code>)
404: * @exception UnsupportedOperationException if this operation is used in
405: * a JLS2 AST
406: * @since 3.1
407: */
408: public List modifiers() {
409: // more efficient than just calling unsupportedIn2() to check
410: if (this .modifiers == null) {
411: unsupportedIn2();
412: }
413: return this .modifiers;
414: }
415:
416: /**
417: * Returns the modifiers explicitly specified on this declaration.
418: * <p>
419: * In the JLS3 API, this method is a convenience method that
420: * computes these flags from <code>modifiers()</code>.
421: * </p>
422: *
423: * @return the bit-wise or of <code>Modifier</code> constants
424: * @see Modifier
425: */
426: public int getModifiers() {
427: // more efficient than checking getAST().API_LEVEL
428: if (this .modifiers == null) {
429: // JLS2 behavior - bona fide property
430: return this .modifierFlags;
431: } else {
432: // JLS3 behavior - convenient method
433: // performance could be improved by caching computed flags
434: // but this would require tracking changes to this.modifiers
435: int computedModifierFlags = Modifier.NONE;
436: for (Iterator it = modifiers().iterator(); it.hasNext();) {
437: Object x = it.next();
438: if (x instanceof Modifier) {
439: computedModifierFlags |= ((Modifier) x)
440: .getKeyword().toFlagValue();
441: }
442: }
443: return computedModifierFlags;
444: }
445: }
446:
447: /**
448: * Sets the modifiers explicitly specified on this declaration (JLS2 API only).
449: * <p>
450: * The following modifiers are meaningful for fields: public, private, protected,
451: * static, final, volatile, and transient. For local variable and formal
452: * parameter declarations, the only meaningful modifier is final.
453: * </p>
454: *
455: * @param modifiers the given modifiers (bit-wise or of <code>Modifier</code> constants)
456: * @exception UnsupportedOperationException if this operation is used in
457: * an AST later than JLS2
458: * @see Modifier
459: * @deprecated In the JLS3 API, this method is replaced by
460: * {@link #modifiers()} which contains a list of a <code>Modifier</code> nodes.
461: */
462: public void setModifiers(int modifiers) {
463: internalSetModifiers(modifiers);
464: }
465:
466: /**
467: * Internal synonym for deprecated method. Used to avoid
468: * deprecation warnings.
469: * @since 3.1
470: */
471: /*package*/final void internalSetModifiers(int pmodifiers) {
472: supportedOnlyIn2();
473: preValueChange(MODIFIERS_PROPERTY);
474: this .modifierFlags = pmodifiers;
475: postValueChange(MODIFIERS_PROPERTY);
476: }
477:
478: /* (omit javadoc for this method)
479: * Method declared on VariableDeclaration.
480: */
481: public SimpleName getName() {
482: if (this .variableName == null) {
483: // lazy init must be thread-safe for readers
484: synchronized (this ) {
485: if (this .variableName == null) {
486: preLazyInit();
487: this .variableName = new SimpleName(this .ast);
488: postLazyInit(this .variableName, NAME_PROPERTY);
489: }
490: }
491: }
492: return this .variableName;
493: }
494:
495: /* (omit javadoc for this method)
496: * Method declared on VariableDeclaration.
497: */
498: public void setName(SimpleName variableName) {
499: if (variableName == null) {
500: throw new IllegalArgumentException();
501: }
502: ASTNode oldChild = this .variableName;
503: preReplaceChild(oldChild, variableName, NAME_PROPERTY);
504: this .variableName = variableName;
505: postReplaceChild(oldChild, variableName, NAME_PROPERTY);
506: }
507:
508: /**
509: * Returns the type of the variable declared in this variable declaration,
510: * exclusive of any extra array dimensions.
511: *
512: * @return the type
513: */
514: public Type getType() {
515: if (this .type == null) {
516: // lazy init must be thread-safe for readers
517: synchronized (this ) {
518: if (this .type == null) {
519: preLazyInit();
520: this .type = this .ast
521: .newPrimitiveType(PrimitiveType.INT);
522: postLazyInit(this .type, TYPE_PROPERTY);
523: }
524: }
525: }
526: return this .type;
527: }
528:
529: /**
530: * Sets the type of the variable declared in this variable declaration to
531: * the given type, exclusive of any extra array dimensions.
532: *
533: * @param type the new type
534: * @exception IllegalArgumentException if:
535: * <ul>
536: * <li>the node belongs to a different AST</li>
537: * <li>the node already has a parent</li>
538: * </ul>
539: */
540: public void setType(Type type) {
541: if (type == null) {
542: throw new IllegalArgumentException();
543: }
544: ASTNode oldChild = this .type;
545: preReplaceChild(oldChild, type, TYPE_PROPERTY);
546: this .type = type;
547: postReplaceChild(oldChild, type, TYPE_PROPERTY);
548: }
549:
550: /**
551: * Returns whether this declaration declares the last parameter of
552: * a variable arity method (added in JLS3 API).
553: * <p>
554: * Note that the binding for the type <code>Foo</code>in the vararg method
555: * declaration <code>void fun(Foo... args)</code> is always for the type as
556: * written; i.e., the type binding for <code>Foo</code>. However, if you
557: * navigate from the method declaration to its method binding to the
558: * type binding for its last parameter, the type binding for the vararg
559: * parameter is always an array type (i.e., <code>Foo[]</code>) reflecting
560: * the way vararg methods get compiled.
561: * </p>
562: *
563: * @return <code>true</code> if this is a variable arity parameter declaration,
564: * and <code>false</code> otherwise
565: * @exception UnsupportedOperationException if this operation is used in
566: * a JLS2 AST
567: * @since 3.1
568: */
569: public boolean isVarargs() {
570: // more efficient than just calling unsupportedIn2() to check
571: if (this .modifiers == null) {
572: unsupportedIn2();
573: }
574: return this .variableArity;
575: }
576:
577: /**
578: * Sets whether this declaration declares the last parameter of
579: * a variable arity method (added in JLS3 API).
580: *
581: * @param variableArity <code>true</code> if this is a variable arity
582: * parameter declaration, and <code>false</code> otherwise
583: * @since 3.1
584: */
585: public void setVarargs(boolean variableArity) {
586: // more efficient than just calling unsupportedIn2() to check
587: if (this .modifiers == null) {
588: unsupportedIn2();
589: }
590: preValueChange(VARARGS_PROPERTY);
591: this .variableArity = variableArity;
592: postValueChange(VARARGS_PROPERTY);
593: }
594:
595: /* (omit javadoc for this method)
596: * Method declared on VariableDeclaration.
597: * @since 2.1
598: */
599: public int getExtraDimensions() {
600: return this .extraArrayDimensions;
601: }
602:
603: /* (omit javadoc for this method)
604: * Method declared on VariableDeclaration.
605: * @since 2.1
606: */
607: public void setExtraDimensions(int dimensions) {
608: if (dimensions < 0) {
609: throw new IllegalArgumentException();
610: }
611: preValueChange(EXTRA_DIMENSIONS_PROPERTY);
612: this .extraArrayDimensions = dimensions;
613: postValueChange(EXTRA_DIMENSIONS_PROPERTY);
614: }
615:
616: /* (omit javadoc for this method)
617: * Method declared on VariableDeclaration.
618: */
619: public Expression getInitializer() {
620: return this .optionalInitializer;
621: }
622:
623: /* (omit javadoc for this method)
624: * Method declared on VariableDeclaration.
625: */
626: public void setInitializer(Expression initializer) {
627: // a SingleVariableDeclaration may occur inside an Expression
628: // must check cycles
629: ASTNode oldChild = this .optionalInitializer;
630: preReplaceChild(oldChild, initializer, INITIALIZER_PROPERTY);
631: this .optionalInitializer = initializer;
632: postReplaceChild(oldChild, initializer, INITIALIZER_PROPERTY);
633: }
634:
635: /* (omit javadoc for this method)
636: * Method declared on ASTNode.
637: */
638: int memSize() {
639: // treat Operator as free
640: return BASE_NODE_SIZE + 7 * 4;
641: }
642:
643: /* (omit javadoc for this method)
644: * Method declared on ASTNode.
645: */
646: int treeSize() {
647: return memSize()
648: + (this .modifiers == null ? 0 : this .modifiers
649: .listSize())
650: + (this .type == null ? 0 : getType().treeSize())
651: + (this .variableName == null ? 0 : getName().treeSize())
652: + (this .optionalInitializer == null ? 0
653: : getInitializer().treeSize());
654: }
655: }
|