KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > PrimitiveType


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.core.dom;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 /**
20  * Primitive type nodes.
21  * <pre>
22  * PrimitiveType:
23  * <b>byte</b>
24  * <b>short</b>
25  * <b>char</b>
26  * <b>int</b>
27  * <b>long</b>
28  * <b>float</b>
29  * <b>double</b>
30  * <b>boolean</b>
31  * <b>void</b>
32  * </pre>
33  * <p>
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
36  * nodes.
37  * </p>
38  *
39  * @since 2.0
40  */

41 public class PrimitiveType extends Type {
42     
43     /**
44      * Primitive type codes (typesafe enumeration).
45      * <pre>
46      * <b>byte</b> BYTE
47      * <b>short</b> SHORT
48      * <b>char</b> CHAR
49      * <b>int</b> INT
50      * <b>long</b> LONG
51      * <b>float</b> FLOAT
52      * <b>double</b> DOUBLE
53      * <b>boolean</b> BOOLEAN
54      * <b>void</b> VOID
55      * </pre>
56      */

57     public static class Code {
58     
59         /**
60          * The name of the type.
61          */

62         private String JavaDoc name;
63         
64         /**
65          * Creates a new primitive type code with the given name.
66          * <p>
67          * Note: this constructor is package-private. The only instances
68          * ever created are the ones for the standard primitive types.
69          * </p>
70          *
71          * @param name the standard name of the primitive type
72          */

73         Code(String JavaDoc name) {
74             this.name = name;
75         }
76         
77         /**
78          * Returns the standard name of the primitive type.
79          *
80          * @return the standard name of the primitive type
81          */

82         public String JavaDoc toString() {
83             return name;
84         }
85     }
86     
87     /** Type code for the primitive type "int". */
88     public static final Code INT = new Code("int");//$NON-NLS-1$
89
/** Type code for the primitive type "char". */
90     public static final Code CHAR = new Code("char");//$NON-NLS-1$
91
/** Type code for the primitive type "boolean". */
92     public static final Code BOOLEAN = new Code("boolean");//$NON-NLS-1$
93
/** Type code for the primitive type "short". */
94     public static final Code SHORT = new Code("short");//$NON-NLS-1$
95
/** Type code for the primitive type "long". */
96     public static final Code LONG = new Code("long");//$NON-NLS-1$
97
/** Type code for the primitive type "float". */
98     public static final Code FLOAT = new Code("float");//$NON-NLS-1$
99
/** Type code for the primitive type "double". */
100     public static final Code DOUBLE = new Code("double");//$NON-NLS-1$
101
/** Type code for the primitive type "byte". */
102     public static final Code BYTE = new Code("byte");//$NON-NLS-1$
103

104     /** Type code for the primitive type "void". Note that "void" is
105      * special in that its only legitimate uses are as a method return
106      * type and as a type literal.
107      */

108     public static final Code VOID = new Code("void");//$NON-NLS-1$
109

110     /**
111      * The primitive type code; one of the PrimitiveType constants; default
112      * is int.
113      */

114     private PrimitiveType.Code typeCode = INT;
115     
116     /**
117      * Map from token to primitive type code (key type: <code>String</code>;
118      * value type: <code>PrimitiveType.Code</code>).
119      */

120     private static final Map JavaDoc CODES;
121     static {
122         CODES = new HashMap JavaDoc(20);
123         Code[] ops = {
124                 INT,
125                 BYTE,
126                 CHAR,
127                 BOOLEAN,
128                 SHORT,
129                 LONG,
130                 FLOAT,
131                 DOUBLE,
132                 VOID,
133             };
134         for (int i = 0; i < ops.length; i++) {
135             CODES.put(ops[i].toString(), ops[i]);
136         }
137     }
138     
139     /**
140      * Returns the primitive type code corresponding to the given string,
141      * or <code>null</code> if none.
142      * <p>
143      * <code>toCode</code> is the converse of <code>toString</code>:
144      * that is,
145      * <code>PrimitiveType.Code.toCode(code.toString()) == code</code>
146      * for all type code <code>code</code>.
147      * </p>
148      *
149      * @param token the standard name of the primitive type
150      * @return the primitive type code, or <code>null</code> if none
151      */

152     public static PrimitiveType.Code toCode(String JavaDoc token) {
153         return (PrimitiveType.Code) CODES.get(token);
154     }
155     
156     /**
157      * The "primitiveTypeCode" structural property of this node type.
158      * @since 3.0
159      */

160     public static final SimplePropertyDescriptor PRIMITIVE_TYPE_CODE_PROPERTY =
161         new SimplePropertyDescriptor(PrimitiveType.class, "primitiveTypeCode", PrimitiveType.Code.class, MANDATORY); //$NON-NLS-1$
162

163     /**
164      * A list of property descriptors (element type:
165      * {@link StructuralPropertyDescriptor}),
166      * or null if uninitialized.
167      */

168     private static final List JavaDoc PROPERTY_DESCRIPTORS;
169     
170     static {
171         List JavaDoc propertyList = new ArrayList JavaDoc(2);
172         createPropertyList(PrimitiveType.class, propertyList);
173         addProperty(PRIMITIVE_TYPE_CODE_PROPERTY, propertyList);
174         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
175     }
176
177     /**
178      * Returns a list of structural property descriptors for this node type.
179      * Clients must not modify the result.
180      *
181      * @param apiLevel the API level; one of the
182      * <code>AST.JLS&ast;</code> constants
183
184      * @return a list of property descriptors (element type:
185      * {@link StructuralPropertyDescriptor})
186      * @since 3.0
187      */

188     public static List JavaDoc propertyDescriptors(int apiLevel) {
189         return PROPERTY_DESCRIPTORS;
190     }
191             
192     /**
193      * Creates a new unparented node for a primitive type owned by the given
194      * AST. By default, the node has type "int".
195      * <p>
196      * N.B. This constructor is package-private.
197      * </p>
198      *
199      * @param ast the AST that is to own this node
200      */

201     PrimitiveType(AST ast) {
202         super(ast);
203     }
204     
205     /* (omit javadoc for this method)
206      * Method declared on ASTNode.
207      */

208     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
209         return propertyDescriptors(apiLevel);
210     }
211     
212     /* (omit javadoc for this method)
213      * Method declared on ASTNode.
214      */

215     final Object JavaDoc internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object JavaDoc value) {
216         if (property == PRIMITIVE_TYPE_CODE_PROPERTY) {
217             if (get) {
218                 return getPrimitiveTypeCode();
219             } else {
220                 setPrimitiveTypeCode((Code) value);
221                 return null;
222             }
223         }
224         // allow default implementation to flag the error
225
return super.internalGetSetObjectProperty(property, get, value);
226     }
227
228     /* (omit javadoc for this method)
229      * Method declared on ASTNode.
230      */

231     final int getNodeType0() {
232         return PRIMITIVE_TYPE;
233     }
234
235     /* (omit javadoc for this method)
236      * Method declared on ASTNode.
237      */

238     ASTNode clone0(AST target) {
239         PrimitiveType result = new PrimitiveType(target);
240         result.setSourceRange(this.getStartPosition(), this.getLength());
241         result.setPrimitiveTypeCode(getPrimitiveTypeCode());
242         return result;
243     }
244
245     /* (omit javadoc for this method)
246      * Method declared on ASTNode.
247      */

248     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
249         // dispatch to correct overloaded match method
250
return matcher.match(this, other);
251     }
252
253     /* (omit javadoc for this method)
254      * Method declared on ASTNode.
255      */

256     void accept0(ASTVisitor visitor) {
257         visitor.visit(this);
258         visitor.endVisit(this);
259     }
260     
261     /**
262      * Returns the primitive type code.
263      *
264      * @return one of the primitive type code constants declared in this
265      * class
266      */

267     public PrimitiveType.Code getPrimitiveTypeCode() {
268         return this.typeCode;
269     }
270     
271     /**
272      * Sets the primitive type code.
273      *
274      * @param typeCode one of the primitive type code constants declared in
275      * this class
276      * @exception IllegalArgumentException if the argument is incorrect
277      */

278     public void setPrimitiveTypeCode(PrimitiveType.Code typeCode) {
279         if (typeCode == null) {
280             throw new IllegalArgumentException JavaDoc();
281         }
282         preValueChange(PRIMITIVE_TYPE_CODE_PROPERTY);
283         this.typeCode = typeCode;
284         postValueChange(PRIMITIVE_TYPE_CODE_PROPERTY);
285     }
286
287     /* (omit javadoc for this method)
288      * Method declared on ASTNode.
289      */

290     int memSize() {
291         // treat Code as free
292
return BASE_NODE_SIZE + 1 * 4;
293     }
294     
295     /* (omit javadoc for this method)
296      * Method declared on ASTNode.
297      */

298     int treeSize() {
299         return memSize();
300     }
301 }
302
Popular Tags