001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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: * Simple or qualified "this" AST node type.
017: *
018: * <pre>
019: * ThisExpression:
020: * [ ClassName <b>.</b> ] <b>this</b>
021: * </pre>
022: * <p>
023: * See <code>FieldAccess</code> for guidelines on handling other expressions
024: * that resemble qualified names.
025: * </p>
026: *
027: * @see FieldAccess
028: * @since 2.0
029: */
030: public class ThisExpression extends Expression {
031:
032: /**
033: * The "qualifier" structural property of this node type.
034: * @since 3.0
035: */
036: public static final ChildPropertyDescriptor QUALIFIER_PROPERTY = new ChildPropertyDescriptor(
037: ThisExpression.class,
038: "qualifier", Name.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$
039:
040: /**
041: * A list of property descriptors (element type:
042: * {@link StructuralPropertyDescriptor}),
043: * or null if uninitialized.
044: */
045: private static final List PROPERTY_DESCRIPTORS;
046:
047: static {
048: List propertyList = new ArrayList(2);
049: createPropertyList(ThisExpression.class, propertyList);
050: addProperty(QUALIFIER_PROPERTY, propertyList);
051: PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
052: }
053:
054: /**
055: * Returns a list of structural property descriptors for this node type.
056: * Clients must not modify the result.
057: *
058: * @param apiLevel the API level; one of the
059: * <code>AST.JLS*</code> constants
060: * @return a list of property descriptors (element type:
061: * {@link StructuralPropertyDescriptor})
062: * @since 3.0
063: */
064: public static List propertyDescriptors(int apiLevel) {
065: return PROPERTY_DESCRIPTORS;
066: }
067:
068: /**
069: * The optional qualifier; <code>null</code> for none; defaults to none.
070: */
071: private Name optionalQualifier = null;
072:
073: /**
074: * Creates a new AST node for a "this" expression owned by the
075: * given AST. By default, there is no qualifier.
076: *
077: * @param ast the AST that is to own this node
078: */
079: ThisExpression(AST ast) {
080: super (ast);
081: }
082:
083: /* (omit javadoc for this method)
084: * Method declared on ASTNode.
085: */
086: final List internalStructuralPropertiesForType(int apiLevel) {
087: return propertyDescriptors(apiLevel);
088: }
089:
090: /* (omit javadoc for this method)
091: * Method declared on ASTNode.
092: */
093: final ASTNode internalGetSetChildProperty(
094: ChildPropertyDescriptor property, boolean get, ASTNode child) {
095: if (property == QUALIFIER_PROPERTY) {
096: if (get) {
097: return getQualifier();
098: } else {
099: setQualifier((Name) child);
100: return null;
101: }
102: }
103: // allow default implementation to flag the error
104: return super .internalGetSetChildProperty(property, get, child);
105: }
106:
107: /* (omit javadoc for this method)
108: * Method declared on ASTNode.
109: */
110: final int getNodeType0() {
111: return THIS_EXPRESSION;
112: }
113:
114: /* (omit javadoc for this method)
115: * Method declared on ASTNode.
116: */
117: ASTNode clone0(AST target) {
118: ThisExpression result = new ThisExpression(target);
119: result
120: .setSourceRange(this .getStartPosition(), this
121: .getLength());
122: result.setQualifier((Name) ASTNode.copySubtree(target,
123: getQualifier()));
124: return result;
125: }
126:
127: /* (omit javadoc for this method)
128: * Method declared on ASTNode.
129: */
130: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
131: // dispatch to correct overloaded match method
132: return matcher.match(this , other);
133: }
134:
135: /* (omit javadoc for this method)
136: * Method declared on ASTNode.
137: */
138: void accept0(ASTVisitor visitor) {
139: boolean visitChildren = visitor.visit(this );
140: if (visitChildren) {
141: acceptChild(visitor, getQualifier());
142: }
143: visitor.endVisit(this );
144: }
145:
146: /**
147: * Returns the qualifier of this "this" expression, or
148: * <code>null</code> if there is none.
149: *
150: * @return the qualifier name node, or <code>null</code> if there is none
151: */
152: public Name getQualifier() {
153: return this .optionalQualifier;
154: }
155:
156: /**
157: * Sets or clears the qualifier of this "this" expression.
158: *
159: * @param name the qualifier name node, or <code>null</code> if
160: * there is none
161: * @exception IllegalArgumentException if:
162: * <ul>
163: * <li>the node belongs to a different AST</li>
164: * <li>the node already has a parent</li>
165: * </ul>
166: */
167: public void setQualifier(Name name) {
168: ASTNode oldChild = this .optionalQualifier;
169: preReplaceChild(oldChild, name, QUALIFIER_PROPERTY);
170: this .optionalQualifier = name;
171: postReplaceChild(oldChild, name, QUALIFIER_PROPERTY);
172: }
173:
174: /* (omit javadoc for this method)
175: * Method declared on ASTNode.
176: */
177: int memSize() {
178: // treat Operator as free
179: return BASE_NODE_SIZE + 1 * 4;
180: }
181:
182: /* (omit javadoc for this method)
183: * Method declared on ASTNode.
184: */
185: int treeSize() {
186: return memSize()
187: + (this .optionalQualifier == null ? 0 : getQualifier()
188: .treeSize());
189: }
190: }
|