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;
18 * Type node for a named class type, a named interface type, or a type variable.
20 * This kind of node is used to convert a name (<code>Name</code>) into a type
21 * (<code>Type</code>) by wrapping it.
25 * @noinstantiate This class is not intended to be instantiated by clients.
27 public class SimpleType extends Type {
30 * The "name" structural property of this node type.
33 public static final ChildPropertyDescriptor NAME_PROPERTY =
34 new ChildPropertyDescriptor(SimpleType.class, "name", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
37 * A list of property descriptors (element type:
38 * {@link StructuralPropertyDescriptor}),
39 * or null if uninitialized.
41 private static final List PROPERTY_DESCRIPTORS;
44 List propertyList = new ArrayList(2);
45 createPropertyList(SimpleType.class, propertyList);
46 addProperty(NAME_PROPERTY, propertyList);
47 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
51 * Returns a list of structural property descriptors for this node type.
52 * Clients must not modify the result.
54 * @param apiLevel the API level; one of the
55 * <code>AST.JLS*</code> constants
56 * @return a list of property descriptors (element type:
57 * {@link StructuralPropertyDescriptor})
60 public static List propertyDescriptors(int apiLevel) {
61 return PROPERTY_DESCRIPTORS;
65 * The type name node; lazily initialized; defaults to a type with
66 * an unspecfied, but legal, name.
68 private Name typeName = null;
71 * Creates a new unparented node for a simple type owned by the given AST.
72 * By default, an unspecified, but legal, name.
74 * N.B. This constructor is package-private.
77 * @param ast the AST that is to own this node
83 /* (omit javadoc for this method)
84 * Method declared on ASTNode.
86 final List internalStructuralPropertiesForType(int apiLevel) {
87 return propertyDescriptors(apiLevel);
90 /* (omit javadoc for this method)
91 * Method declared on ASTNode.
93 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
94 if (property == NAME_PROPERTY) {
98 setName((Name) child);
102 // allow default implementation to flag the error
103 return super.internalGetSetChildProperty(property, get, child);
106 /* (omit javadoc for this method)
107 * Method declared on ASTNode.
109 final int getNodeType0() {
113 /* (omit javadoc for this method)
114 * Method declared on ASTNode.
116 ASTNode clone0(AST target) {
117 SimpleType result = new SimpleType(target);
118 result.setSourceRange(this.getStartPosition(), this.getLength());
119 result.setName((Name) (getName()).clone(target));
123 /* (omit javadoc for this method)
124 * Method declared on ASTNode.
126 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
127 // dispatch to correct overloaded match method
128 return matcher.match(this, other);
131 /* (omit javadoc for this method)
132 * Method declared on ASTNode.
134 void accept0(ASTVisitor visitor) {
135 boolean visitChildren = visitor.visit(this);
137 acceptChild(visitor, getName());
139 visitor.endVisit(this);
143 * Returns the name of this simple type.
145 * @return the name of this simple type
147 public Name getName() {
148 if (this.typeName == null) {
149 // lazy init must be thread-safe for readers
150 synchronized (this) {
151 if (this.typeName == null) {
153 this.typeName = new SimpleName(this.ast);
154 postLazyInit(this.typeName, NAME_PROPERTY);
158 return this.typeName;
162 * Sets the name of this simple type to the given name.
164 * @param typeName the new name of this simple type
165 * @exception IllegalArgumentException if:
167 * <li>the node belongs to a different AST</li>
168 * <li>the node already has a parent</li>
171 public void setName(Name typeName) {
172 if (typeName == null) {
173 throw new IllegalArgumentException();
175 ASTNode oldChild = this.typeName;
176 preReplaceChild(oldChild, typeName, NAME_PROPERTY);
177 this.typeName = typeName;
178 postReplaceChild(oldChild, typeName, NAME_PROPERTY);
181 /* (omit javadoc for this method)
182 * Method declared on ASTNode.
185 // treat Code as free
186 return BASE_NODE_SIZE + 1 * 4;
189 /* (omit javadoc for this method)
190 * Method declared on ASTNode.
195 + (this.typeName == null ? 0 : getName().treeSize());