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.HashMap;
16 import java.util.List;
20 * Primitive type nodes.
34 * Note that due to the fact that AST nodes belong to a specific AST and
35 * have a specific parent, there needs to multiple instances of these
40 * @noinstantiate This class is not intended to be instantiated by clients.
42 public class PrimitiveType extends Type {
45 * Primitive type codes (typesafe enumeration).
53 * <b>double</b> DOUBLE
54 * <b>boolean</b> BOOLEAN
58 public static class Code {
61 * The name of the type.
66 * Creates a new primitive type code with the given name.
68 * Note: this constructor is package-private. The only instances
69 * ever created are the ones for the standard primitive types.
72 * @param name the standard name of the primitive type
79 * Returns the standard name of the primitive type.
81 * @return the standard name of the primitive type
83 public String toString() {
88 /** Type code for the primitive type "int". */
89 public static final Code INT = new Code("int");//$NON-NLS-1$
90 /** Type code for the primitive type "char". */
91 public static final Code CHAR = new Code("char");//$NON-NLS-1$
92 /** Type code for the primitive type "boolean". */
93 public static final Code BOOLEAN = new Code("boolean");//$NON-NLS-1$
94 /** Type code for the primitive type "short". */
95 public static final Code SHORT = new Code("short");//$NON-NLS-1$
96 /** Type code for the primitive type "long". */
97 public static final Code LONG = new Code("long");//$NON-NLS-1$
98 /** Type code for the primitive type "float". */
99 public static final Code FLOAT = new Code("float");//$NON-NLS-1$
100 /** Type code for the primitive type "double". */
101 public static final Code DOUBLE = new Code("double");//$NON-NLS-1$
102 /** Type code for the primitive type "byte". */
103 public static final Code BYTE = new Code("byte");//$NON-NLS-1$
105 /** Type code for the primitive type "void". Note that "void" is
106 * special in that its only legitimate uses are as a method return
107 * type and as a type literal.
109 public static final Code VOID = new Code("void");//$NON-NLS-1$
112 * The primitive type code; one of the PrimitiveType constants; default
115 private PrimitiveType.Code typeCode = INT;
118 * Map from token to primitive type code (key type: <code>String</code>;
119 * value type: <code>PrimitiveType.Code</code>).
121 private static final Map CODES;
123 CODES = new HashMap(20);
135 for (int i = 0; i < ops.length; i++) {
136 CODES.put(ops[i].toString(), ops[i]);
141 * Returns the primitive type code corresponding to the given string,
142 * or <code>null</code> if none.
144 * <code>toCode</code> is the converse of <code>toString</code>:
146 * <code>PrimitiveType.Code.toCode(code.toString()) == code</code>
147 * for all type code <code>code</code>.
150 * @param token the standard name of the primitive type
151 * @return the primitive type code, or <code>null</code> if none
153 public static PrimitiveType.Code toCode(String token) {
154 return (PrimitiveType.Code) CODES.get(token);
158 * The "primitiveTypeCode" structural property of this node type.
161 public static final SimplePropertyDescriptor PRIMITIVE_TYPE_CODE_PROPERTY =
162 new SimplePropertyDescriptor(PrimitiveType.class, "primitiveTypeCode", PrimitiveType.Code.class, MANDATORY); //$NON-NLS-1$
165 * A list of property descriptors (element type:
166 * {@link StructuralPropertyDescriptor}),
167 * or null if uninitialized.
169 private static final List PROPERTY_DESCRIPTORS;
172 List propertyList = new ArrayList(2);
173 createPropertyList(PrimitiveType.class, propertyList);
174 addProperty(PRIMITIVE_TYPE_CODE_PROPERTY, propertyList);
175 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
179 * Returns a list of structural property descriptors for this node type.
180 * Clients must not modify the result.
182 * @param apiLevel the API level; one of the
183 * <code>AST.JLS*</code> constants
185 * @return a list of property descriptors (element type:
186 * {@link StructuralPropertyDescriptor})
189 public static List propertyDescriptors(int apiLevel) {
190 return PROPERTY_DESCRIPTORS;
194 * Creates a new unparented node for a primitive type owned by the given
195 * AST. By default, the node has type "int".
197 * N.B. This constructor is package-private.
200 * @param ast the AST that is to own this node
202 PrimitiveType(AST ast) {
206 /* (omit javadoc for this method)
207 * Method declared on ASTNode.
209 final List internalStructuralPropertiesForType(int apiLevel) {
210 return propertyDescriptors(apiLevel);
213 /* (omit javadoc for this method)
214 * Method declared on ASTNode.
216 final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) {
217 if (property == PRIMITIVE_TYPE_CODE_PROPERTY) {
219 return getPrimitiveTypeCode();
221 setPrimitiveTypeCode((Code) value);
225 // allow default implementation to flag the error
226 return super.internalGetSetObjectProperty(property, get, value);
229 /* (omit javadoc for this method)
230 * Method declared on ASTNode.
232 final int getNodeType0() {
233 return PRIMITIVE_TYPE;
236 /* (omit javadoc for this method)
237 * Method declared on ASTNode.
239 ASTNode clone0(AST target) {
240 PrimitiveType result = new PrimitiveType(target);
241 result.setSourceRange(this.getStartPosition(), this.getLength());
242 result.setPrimitiveTypeCode(getPrimitiveTypeCode());
246 /* (omit javadoc for this method)
247 * Method declared on ASTNode.
249 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
250 // dispatch to correct overloaded match method
251 return matcher.match(this, other);
254 /* (omit javadoc for this method)
255 * Method declared on ASTNode.
257 void accept0(ASTVisitor visitor) {
259 visitor.endVisit(this);
263 * Returns the primitive type code.
265 * @return one of the primitive type code constants declared in this
268 public PrimitiveType.Code getPrimitiveTypeCode() {
269 return this.typeCode;
273 * Sets the primitive type code.
275 * @param typeCode one of the primitive type code constants declared in
277 * @exception IllegalArgumentException if the argument is incorrect
279 public void setPrimitiveTypeCode(PrimitiveType.Code typeCode) {
280 if (typeCode == null) {
281 throw new IllegalArgumentException();
283 preValueChange(PRIMITIVE_TYPE_CODE_PROPERTY);
284 this.typeCode = typeCode;
285 postValueChange(PRIMITIVE_TYPE_CODE_PROPERTY);
288 /* (omit javadoc for this method)
289 * Method declared on ASTNode.
292 // treat Code as free
293 return BASE_NODE_SIZE + 1 * 4;
296 /* (omit javadoc for this method)
297 * Method declared on ASTNode.