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 * Instanceof expression AST node type.
20 * InstanceofExpression:
21 * Expression <b>instanceof</b> Type
25 * @noinstantiate This class is not intended to be instantiated by clients.
27 public class InstanceofExpression extends Expression {
30 * The "leftOperand" structural property of this node type.
33 public static final ChildPropertyDescriptor LEFT_OPERAND_PROPERTY =
34 new ChildPropertyDescriptor(InstanceofExpression.class, "leftOperand", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
37 * The "rightOperand" structural property of this node type.
40 public static final ChildPropertyDescriptor RIGHT_OPERAND_PROPERTY =
41 new ChildPropertyDescriptor(InstanceofExpression.class, "rightOperand", Type.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
44 * A list of property descriptors (element type:
45 * {@link StructuralPropertyDescriptor}),
46 * or null if uninitialized.
48 private static final List PROPERTY_DESCRIPTORS;
51 List properyList = new ArrayList(3);
52 createPropertyList(InstanceofExpression.class, properyList);
53 addProperty(LEFT_OPERAND_PROPERTY, properyList);
54 addProperty(RIGHT_OPERAND_PROPERTY, properyList);
55 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
59 * Returns a list of structural property descriptors for this node type.
60 * Clients must not modify the result.
62 * @param apiLevel the API level; one of the
63 * <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 left operand; lazily initialized; defaults to an unspecified,
75 * but legal, simple name.
77 private Expression leftOperand = null;
80 * The right operand; lazily initialized; defaults to an unspecified,
81 * but legal, simple type.
83 private Type rightOperand = null;
86 * Creates a new AST node for an instanceof expression owned by the given
87 * AST. By default, the node has unspecified (but legal) operator,
88 * left and right operands.
90 * @param ast the AST that is to own this node
92 InstanceofExpression(AST ast) {
96 /* (omit javadoc for this method)
97 * Method declared on ASTNode.
99 final List internalStructuralPropertiesForType(int apiLevel) {
100 return propertyDescriptors(apiLevel);
103 /* (omit javadoc for this method)
104 * Method declared on ASTNode.
106 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
107 if (property == LEFT_OPERAND_PROPERTY) {
109 return getLeftOperand();
111 setLeftOperand((Expression) child);
115 if (property == RIGHT_OPERAND_PROPERTY) {
117 return getRightOperand();
119 setRightOperand((Type) child);
123 // allow default implementation to flag the error
124 return super.internalGetSetChildProperty(property, get, child);
127 /* (omit javadoc for this method)
128 * Method declared on ASTNode.
130 final int getNodeType0() {
131 return INSTANCEOF_EXPRESSION;
134 /* (omit javadoc for this method)
135 * Method declared on ASTNode.
137 ASTNode clone0(AST target) {
138 InstanceofExpression result = new InstanceofExpression(target);
139 result.setSourceRange(this.getStartPosition(), this.getLength());
140 result.setLeftOperand((Expression) getLeftOperand().clone(target));
141 result.setRightOperand((Type) getRightOperand().clone(target));
145 /* (omit javadoc for this method)
146 * Method declared on ASTNode.
148 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
149 // dispatch to correct overloaded match method
150 return matcher.match(this, other);
153 /* (omit javadoc for this method)
154 * Method declared on ASTNode.
156 void accept0(ASTVisitor visitor) {
157 boolean visitChildren = visitor.visit(this);
159 // visit children in normal left to right reading order
160 acceptChild(visitor, getLeftOperand());
161 acceptChild(visitor, getRightOperand());
163 visitor.endVisit(this);
167 * Returns the left operand of this instanceof expression.
169 * @return the left operand node
171 public Expression getLeftOperand() {
172 if (this.leftOperand == null) {
173 // lazy init must be thread-safe for readers
174 synchronized (this) {
175 if (this.leftOperand == null) {
177 this.leftOperand= new SimpleName(this.ast);
178 postLazyInit(this.leftOperand, LEFT_OPERAND_PROPERTY);
182 return this.leftOperand;
186 * Sets the left operand of this instanceof expression.
188 * @param expression the left operand node
189 * @exception IllegalArgumentException if:
191 * <li>the node belongs to a different AST</li>
192 * <li>the node already has a parent</li>
193 * <li>a cycle in would be created</li>
196 public void setLeftOperand(Expression expression) {
197 if (expression == null) {
198 throw new IllegalArgumentException();
200 ASTNode oldChild = this.leftOperand;
201 preReplaceChild(oldChild, expression, LEFT_OPERAND_PROPERTY);
202 this.leftOperand = expression;
203 postReplaceChild(oldChild, expression, LEFT_OPERAND_PROPERTY);
207 * Returns the right operand of this instanceof expression.
209 * @return the right operand node
211 public Type getRightOperand() {
212 if (this.rightOperand == null) {
213 // lazy init must be thread-safe for readers
214 synchronized (this) {
215 if (this.rightOperand == null) {
217 this.rightOperand= new SimpleType(this.ast);
218 postLazyInit(this.rightOperand, RIGHT_OPERAND_PROPERTY);
222 return this.rightOperand;
226 * Sets the right operand of this instanceof expression.
228 * @param referenceType the right operand node
229 * @exception IllegalArgumentException if:
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>
236 public void setRightOperand(Type referenceType) {
237 if (referenceType == null) {
238 throw new IllegalArgumentException();
240 ASTNode oldChild = this.rightOperand;
241 preReplaceChild(oldChild, referenceType, RIGHT_OPERAND_PROPERTY);
242 this.rightOperand = referenceType;
243 postReplaceChild(oldChild, referenceType, RIGHT_OPERAND_PROPERTY);
246 /* (omit javadoc for this method)
247 * Method declared on ASTNode.
250 // treat Operator as free
251 return BASE_NODE_SIZE + 2 * 4;
254 /* (omit javadoc for this method)
255 * Method declared on ASTNode.
260 + (this.leftOperand == null ? 0 : getLeftOperand().treeSize())
261 + (this.rightOperand == null ? 0 : getRightOperand().treeSize());