KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Postfix expression AST node type.
21  *
22  * <pre>
23  * PostfixExpression:
24  * Expression PostfixOperator
25  * </pre>
26  *
27  * @since 2.0
28  */

29 public class PostfixExpression extends Expression {
30
31     /**
32      * Postfix operators (typesafe enumeration).
33      * <pre>
34      * PostfixOperator:
35      * <b><code>++</code></b> <code>INCREMENT</code>
36      * <b><code>--</code></b> <code>DECREMENT</code>
37      * </pre>
38      */

39     public static class Operator {
40     
41         /**
42          * The token for the operator.
43          */

44         private String JavaDoc token;
45         
46         /**
47          * Creates a new postfix operator with the given token.
48          * <p>
49          * Note: this constructor is private. The only instances
50          * ever created are the ones for the standard operators.
51          * </p>
52          *
53          * @param token the character sequence for the operator
54          */

55         private Operator(String JavaDoc token) {
56             this.token = token;
57         }
58         
59         /**
60          * Returns the character sequence for the operator.
61          *
62          * @return the character sequence for the operator
63          */

64         public String JavaDoc toString() {
65             return token;
66         }
67         
68         /** Postfix increment "++" operator. */
69         public static final Operator INCREMENT = new Operator("++");//$NON-NLS-1$
70
/** Postfix decrement "--" operator. */
71         public static final Operator DECREMENT = new Operator("--");//$NON-NLS-1$
72

73         /**
74          * Map from token to operator (key type: <code>String</code>;
75          * value type: <code>Operator</code>).
76          */

77         private static final Map JavaDoc CODES;
78         static {
79             CODES = new HashMap JavaDoc(20);
80             Operator[] ops = {
81                     INCREMENT,
82                     DECREMENT,
83                 };
84             for (int i = 0; i < ops.length; i++) {
85                 CODES.put(ops[i].toString(), ops[i]);
86             }
87         }
88
89         /**
90          * Returns the postfix operator corresponding to the given string,
91          * or <code>null</code> if none.
92          * <p>
93          * <code>toOperator</code> is the converse of <code>toString</code>:
94          * that is, <code>Operator.toOperator(op.toString()) == op</code> for
95          * all operators <code>op</code>.
96          * </p>
97          *
98          * @param token the character sequence for the operator
99          * @return the postfix operator, or <code>null</code> if none
100          */

101         public static Operator toOperator(String JavaDoc token) {
102             return (Operator) CODES.get(token);
103         }
104     }
105     
106     /**
107      * The "operator" structural property of this node type.
108      * @since 3.0
109      */

110     public static final SimplePropertyDescriptor OPERATOR_PROPERTY =
111         new SimplePropertyDescriptor(PostfixExpression.class, "operator", PostfixExpression.Operator.class, MANDATORY); //$NON-NLS-1$
112

113     /**
114      * The "operand" structural property of this node type.
115      * @since 3.0
116      */

117     public static final ChildPropertyDescriptor OPERAND_PROPERTY =
118         new ChildPropertyDescriptor(PostfixExpression.class, "operand", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
119

120     /**
121      * A list of property descriptors (element type:
122      * {@link StructuralPropertyDescriptor}),
123      * or null if uninitialized.
124      */

125     private static final List JavaDoc PROPERTY_DESCRIPTORS;
126     
127     static {
128         List JavaDoc propertyList = new ArrayList JavaDoc(3);
129         createPropertyList(PostfixExpression.class, propertyList);
130         addProperty(OPERAND_PROPERTY, propertyList);
131         addProperty(OPERATOR_PROPERTY, propertyList);
132         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
133     }
134
135     /**
136      * Returns a list of structural property descriptors for this node type.
137      * Clients must not modify the result.
138      *
139      * @param apiLevel the API level; one of the
140      * <code>AST.JLS&ast;</code> constants
141
142      * @return a list of property descriptors (element type:
143      * {@link StructuralPropertyDescriptor})
144      * @since 3.0
145      */

146     public static List JavaDoc propertyDescriptors(int apiLevel) {
147         return PROPERTY_DESCRIPTORS;
148     }
149             
150     /**
151      * The operator; defaults to an unspecified postfix operator.
152      */

153     private PostfixExpression.Operator operator =
154         PostfixExpression.Operator.INCREMENT;
155
156     /**
157      * The operand; lazily initialized; defaults to an unspecified,
158      * but legal, simple name.
159      */

160     private Expression operand = null;
161
162     /**
163      * Creates a new AST node for an postfix expression owned by the given
164      * AST. By default, the node has unspecified (but legal) operator and
165      * operand.
166      *
167      * @param ast the AST that is to own this node
168      */

169     PostfixExpression(AST ast) {
170         super(ast);
171     }
172
173     /* (omit javadoc for this method)
174      * Method declared on ASTNode.
175      */

176     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
177         return propertyDescriptors(apiLevel);
178     }
179     
180     /* (omit javadoc for this method)
181      * Method declared on ASTNode.
182      */

183     final Object JavaDoc internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object JavaDoc value) {
184         if (property == OPERATOR_PROPERTY) {
185             if (get) {
186                 return getOperator();
187             } else {
188                 setOperator((Operator) value);
189                 return null;
190             }
191         }
192         // allow default implementation to flag the error
193
return super.internalGetSetObjectProperty(property, get, value);
194     }
195
196     /* (omit javadoc for this method)
197      * Method declared on ASTNode.
198      */

199     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
200         if (property == OPERAND_PROPERTY) {
201             if (get) {
202                 return getOperand();
203             } else {
204                 setOperand((Expression) child);
205                 return null;
206             }
207         }
208         // allow default implementation to flag the error
209
return super.internalGetSetChildProperty(property, get, child);
210     }
211     
212     /* (omit javadoc for this method)
213      * Method declared on ASTNode.
214      */

215     final int getNodeType0() {
216         return POSTFIX_EXPRESSION;
217     }
218
219     /* (omit javadoc for this method)
220      * Method declared on ASTNode.
221      */

222     ASTNode clone0(AST target) {
223         PostfixExpression result = new PostfixExpression(target);
224         result.setSourceRange(this.getStartPosition(), this.getLength());
225         result.setOperator(getOperator());
226         result.setOperand((Expression) getOperand().clone(target));
227         return result;
228     }
229
230     /* (omit javadoc for this method)
231      * Method declared on ASTNode.
232      */

233     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
234         // dispatch to correct overloaded match method
235
return matcher.match(this, other);
236     }
237
238     /* (omit javadoc for this method)
239      * Method declared on ASTNode.
240      */

241     void accept0(ASTVisitor visitor) {
242         boolean visitChildren = visitor.visit(this);
243         if (visitChildren) {
244             acceptChild(visitor, getOperand());
245         }
246         visitor.endVisit(this);
247     }
248     
249     /**
250      * Returns the operator of this postfix expression.
251      *
252      * @return the operator
253      */

254     public PostfixExpression.Operator getOperator() {
255         return this.operator;
256     }
257
258     /**
259      * Sets the operator of this postfix expression.
260      *
261      * @param operator the operator
262      * @exception IllegalArgumentException if the argument is incorrect
263      */

264     public void setOperator(PostfixExpression.Operator operator) {
265         if (operator == null) {
266             throw new IllegalArgumentException JavaDoc();
267         }
268         preValueChange(OPERATOR_PROPERTY);
269         this.operator = operator;
270         postValueChange(OPERATOR_PROPERTY);
271     }
272
273     /**
274      * Returns the operand of this postfix expression.
275      *
276      * @return the operand expression node
277      */

278     public Expression getOperand() {
279         if (this.operand == null) {
280             // lazy init must be thread-safe for readers
281
synchronized (this) {
282                 if (this.operand == null) {
283                     preLazyInit();
284                     this.operand= new SimpleName(this.ast);
285                     postLazyInit(this.operand, OPERAND_PROPERTY);
286                 }
287             }
288         }
289         return this.operand;
290     }
291         
292     /**
293      * Sets the operand of this postfix expression.
294      *
295      * @param expression the operand expression node
296      * @exception IllegalArgumentException if:
297      * <ul>
298      * <li>the node belongs to a different AST</li>
299      * <li>the node already has a parent</li>
300      * <li>a cycle in would be created</li>
301      * </ul>
302      */

303     public void setOperand(Expression expression) {
304         if (expression == null) {
305             throw new IllegalArgumentException JavaDoc();
306         }
307         ASTNode oldChild = this.operand;
308         preReplaceChild(oldChild, expression, OPERAND_PROPERTY);
309         this.operand = expression;
310         postReplaceChild(oldChild, expression, OPERAND_PROPERTY);
311     }
312
313     /* (omit javadoc for this method)
314      * Method declared on ASTNode.
315      */

316     int memSize() {
317         // treat Operator as free
318
return BASE_NODE_SIZE + 2 * 4;
319     }
320     
321     /* (omit javadoc for this method)
322      * Method declared on ASTNode.
323      */

324     int treeSize() {
325         return
326             memSize()
327             + (this.operand == null ? 0 : getOperand().treeSize());
328     }
329 }
330
Popular Tags