KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
16
17 /**
18  * Expression statement AST node type.
19  * <p>
20  * This kind of node is used to convert an expression (<code>Expression</code>)
21  * into a statement (<code>Statement</code>) by wrapping it.
22  * </p>
23  * <pre>
24  * ExpressionStatement:
25  * StatementExpression <b>;</b>
26  * </pre>
27  *
28  * @since 2.0
29  */

30 public class ExpressionStatement extends Statement {
31     
32     /**
33      * The "expression" structural property of this node type.
34      * @since 3.0
35      */

36     public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
37         new ChildPropertyDescriptor(ExpressionStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
38

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

44     private static final List JavaDoc PROPERTY_DESCRIPTORS;
45     
46     static {
47         List JavaDoc properyList = new ArrayList JavaDoc(2);
48         createPropertyList(ExpressionStatement.class, properyList);
49         addProperty(EXPRESSION_PROPERTY, properyList);
50         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
51     }
52
53     /**
54      * Returns a list of structural property descriptors for this node type.
55      * Clients must not modify the result.
56      *
57      * @param apiLevel the API level; one of the
58      * <code>AST.JLS&ast;</code> constants
59
60      * @return a list of property descriptors (element type:
61      * {@link StructuralPropertyDescriptor})
62      * @since 3.0
63      */

64     public static List JavaDoc propertyDescriptors(int apiLevel) {
65         return PROPERTY_DESCRIPTORS;
66     }
67             
68     /**
69      * The expression; lazily initialized; defaults to a unspecified, but legal,
70      * expression.
71      */

72     private Expression expression = null;
73
74     /**
75      * Creates a new unparented expression statement node owned by the given
76      * AST. By default, the expression statement is unspecified, but legal,
77      * method invocation expression.
78      * <p>
79      * N.B. This constructor is package-private.
80      * </p>
81      *
82      * @param ast the AST that is to own this node
83      */

84     ExpressionStatement(AST ast) {
85         super(ast);
86     }
87
88     /* (omit javadoc for this method)
89      * Method declared on ASTNode.
90      */

91     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
92         return propertyDescriptors(apiLevel);
93     }
94     
95     /* (omit javadoc for this method)
96      * Method declared on ASTNode.
97      */

98     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
99         if (property == EXPRESSION_PROPERTY) {
100             if (get) {
101                 return getExpression();
102             } else {
103                 setExpression((Expression) child);
104                 return null;
105             }
106         }
107         // allow default implementation to flag the error
108
return super.internalGetSetChildProperty(property, get, child);
109     }
110
111     /* (omit javadoc for this method)
112      * Method declared on ASTNode.
113      */

114     final int getNodeType0() {
115         return EXPRESSION_STATEMENT;
116     }
117
118     /* (omit javadoc for this method)
119      * Method declared on ASTNode.
120      */

121     ASTNode clone0(AST target) {
122         ExpressionStatement result = new ExpressionStatement(target);
123         result.setSourceRange(this.getStartPosition(), this.getLength());
124         result.copyLeadingComment(this);
125         result.setExpression((Expression) getExpression().clone(target));
126         return result;
127     }
128     
129     /* (omit javadoc for this method)
130      * Method declared on ASTNode.
131      */

132     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
133         // dispatch to correct overloaded match method
134
return matcher.match(this, other);
135     }
136
137     /* (omit javadoc for this method)
138      * Method declared on ASTNode.
139      */

140     void accept0(ASTVisitor visitor) {
141         boolean visitChildren = visitor.visit(this);
142         if (visitChildren) {
143             acceptChild(visitor, getExpression());
144         }
145         visitor.endVisit(this);
146     }
147     
148     /**
149      * Returns the expression of this expression statement.
150      *
151      * @return the expression node
152      */

153     public Expression getExpression() {
154         if (this.expression == null) {
155             // lazy init must be thread-safe for readers
156
synchronized (this) {
157                 if (this.expression == null) {
158                     preLazyInit();
159                     this.expression = new MethodInvocation(this.ast);
160                     postLazyInit(this.expression, EXPRESSION_PROPERTY);
161                 }
162             }
163         }
164         return this.expression;
165     }
166         
167     /**
168      * Sets the expression of this expression statement.
169      *
170      * @param expression the new expression node
171      * @exception IllegalArgumentException if:
172      * <ul>
173      * <li>the node belongs to a different AST</li>
174      * <li>the node already has a parent</li>
175      * <li>a cycle in would be created</li>
176      * </ul>
177      */

178     public void setExpression(Expression expression) {
179         if (expression == null) {
180             throw new IllegalArgumentException JavaDoc();
181         }
182         ASTNode oldChild = this.expression;
183         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
184         this.expression = expression;
185         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
186     }
187     
188     /* (omit javadoc for this method)
189      * Method declared on ASTNode.
190      */

191     int memSize() {
192         return super.memSize() + 1 * 4;
193     }
194     
195     /* (omit javadoc for this method)
196      * Method declared on ASTNode.
197      */

198     int treeSize() {
199         return
200             memSize()
201             + (this.expression == null ? 0 : getExpression().treeSize());
202     }
203 }
204
205
Popular Tags