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 * Cast expression AST node type.
22 * <b>(</b> Type <b>)</b> Expression
26 * @noinstantiate This class is not intended to be instantiated by clients.
28 public class CastExpression extends Expression {
31 * The "type" structural property of this node type.
34 public static final ChildPropertyDescriptor TYPE_PROPERTY =
35 new ChildPropertyDescriptor(CastExpression.class, "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
38 * The "expression" structural property of this node type.
41 public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
42 new ChildPropertyDescriptor(CastExpression.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
45 * A list of property descriptors (element type:
46 * {@link StructuralPropertyDescriptor}),
47 * or null if uninitialized.
49 private static final List PROPERTY_DESCRIPTORS;
52 List properyList = new ArrayList(3);
53 createPropertyList(CastExpression.class, properyList);
54 addProperty(TYPE_PROPERTY, properyList);
55 addProperty(EXPRESSION_PROPERTY, properyList);
56 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
60 * Returns a list of structural property descriptors for this node type.
61 * Clients must not modify the result.
63 * @param apiLevel the API level; one of the
64 * <code>AST.JLS*</code> constants
65 * @return a list of property descriptors (element type:
66 * {@link StructuralPropertyDescriptor})
69 public static List propertyDescriptors(int apiLevel) {
70 return PROPERTY_DESCRIPTORS;
74 * The type; lazily initialized; defaults to a unspecified,
77 private Type type = null;
80 * The expression; lazily initialized; defaults to a unspecified, but legal,
83 private Expression expression = null;
86 * Creates a new AST node for a cast expression owned by the given
87 * AST. By default, the type and expression are unspecified (but legal).
89 * N.B. This constructor is package-private.
92 * @param ast the AST that is to own this node
94 CastExpression(AST ast) {
98 /* (omit javadoc for this method)
99 * Method declared on ASTNode.
101 final List internalStructuralPropertiesForType(int apiLevel) {
102 return propertyDescriptors(apiLevel);
105 /* (omit javadoc for this method)
106 * Method declared on ASTNode.
108 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
109 if (property == EXPRESSION_PROPERTY) {
111 return getExpression();
113 setExpression((Expression) child);
117 if (property == TYPE_PROPERTY) {
121 setType((Type) child);
125 // allow default implementation to flag the error
126 return super.internalGetSetChildProperty(property, get, child);
129 /* (omit javadoc for this method)
130 * Method declared on ASTNode.
132 final int getNodeType0() {
133 return CAST_EXPRESSION;
136 /* (omit javadoc for this method)
137 * Method declared on ASTNode.
139 ASTNode clone0(AST target) {
140 CastExpression result = new CastExpression(target);
141 result.setSourceRange(this.getStartPosition(), this.getLength());
142 result.setType((Type) getType().clone(target));
143 result.setExpression((Expression) getExpression().clone(target));
147 /* (omit javadoc for this method)
148 * Method declared on ASTNode.
150 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
151 // dispatch to correct overloaded match method
152 return matcher.match(this, other);
155 /* (omit javadoc for this method)
156 * Method declared on ASTNode.
158 void accept0(ASTVisitor visitor) {
159 boolean visitChildren = visitor.visit(this);
161 // visit children in normal left to right reading order
162 acceptChild(visitor, getType());
163 acceptChild(visitor, getExpression());
165 visitor.endVisit(this);
169 * Returns the type in this cast expression.
173 public Type getType() {
174 if (this.type == null) {
175 // lazy init must be thread-safe for readers
176 synchronized (this) {
177 if (this.type == null) {
179 this.type = this.ast.newPrimitiveType(PrimitiveType.INT);
180 postLazyInit(this.type, TYPE_PROPERTY);
188 * Sets the type in this cast expression to the given type.
190 * @param type the new type
191 * @exception IllegalArgumentException if:
193 * <li>the node belongs to a different AST</li>
194 * <li>the node already has a parent</li>
197 public void setType(Type type) {
199 throw new IllegalArgumentException();
201 ASTNode oldChild = this.type;
202 preReplaceChild(oldChild, type, TYPE_PROPERTY);
204 postReplaceChild(oldChild, type, TYPE_PROPERTY);
208 * Returns the expression of this cast expression.
210 * @return the expression node
212 public Expression getExpression() {
213 if (this.expression == null) {
214 // lazy init must be thread-safe for readers
215 synchronized (this) {
216 if (this.expression == null) {
218 this.expression = new SimpleName(this.ast);
219 postLazyInit(this.expression, EXPRESSION_PROPERTY);
223 return this.expression;
227 * Sets the expression of this cast expression.
229 * @param expression the new expression node
230 * @exception IllegalArgumentException if:
232 * <li>the node belongs to a different AST</li>
233 * <li>the node already has a parent</li>
234 * <li>a cycle in would be created</li>
237 public void setExpression(Expression expression) {
238 if (expression == null) {
239 throw new IllegalArgumentException();
241 ASTNode oldChild = this.expression;
242 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
243 this.expression = expression;
244 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
247 /* (omit javadoc for this method)
248 * Method declared on ASTNode.
251 // treat Code as free
252 return BASE_NODE_SIZE + 2 * 4;
255 /* (omit javadoc for this method)
256 * Method declared on ASTNode.
261 + (this.expression == null ? 0 : getExpression().treeSize())
262 + (this.type == null ? 0 : getType().treeSize());