EclipseJDT Source Viewer

Home|eclipse_jdt/src/org/eclipse/jdt/core/dom/WhileStatement.java
1/*******************************************************************************
2 * Copyright (c) 2000, 2013 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 *     IBM Corporation - initial API and implementation
13 *******************************************************************************/
14
15package org.eclipse.jdt.core.dom;
16
17import java.util.ArrayList;
18import java.util.List;
19
20/**
21 * While statement AST node type.
22 *
23 * <pre>
24 * WhileStatement:
25 *    <b>while</b> <b>(</b> Expression <b>)</b> Statement
26 * </pre>
27 *
28 * @since 2.0
29 * @noinstantiate This class is not intended to be instantiated by clients.
30 */
31@SuppressWarnings("rawtypes")
32public class WhileStatement extends Statement {
33
34    /**
35     * The "expression" structural property of this node type (child type: {@link Expression}).
36     * @since 3.0
37     */
38    public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
39        new ChildPropertyDescriptor(WhileStatement.class"expression"Expression.classMANDATORYCYCLE_RISK); //$NON-NLS-1$
40
41    /**
42     * The "body" structural property of this node type (child type: {@link Statement}).
43     * @since 3.0
44     */
45    public static final ChildPropertyDescriptor BODY_PROPERTY =
46        new ChildPropertyDescriptor(WhileStatement.class"body"Statement.classMANDATORYCYCLE_RISK); //$NON-NLS-1$
47
48    /**
49     * A list of property descriptors (element type:
50     * {@link StructuralPropertyDescriptor}),
51     * or null if uninitialized.
52     */
53    private static final List PROPERTY_DESCRIPTORS;
54
55    static {
56        List propertyList = new ArrayList(3);
57        createPropertyList(WhileStatement.classpropertyList);
58        addProperty(EXPRESSION_PROPERTYpropertyList);
59        addProperty(BODY_PROPERTYpropertyList);
60        PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
61    }
62
63    /**
64     * Returns a list of structural property descriptors for this node type.
65     * Clients must not modify the result.
66     *
67     * @param apiLevel the API level; one of the
68     * <code>AST.JLS*</code> constants
69
70     * @return a list of property descriptors (element type:
71     * {@link StructuralPropertyDescriptor})
72     * @since 3.0
73     */
74    public static List propertyDescriptors(int apiLevel) {
75        return PROPERTY_DESCRIPTORS;
76    }
77
78    /**
79     * The expression; lazily initialized; defaults to an unspecified, but
80     * legal, expression.
81     */
82    private Expression expression = null;
83
84    /**
85     * The body statement; lazily initialized; defaults to an empty block
86     * statement.
87     */
88    private Statement body = null;
89
90    /**
91     * Creates a new unparented while statement node owned by the given
92     * AST. By default, the expresssion is unspecified, but legal, and
93     * the body statement is an empty block.
94     * <p>
95     * N.B. This constructor is package-private.
96     * </p>
97     *
98     * @param ast the AST that is to own this node
99     */
100    WhileStatement(AST ast) {
101        super(ast);
102    }
103
104    @Override
105    final List internalStructuralPropertiesForType(int apiLevel) {
106        return propertyDescriptors(apiLevel);
107    }
108
109    @Override
110    final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor propertyboolean getASTNode child) {
111        if (property == EXPRESSION_PROPERTY) {
112            if (get) {
113                return getExpression();
114            } else {
115                setExpression((Expressionchild);
116                return null;
117            }
118        }
119        if (property == BODY_PROPERTY) {
120            if (get) {
121                return getBody();
122            } else {
123                setBody((Statementchild);
124                return null;
125            }
126        }
127        // allow default implementation to flag the error
128        return super.internalGetSetChildProperty(propertygetchild);
129    }
130
131    @Override
132    final int getNodeType0() {
133        return WHILE_STATEMENT;
134    }
135
136    @Override
137    ASTNode clone0(AST target) {
138        WhileStatement result = new WhileStatement(target);
139        result.setSourceRange(getStartPosition(), getLength());
140        result.copyLeadingComment(this);
141        result.setExpression((ExpressiongetExpression().clone(target));
142        result.setBody((StatementgetBody().clone(target));
143        return result;
144    }
145
146    @Override
147    final boolean subtreeMatch0(ASTMatcher matcherObject other) {
148        // dispatch to correct overloaded match method
149        return matcher.match(this, other);
150    }
151
152    @Override
153    void accept0(ASTVisitor visitor) {
154        boolean visitChildren = visitor.visit(this);
155        if (visitChildren) {
156            // visit children in normal left to right reading order
157            acceptChild(visitorgetExpression());
158            acceptChild(visitorgetBody());
159        }
160        visitor.endVisit(this);
161    }
162
163    /**
164     * Returns the expression of this while statement.
165     *
166     * @return the expression node
167     */
168    public Expression getExpression() {
169        if (this.expression == null) {
170            // lazy init must be thread-safe for readers
171            synchronized (this) {
172                if (this.expression == null) {
173                    preLazyInit();
174                    this.expression = new SimpleName(this.ast);
175                    postLazyInit(this.expressionEXPRESSION_PROPERTY);
176                }
177            }
178        }
179        return this.expression;
180    }
181
182    /**
183     * Sets the expression of this while statement.
184     *
185     * @param expression the expression node
186     * @exception IllegalArgumentException if:
187     * <ul>
188     * <li>the node belongs to a different AST</li>
189     * <li>the node already has a parent</li>
190     * <li>a cycle in would be created</li>
191     * </ul>
192     */
193    public void setExpression(Expression expression) {
194        if (expression == null) {
195            throw new IllegalArgumentException();
196        }
197        ASTNode oldChild = this.expression;
198        preReplaceChild(oldChildexpressionEXPRESSION_PROPERTY);
199        this.expression = expression;
200        postReplaceChild(oldChildexpressionEXPRESSION_PROPERTY);
201    }
202
203    /**
204     * Returns the body of this while statement.
205     *
206     * @return the body statement node
207     */
208    public Statement getBody() {
209        if (this.body == null) {
210            // lazy init must be thread-safe for readers
211            synchronized (this) {
212                if (this.body == null) {
213                    preLazyInit();
214                    this.body = new Block(this.ast);
215                    postLazyInit(this.bodyBODY_PROPERTY);
216                }
217            }
218        }
219        return this.body;
220    }
221
222    /**
223     * Sets the body of this while statement.
224     * <p>
225     * Special note: The Java language does not allow a local variable declaration
226     * to appear as the body of a while statement (they may only appear within a
227     * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
228     * as the body of a <code>WhileStatement</code>. To get something that will
229     * compile, be sure to embed the <code>VariableDeclarationStatement</code>
230     * inside a <code>Block</code>.
231     * </p>
232     *
233     * @param statement the body statement node
234     * @exception IllegalArgumentException if:
235     * <ul>
236     * <li>the node belongs to a different AST</li>
237     * <li>the node already has a parent</li>
238     * <li>a cycle in would be created</li>
239     * </ul>
240     */
241    public void setBody(Statement statement) {
242        if (statement == null) {
243            throw new IllegalArgumentException();
244        }
245        ASTNode oldChild = this.body;
246        preReplaceChild(oldChildstatementBODY_PROPERTY);
247        this.body = statement;
248        postReplaceChild(oldChildstatementBODY_PROPERTY);
249    }
250
251    @Override
252    int memSize() {
253        return super.memSize() + 2 * 4;
254    }
255
256    @Override
257    int treeSize() {
258        return
259            memSize()
260            + (this.expression == null ? 0 : getExpression().treeSize())
261            + (this.body == null ? 0 : getBody().treeSize());
262    }
263}
264
265
MembersX
WhileStatement:subtreeMatch0
WhileStatement:setBody
WhileStatement:WhileStatement
WhileStatement:setBody:Block:oldChild
WhileStatement:accept0
WhileStatement:getBody
WhileStatement:body
WhileStatement:BODY_PROPERTY
WhileStatement:accept0:Block:visitChildren
WhileStatement:setExpression:Block:oldChild
WhileStatement:getExpression
WhileStatement:internalStructuralPropertiesForType
WhileStatement:PROPERTY_DESCRIPTORS
WhileStatement:Block:propertyList
WhileStatement:propertyDescriptors
WhileStatement:internalGetSetChildProperty
WhileStatement:getNodeType0
WhileStatement:clone0
WhileStatement:expression
WhileStatement:treeSize
WhileStatement:memSize
WhileStatement:clone0:Block:result
WhileStatement:EXPRESSION_PROPERTY
WhileStatement:setExpression
Members
X