KickJava   Java API By Example, From Geeks To Geeks.

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


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

29 public class PrefixExpression extends Expression {
30
31     /**
32      * Prefix operators (typesafe enumeration).
33      * <pre>
34      * PrefixOperator:
35      * <b><code>++</code></b> <code>INCREMENT</code>
36      * <b><code>--</code></b> <code>DECREMENT</code>
37      * <b><code>+</code></b> <code>PLUS</code>
38      * <b><code>-</code></b> <code>MINUS</code>
39      * <b><code>~</code></b> <code>COMPLEMENT</code>
40      * <b><code>!</code></b> <code>NOT</code>
41      * </pre>
42      */

43     public static class Operator {
44     
45         /**
46          * The token for the operator.
47          */

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

59         private Operator(String JavaDoc token) {
60             this.token = token;
61         }
62         
63         /**
64          * Returns the character sequence for the operator.
65          *
66          * @return the character sequence for the operator
67          */

68         public String JavaDoc toString() {
69             return token;
70         }
71         
72         /** Prefix increment "++" operator. */
73         public static final Operator INCREMENT = new Operator("++");//$NON-NLS-1$
74
/** Prefix decrement "--" operator. */
75         public static final Operator DECREMENT = new Operator("--");//$NON-NLS-1$
76
/** Unary plus "+" operator. */
77         public static final Operator PLUS = new Operator("+");//$NON-NLS-1$
78
/** Unary minus "-" operator. */
79         public static final Operator MINUS = new Operator("-");//$NON-NLS-1$
80
/** Bitwise complement "~" operator. */
81         public static final Operator COMPLEMENT = new Operator("~");//$NON-NLS-1$
82
/** Logical complement "!" operator. */
83         public static final Operator NOT = new Operator("!");//$NON-NLS-1$
84

85         /**
86          * Map from token to operator (key type: <code>String</code>;
87          * value type: <code>Operator</code>).
88          */

89         private static final Map JavaDoc CODES;
90         static {
91             CODES = new HashMap JavaDoc(20);
92             Operator[] ops = {
93                     INCREMENT,
94                     DECREMENT,
95                     PLUS,
96                     MINUS,
97                     COMPLEMENT,
98                     NOT,
99                 };
100             for (int i = 0; i < ops.length; i++) {
101                 CODES.put(ops[i].toString(), ops[i]);
102             }
103         }
104
105         /**
106          * Returns the prefix operator corresponding to the given string,
107          * or <code>null</code> if none.
108          * <p>
109          * <code>toOperator</code> is the converse of <code>toString</code>:
110          * that is, <code>Operator.toOperator(op.toString()) == op</code> for
111          * all operators <code>op</code>.
112          * </p>
113          *
114          * @param token the character sequence for the operator
115          * @return the prefix operator, or <code>null</code> if none
116          */

117         public static Operator toOperator(String JavaDoc token) {
118             return (Operator) CODES.get(token);
119         }
120     }
121     
122     /**
123      * The "operator" structural property of this node type.
124      * @since 3.0
125      */

126     public static final SimplePropertyDescriptor OPERATOR_PROPERTY =
127         new SimplePropertyDescriptor(PrefixExpression.class, "operator", PrefixExpression.Operator.class, MANDATORY); //$NON-NLS-1$
128

129     /**
130      * The "operand" structural property of this node type.
131      * @since 3.0
132      */

133     public static final ChildPropertyDescriptor OPERAND_PROPERTY =
134         new ChildPropertyDescriptor(PrefixExpression.class, "operand", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
135

136     /**
137      * A list of property descriptors (element type:
138      * {@link StructuralPropertyDescriptor}),
139      * or null if uninitialized.
140      */

141     private static final List JavaDoc PROPERTY_DESCRIPTORS;
142     
143     static {
144         List JavaDoc propertyList = new ArrayList JavaDoc(3);
145         createPropertyList(PrefixExpression.class, propertyList);
146         addProperty(OPERATOR_PROPERTY, propertyList);
147         addProperty(OPERAND_PROPERTY, propertyList);
148         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
149     }
150
151     /**
152      * Returns a list of structural property descriptors for this node type.
153      * Clients must not modify the result.
154      *
155      * @param apiLevel the API level; one of the
156      * <code>AST.JLS&ast;</code> constants
157
158      * @return a list of property descriptors (element type:
159      * {@link StructuralPropertyDescriptor})
160      * @since 3.0
161      */

162     public static List JavaDoc propertyDescriptors(int apiLevel) {
163         return PROPERTY_DESCRIPTORS;
164     }
165             
166     /**
167      * The operator; defaults to an unspecified prefix operator.
168      */

169     private PrefixExpression.Operator operator =
170         PrefixExpression.Operator.PLUS;
171
172     /**
173      * The operand; lazily initialized; defaults to an unspecified,
174      * but legal, simple name.
175      */

176     private Expression operand = null;
177
178     /**
179      * Creates a new AST node for an prefix expression owned by the given
180      * AST. By default, the node has unspecified (but legal) operator and
181      * operand.
182      *
183      * @param ast the AST that is to own this node
184      */

185     PrefixExpression(AST ast) {
186         super(ast);
187     }
188
189     /* (omit javadoc for this method)
190      * Method declared on ASTNode.
191      */

192     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
193         return propertyDescriptors(apiLevel);
194     }
195     
196     /* (omit javadoc for this method)
197      * Method declared on ASTNode.
198      */

199     final Object JavaDoc internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object JavaDoc value) {
200         if (property == OPERATOR_PROPERTY) {
201             if (get) {
202                 return getOperator();
203             } else {
204                 setOperator((Operator) value);
205                 return null;
206             }
207         }
208         // allow default implementation to flag the error
209
return super.internalGetSetObjectProperty(property, get, value);
210     }
211
212     /* (omit javadoc for this method)
213      * Method declared on ASTNode.
214      */

215     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
216         if (property == OPERAND_PROPERTY) {
217             if (get) {
218                 return getOperand();
219             } else {
220                 setOperand((Expression) child);
221                 return null;
222             }
223         }
224         // allow default implementation to flag the error
225
return super.internalGetSetChildProperty(property, get, child);
226     }
227     
228     /* (omit javadoc for this method)
229      * Method declared on ASTNode.
230      */

231     final int getNodeType0() {
232         return PREFIX_EXPRESSION;
233     }
234
235     /* (omit javadoc for this method)
236      * Method declared on ASTNode.
237      */

238     ASTNode clone0(AST target) {
239         PrefixExpression result = new PrefixExpression(target);
240         result.setSourceRange(this.getStartPosition(), this.getLength());
241         result.setOperator(getOperator());
242         result.setOperand((Expression) getOperand().clone(target));
243         return result;
244     }
245
246     /* (omit javadoc for this method)
247      * Method declared on ASTNode.
248      */

249     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
250         // dispatch to correct overloaded match method
251
return matcher.match(this, other);
252     }
253
254     /* (omit javadoc for this method)
255      * Method declared on ASTNode.
256      */

257     void accept0(ASTVisitor visitor) {
258         boolean visitChildren = visitor.visit(this);
259         if (visitChildren) {
260             // visit children in normal left to right reading order
261
acceptChild(visitor, getOperand());
262         }
263         visitor.endVisit(this);
264     }
265     
266     /**
267      * Returns the operator of this prefix expression.
268      *
269      * @return the operator
270      */

271     public PrefixExpression.Operator getOperator() {
272         return this.operator;
273     }
274
275     /**
276      * Sets the operator of this prefix expression.
277      *
278      * @param operator the operator
279      * @exception IllegalArgumentException if the argument is incorrect
280      */

281     public void setOperator(PrefixExpression.Operator operator) {
282         if (operator == null) {
283             throw new IllegalArgumentException JavaDoc();
284         }
285         preValueChange(OPERATOR_PROPERTY);
286         this.operator = operator;
287         postValueChange(OPERATOR_PROPERTY);
288     }
289
290     /**
291      * Returns the operand of this prefix expression.
292      *
293      * @return the operand expression node
294      */

295     public Expression getOperand() {
296         if (this.operand == null) {
297             // lazy init must be thread-safe for readers
298
synchronized (this) {
299                 if (this.operand == null) {
300                     preLazyInit();
301                     this.operand= new SimpleName(this.ast);
302                     postLazyInit(this.operand, OPERAND_PROPERTY);
303                 }
304             }
305         }
306         return this.operand;
307     }
308         
309     /**
310      * Sets the operand of this prefix expression.
311      *
312      * @param expression the operand expression node
313      * @exception IllegalArgumentException if:
314      * <ul>
315      * <li>the node belongs to a different AST</li>
316      * <li>the node already has a parent</li>
317      * <li>a cycle in would be created</li>
318      * </ul>
319      */

320     public void setOperand(Expression expression) {
321         if (expression == null) {
322             throw new IllegalArgumentException JavaDoc();
323         }
324         ASTNode oldChild = this.operand;
325         preReplaceChild(oldChild, expression, OPERAND_PROPERTY);
326         this.operand = expression;
327         postReplaceChild(oldChild, expression, OPERAND_PROPERTY);
328     }
329
330     /* (omit javadoc for this method)
331      * Method declared on ASTNode.
332      */

333     int memSize() {
334         // treat Operator as free
335
return BASE_NODE_SIZE + 2 * 4;
336     }
337     
338     /* (omit javadoc for this method)
339      * Method declared on ASTNode.
340      */

341     int treeSize() {
342         return
343             memSize()
344             + (this.operand == null ? 0 : getOperand().treeSize());
345     }
346 }
347
348
Popular Tags