KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Instanceof expression AST node type.
19  * <pre>
20  * InstanceofExpression:
21  * Expression <b>instanceof</b> Type
22  * </pre>
23  *
24  * @since 2.0
25  */

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

32     public static final ChildPropertyDescriptor LEFT_OPERAND_PROPERTY =
33         new ChildPropertyDescriptor(InstanceofExpression.class, "leftOperand", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
34

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

39     public static final ChildPropertyDescriptor RIGHT_OPERAND_PROPERTY =
40         new ChildPropertyDescriptor(InstanceofExpression.class, "rightOperand", Type.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
41

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

47     private static final List JavaDoc PROPERTY_DESCRIPTORS;
48     
49     static {
50         List JavaDoc properyList = new ArrayList JavaDoc(3);
51         createPropertyList(InstanceofExpression.class, properyList);
52         addProperty(LEFT_OPERAND_PROPERTY, properyList);
53         addProperty(RIGHT_OPERAND_PROPERTY, properyList);
54         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
55     }
56
57     /**
58      * Returns a list of structural property descriptors for this node type.
59      * Clients must not modify the result.
60      *
61      * @param apiLevel the API level; one of the
62      * <code>AST.JLS*</code> constants
63
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 left operand; lazily initialized; defaults to an unspecified,
74      * but legal, simple name.
75      */

76     private Expression leftOperand = null;
77
78     /**
79      * The right operand; lazily initialized; defaults to an unspecified,
80      * but legal, simple type.
81      */

82     private Type rightOperand = null;
83
84     /**
85      * Creates a new AST node for an instanceof expression owned by the given
86      * AST. By default, the node has unspecified (but legal) operator,
87      * left and right operands.
88      *
89      * @param ast the AST that is to own this node
90      */

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

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

105     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
106         if (property == LEFT_OPERAND_PROPERTY) {
107             if (get) {
108                 return getLeftOperand();
109             } else {
110                 setLeftOperand((Expression) child);
111                 return null;
112             }
113         }
114         if (property == RIGHT_OPERAND_PROPERTY) {
115             if (get) {
116                 return getRightOperand();
117             } else {
118                 setRightOperand((Type) child);
119                 return null;
120             }
121         }
122         // allow default implementation to flag the error
123
return super.internalGetSetChildProperty(property, get, child);
124     }
125     
126     /* (omit javadoc for this method)
127      * Method declared on ASTNode.
128      */

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

136     ASTNode clone0(AST target) {
137         InstanceofExpression result = new InstanceofExpression(target);
138         result.setSourceRange(this.getStartPosition(), this.getLength());
139         result.setLeftOperand((Expression) getLeftOperand().clone(target));
140         result.setRightOperand((Type) getRightOperand().clone(target));
141         return result;
142     }
143
144     /* (omit javadoc for this method)
145      * Method declared on ASTNode.
146      */

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

155     void accept0(ASTVisitor visitor) {
156         boolean visitChildren = visitor.visit(this);
157         if (visitChildren) {
158             // visit children in normal left to right reading order
159
acceptChild(visitor, getLeftOperand());
160             acceptChild(visitor, getRightOperand());
161         }
162         visitor.endVisit(this);
163     }
164     
165     /**
166      * Returns the left operand of this instanceof expression.
167      *
168      * @return the left operand node
169      */

170     public Expression getLeftOperand() {
171         if (this.leftOperand == null) {
172             // lazy init must be thread-safe for readers
173
synchronized (this) {
174                 if (this.leftOperand == null) {
175                     preLazyInit();
176                     this.leftOperand= new SimpleName(this.ast);
177                     postLazyInit(this.leftOperand, LEFT_OPERAND_PROPERTY);
178                 }
179             }
180         }
181         return this.leftOperand;
182     }
183         
184     /**
185      * Sets the left operand of this instanceof expression.
186      *
187      * @param expression the left operand node
188      * @exception IllegalArgumentException if:
189      * <ul>
190      * <li>the node belongs to a different AST</li>
191      * <li>the node already has a parent</li>
192      * <li>a cycle in would be created</li>
193      * </ul>
194      */

195     public void setLeftOperand(Expression expression) {
196         if (expression == null) {
197             throw new IllegalArgumentException JavaDoc();
198         }
199         ASTNode oldChild = this.leftOperand;
200         preReplaceChild(oldChild, expression, LEFT_OPERAND_PROPERTY);
201         this.leftOperand = expression;
202         postReplaceChild(oldChild, expression, LEFT_OPERAND_PROPERTY);
203     }
204
205     /**
206      * Returns the right operand of this instanceof expression.
207      *
208      * @return the right operand node
209      */

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

235     public void setRightOperand(Type referenceType) {
236         if (referenceType == null) {
237             throw new IllegalArgumentException JavaDoc();
238         }
239         ASTNode oldChild = this.rightOperand;
240         preReplaceChild(oldChild, referenceType, RIGHT_OPERAND_PROPERTY);
241         this.rightOperand = referenceType;
242         postReplaceChild(oldChild, referenceType, RIGHT_OPERAND_PROPERTY);
243     }
244     
245     /* (omit javadoc for this method)
246      * Method declared on ASTNode.
247      */

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

256     int treeSize() {
257         return
258             memSize()
259             + (this.leftOperand == null ? 0 : getLeftOperand().treeSize())
260             + (this.rightOperand == null ? 0 : getRightOperand().treeSize());
261     }
262 }
263
Popular Tags