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: * Type node for a named class type, a named interface type, or a type variable.
017: * <p>
018: * This kind of node is used to convert a name (<code>Name</code>) into a type
019: * (<code>Type</code>) by wrapping it.
020: * </p>
021: *
022: * @since 2.0
023: */
024: public class SimpleType extends Type {
025:
026: /**
027: * The "name" structural property of this node type.
028: * @since 3.0
029: */
030: public static final ChildPropertyDescriptor NAME_PROPERTY = new ChildPropertyDescriptor(
031: SimpleType.class,
032: "name", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
033:
034: /**
035: * A list of property descriptors (element type:
036: * {@link StructuralPropertyDescriptor}),
037: * or null if uninitialized.
038: */
039: private static final List PROPERTY_DESCRIPTORS;
040:
041: static {
042: List propertyList = new ArrayList(2);
043: createPropertyList(SimpleType.class, propertyList);
044: addProperty(NAME_PROPERTY, propertyList);
045: PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
046: }
047:
048: /**
049: * Returns a list of structural property descriptors for this node type.
050: * Clients must not modify the result.
051: *
052: * @param apiLevel the API level; one of the
053: * <code>AST.JLS*</code> constants
054: * @return a list of property descriptors (element type:
055: * {@link StructuralPropertyDescriptor})
056: * @since 3.0
057: */
058: public static List propertyDescriptors(int apiLevel) {
059: return PROPERTY_DESCRIPTORS;
060: }
061:
062: /**
063: * The type name node; lazily initialized; defaults to a type with
064: * an unspecfied, but legal, name.
065: */
066: private Name typeName = null;
067:
068: /**
069: * Creates a new unparented node for a simple type owned by the given AST.
070: * By default, an unspecified, but legal, name.
071: * <p>
072: * N.B. This constructor is package-private.
073: * </p>
074: *
075: * @param ast the AST that is to own this node
076: */
077: SimpleType(AST ast) {
078: super (ast);
079: }
080:
081: /* (omit javadoc for this method)
082: * Method declared on ASTNode.
083: */
084: final List internalStructuralPropertiesForType(int apiLevel) {
085: return propertyDescriptors(apiLevel);
086: }
087:
088: /* (omit javadoc for this method)
089: * Method declared on ASTNode.
090: */
091: final ASTNode internalGetSetChildProperty(
092: ChildPropertyDescriptor property, boolean get, ASTNode child) {
093: if (property == NAME_PROPERTY) {
094: if (get) {
095: return getName();
096: } else {
097: setName((Name) child);
098: return null;
099: }
100: }
101: // allow default implementation to flag the error
102: return super .internalGetSetChildProperty(property, get, child);
103: }
104:
105: /* (omit javadoc for this method)
106: * Method declared on ASTNode.
107: */
108: final int getNodeType0() {
109: return SIMPLE_TYPE;
110: }
111:
112: /* (omit javadoc for this method)
113: * Method declared on ASTNode.
114: */
115: ASTNode clone0(AST target) {
116: SimpleType result = new SimpleType(target);
117: result
118: .setSourceRange(this .getStartPosition(), this
119: .getLength());
120: result.setName((Name) (getName()).clone(target));
121: return result;
122: }
123:
124: /* (omit javadoc for this method)
125: * Method declared on ASTNode.
126: */
127: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
128: // dispatch to correct overloaded match method
129: return matcher.match(this , other);
130: }
131:
132: /* (omit javadoc for this method)
133: * Method declared on ASTNode.
134: */
135: void accept0(ASTVisitor visitor) {
136: boolean visitChildren = visitor.visit(this );
137: if (visitChildren) {
138: acceptChild(visitor, getName());
139: }
140: visitor.endVisit(this );
141: }
142:
143: /**
144: * Returns the name of this simple type.
145: *
146: * @return the name of this simple type
147: */
148: public Name getName() {
149: if (this .typeName == null) {
150: // lazy init must be thread-safe for readers
151: synchronized (this ) {
152: if (this .typeName == null) {
153: preLazyInit();
154: this .typeName = new SimpleName(this .ast);
155: postLazyInit(this .typeName, NAME_PROPERTY);
156: }
157: }
158: }
159: return this .typeName;
160: }
161:
162: /**
163: * Sets the name of this simple type to the given name.
164: *
165: * @param typeName the new name of this simple type
166: * @exception IllegalArgumentException if:
167: * <ul>
168: * <li>the node belongs to a different AST</li>
169: * <li>the node already has a parent</li>
170: * </ul>
171: */
172: public void setName(Name typeName) {
173: if (typeName == null) {
174: throw new IllegalArgumentException();
175: }
176: ASTNode oldChild = this .typeName;
177: preReplaceChild(oldChild, typeName, NAME_PROPERTY);
178: this .typeName = typeName;
179: postReplaceChild(oldChild, typeName, NAME_PROPERTY);
180: }
181:
182: /* (omit javadoc for this method)
183: * Method declared on ASTNode.
184: */
185: int memSize() {
186: // treat Code as free
187: return BASE_NODE_SIZE + 1 * 4;
188: }
189:
190: /* (omit javadoc for this method)
191: * Method declared on ASTNode.
192: */
193: int treeSize() {
194: return memSize()
195: + (this .typeName == null ? 0 : getName().treeSize());
196: }
197: }
|