1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
19 * AST node for a qualified name. A qualified name is defined recursively
20 * as a simple name preceded by a name, which qualifies it. Expressing it this
21 * way means that the qualifier and the simple name get their own AST nodes.
24 * Name <b>.</b> SimpleName
27 * See <code>FieldAccess</code> for guidelines on handling other expressions
28 * that resemble qualified names.
33 * @noinstantiate This class is not intended to be instantiated by clients.
35 public class QualifiedName extends Name {
38 * The "qualifier" structural property of this node type.
41 public static final ChildPropertyDescriptor QUALIFIER_PROPERTY =
42 new ChildPropertyDescriptor(QualifiedName.class, "qualifier", Name.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
45 * The "name" structural property of this node type.
48 public static final ChildPropertyDescriptor NAME_PROPERTY =
49 new ChildPropertyDescriptor(QualifiedName.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
52 * A list of property descriptors (element type:
53 * {@link StructuralPropertyDescriptor}),
54 * or null if uninitialized.
56 private static final List PROPERTY_DESCRIPTORS;
59 List propertyList = new ArrayList(3);
60 createPropertyList(QualifiedName.class, propertyList);
61 addProperty(QUALIFIER_PROPERTY, propertyList);
62 addProperty(NAME_PROPERTY, propertyList);
63 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
67 * Returns a list of structural property descriptors for this node type.
68 * Clients must not modify the result.
70 * @param apiLevel the API level; one of the
71 * <code>AST.JLS*</code> constants
72 * @return a list of property descriptors (element type:
73 * {@link StructuralPropertyDescriptor})
76 public static List propertyDescriptors(int apiLevel) {
77 return PROPERTY_DESCRIPTORS;
81 * The identifier; lazily initialized; defaults to a unspecified, legal
84 private Name qualifier = null;
87 * The name being qualified; lazily initialized; defaults to a unspecified,
88 * legal Java identifier.
90 private SimpleName name = null;
93 * Creates a new AST node for a qualified name owned by the given AST.
95 * N.B. This constructor is package-private; all subclasses must be
96 * declared in the same package; clients are unable to declare
97 * additional subclasses.
100 * @param ast the AST that is to own this node
102 QualifiedName(AST ast) {
106 /* (omit javadoc for this method)
107 * Method declared on ASTNode.
109 final List internalStructuralPropertiesForType(int apiLevel) {
110 return propertyDescriptors(apiLevel);
113 /* (omit javadoc for this method)
114 * Method declared on ASTNode.
116 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
117 if (property == QUALIFIER_PROPERTY) {
119 return getQualifier();
121 setQualifier((Name) child);
125 if (property == NAME_PROPERTY) {
129 setName((SimpleName) child);
133 // allow default implementation to flag the error
134 return super.internalGetSetChildProperty(property, get, child);
137 /* (omit javadoc for this method)
138 * Method declared on ASTNode.
140 final int getNodeType0() {
141 return QUALIFIED_NAME;
144 /* (omit javadoc for this method)
145 * Method declared on ASTNode.
147 ASTNode clone0(AST target) {
148 QualifiedName result = new QualifiedName(target);
149 result.setSourceRange(this.getStartPosition(), this.getLength());
150 result.setQualifier((Name) getQualifier().clone(target));
151 result.setName((SimpleName) getName().clone(target));
155 /* (omit javadoc for this method)
156 * Method declared on ASTNode.
158 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
159 // dispatch to correct overloaded match method
160 return matcher.match(this, other);
163 /* (omit javadoc for this method)
164 * Method declared on ASTNode.
166 void accept0(ASTVisitor visitor) {
167 boolean visitChildren = visitor.visit(this);
169 // visit children in normal left to right reading order
170 acceptChild(visitor, getQualifier());
171 acceptChild(visitor, getName());
173 visitor.endVisit(this);
177 * Returns the qualifier part of this qualified name.
179 * @return the qualifier part of this qualified name
181 public Name getQualifier() {
182 if (this.qualifier == null) {
183 // lazy init must be thread-safe for readers
184 synchronized (this) {
185 if (this.qualifier == null) {
187 this.qualifier = new SimpleName(this.ast);
188 postLazyInit(this.qualifier, QUALIFIER_PROPERTY);
192 return this.qualifier;
196 * Sets the qualifier of this qualified name to the given name.
198 * @param qualifier the qualifier of this qualified name
199 * @exception IllegalArgumentException if:
201 * <li>the node belongs to a different AST</li>
202 * <li>the node already has a parent</li>
203 * <li>a cycle in would be created</li>
206 public void setQualifier(Name qualifier) {
207 if (qualifier == null) {
208 throw new IllegalArgumentException();
210 ASTNode oldChild = this.qualifier;
211 preReplaceChild(oldChild, qualifier, QUALIFIER_PROPERTY);
212 this.qualifier = qualifier;
213 postReplaceChild(oldChild, qualifier, QUALIFIER_PROPERTY);
217 * Returns the name part of this qualified name.
219 * @return the name being qualified
221 public SimpleName getName() {
222 if (this.name == null) {
223 // lazy init must be thread-safe for readers
224 synchronized (this) {
225 if (this.name == null) {
227 this.name = new SimpleName(this.ast);
228 postLazyInit(this.name, NAME_PROPERTY);
236 * Sets the name part of this qualified name to the given simple name.
238 * @param name the identifier of this qualified name
239 * @exception IllegalArgumentException if:
241 * <li>the node belongs to a different AST</li>
242 * <li>the node already has a parent</li>
245 public void setName(SimpleName name) {
247 throw new IllegalArgumentException();
249 ASTNode oldChild = this.name;
250 preReplaceChild(oldChild, name, NAME_PROPERTY);
252 postReplaceChild(oldChild, name, NAME_PROPERTY);
255 /* (omit javadoc for this method)
256 * Method declared on Name.
258 void appendName(StringBuffer buffer) {
259 getQualifier().appendName(buffer);
261 getName().appendName(buffer);
264 /* (omit javadoc for this method)
265 * Method declared on ASTNode.
268 return BASE_NAME_NODE_SIZE + 3 * 4;
271 /* (omit javadoc for this method)
272 * Method declared on ASTNode.
277 + (this.name == null ? 0 : getName().treeSize())
278 + (this.qualifier == null ? 0 : getQualifier().treeSize());