KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.List JavaDoc;
16
17 /**
18  * Cast expression AST node type.
19  *
20  * <pre>
21  * CastExpression:
22  * <b>(</b> Type <b>)</b> Expression
23  * </pre>
24  *
25  * @since 2.0
26  */

27 public class CastExpression extends Expression {
28
29     /**
30      * The "type" structural property of this node type.
31      * @since 3.0
32      */

33     public static final ChildPropertyDescriptor TYPE_PROPERTY =
34         new ChildPropertyDescriptor(CastExpression.class, "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
35

36     /**
37      * The "expression" structural property of this node type.
38      * @since 3.0
39      */

40     public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
41         new ChildPropertyDescriptor(CastExpression.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
42

43     /**
44      * A list of property descriptors (element type:
45      * {@link StructuralPropertyDescriptor}),
46      * or null if uninitialized.
47      */

48     private static final List JavaDoc PROPERTY_DESCRIPTORS;
49     
50     static {
51         List JavaDoc properyList = new ArrayList JavaDoc(3);
52         createPropertyList(CastExpression.class, properyList);
53         addProperty(TYPE_PROPERTY, properyList);
54         addProperty(EXPRESSION_PROPERTY, properyList);
55         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
56     }
57
58     /**
59      * Returns a list of structural property descriptors for this node type.
60      * Clients must not modify the result.
61      *
62      * @param apiLevel the API level; one of the
63      * <code>AST.JLS*</code> constants
64      * @return a list of property descriptors (element type:
65      * {@link StructuralPropertyDescriptor})
66      * @since 3.0
67      */

68     public static List JavaDoc propertyDescriptors(int apiLevel) {
69         return PROPERTY_DESCRIPTORS;
70     }
71             
72     /**
73      * The type; lazily initialized; defaults to a unspecified,
74      * legal type.
75      */

76     private Type type = null;
77
78     /**
79      * The expression; lazily initialized; defaults to a unspecified, but legal,
80      * expression.
81      */

82     private Expression expression = null;
83
84     /**
85      * Creates a new AST node for a cast expression owned by the given
86      * AST. By default, the type and expression are unspecified (but legal).
87      * <p>
88      * N.B. This constructor is package-private.
89      * </p>
90      *
91      * @param ast the AST that is to own this node
92      */

93     CastExpression(AST ast) {
94         super(ast);
95     }
96
97     /* (omit javadoc for this method)
98      * Method declared on ASTNode.
99      */

100     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
101         return propertyDescriptors(apiLevel);
102     }
103     
104     /* (omit javadoc for this method)
105      * Method declared on ASTNode.
106      */

107     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
108         if (property == EXPRESSION_PROPERTY) {
109             if (get) {
110                 return getExpression();
111             } else {
112                 setExpression((Expression) child);
113                 return null;
114             }
115         }
116         if (property == TYPE_PROPERTY) {
117             if (get) {
118                 return getType();
119             } else {
120                 setType((Type) child);
121                 return null;
122             }
123         }
124         // allow default implementation to flag the error
125
return super.internalGetSetChildProperty(property, get, child);
126     }
127
128     /* (omit javadoc for this method)
129      * Method declared on ASTNode.
130      */

131     final int getNodeType0() {
132         return CAST_EXPRESSION;
133     }
134
135     /* (omit javadoc for this method)
136      * Method declared on ASTNode.
137      */

138     ASTNode clone0(AST target) {
139         CastExpression result = new CastExpression(target);
140         result.setSourceRange(this.getStartPosition(), this.getLength());
141         result.setType((Type) getType().clone(target));
142         result.setExpression((Expression) getExpression().clone(target));
143         return result;
144     }
145
146     /* (omit javadoc for this method)
147      * Method declared on ASTNode.
148      */

149     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
150         // dispatch to correct overloaded match method
151
return matcher.match(this, other);
152     }
153
154     /* (omit javadoc for this method)
155      * Method declared on ASTNode.
156      */

157     void accept0(ASTVisitor visitor) {
158         boolean visitChildren = visitor.visit(this);
159         if (visitChildren) {
160             // visit children in normal left to right reading order
161
acceptChild(visitor, getType());
162             acceptChild(visitor, getExpression());
163         }
164         visitor.endVisit(this);
165     }
166     
167     /**
168      * Returns the type in this cast expression.
169      *
170      * @return the type
171      */

172     public Type getType() {
173         if (this.type == null) {
174             // lazy init must be thread-safe for readers
175
synchronized (this) {
176                 if (this.type == null) {
177                     preLazyInit();
178                     this.type = this.ast.newPrimitiveType(PrimitiveType.INT);
179                     postLazyInit(this.type, TYPE_PROPERTY);
180                 }
181             }
182         }
183         return this.type;
184     }
185
186     /**
187      * Sets the type in this cast expression to the given type.
188      *
189      * @param type the new type
190      * @exception IllegalArgumentException if:
191      * <ul>
192      * <li>the node belongs to a different AST</li>
193      * <li>the node already has a parent</li>
194      * </ul>
195      */

196     public void setType(Type type) {
197         if (type == null) {
198             throw new IllegalArgumentException JavaDoc();
199         }
200         ASTNode oldChild = this.type;
201         preReplaceChild(oldChild, type, TYPE_PROPERTY);
202         this.type = type;
203         postReplaceChild(oldChild, type, TYPE_PROPERTY);
204     }
205     
206     /**
207      * Returns the expression of this cast expression.
208      *
209      * @return the expression node
210      */

211     public Expression getExpression() {
212         if (this.expression == null) {
213             // lazy init must be thread-safe for readers
214
synchronized (this) {
215                 if (this.expression == null) {
216                     preLazyInit();
217                     this.expression = new SimpleName(this.ast);
218                     postLazyInit(this.expression, EXPRESSION_PROPERTY);
219                 }
220             }
221         }
222         return this.expression;
223     }
224         
225     /**
226      * Sets the expression of this cast expression.
227      *
228      * @param expression the new expression node
229      * @exception IllegalArgumentException if:
230      * <ul>
231      * <li>the node belongs to a different AST</li>
232      * <li>the node already has a parent</li>
233      * <li>a cycle in would be created</li>
234      * </ul>
235      */

236     public void setExpression(Expression expression) {
237         if (expression == null) {
238             throw new IllegalArgumentException JavaDoc();
239         }
240         ASTNode oldChild = this.expression;
241         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
242         this.expression = expression;
243         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
244     }
245
246     /* (omit javadoc for this method)
247      * Method declared on ASTNode.
248      */

249     int memSize() {
250         // treat Code as free
251
return BASE_NODE_SIZE + 2 * 4;
252     }
253     
254     /* (omit javadoc for this method)
255      * Method declared on ASTNode.
256      */

257     int treeSize() {
258         return
259             memSize()
260             + (this.expression == null ? 0 : getExpression().treeSize())
261             + (this.type == null ? 0 : getType().treeSize());
262     }
263 }
264
Popular Tags