EclipseJDT Source Viewer

Home|eclipse_jdt/src/org/eclipse/jdt/core/dom/LabeledStatement.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 * Labeled statement AST node type.
22 *
23 * <pre>
24 * LabeledStatement:
25 *    Identifier <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 LabeledStatement extends Statement {
33
34    /**
35     * The "label" structural property of this node type (child type: {@link SimpleName}).
36     * @since 3.0
37     */
38    public static final ChildPropertyDescriptor LABEL_PROPERTY =
39        new ChildPropertyDescriptor(LabeledStatement.class"label"SimpleName.classMANDATORYNO_CYCLE_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(LabeledStatement.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(LabeledStatement.classpropertyList);
58        addProperty(LABEL_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 label; lazily initialized; defaults to a unspecified,
80     * legal Java identifier.
81     */
82    private SimpleName labelName = null;
83
84    /**
85     * The body statement; lazily initialized; defaults to an unspecified, but
86     * legal, statement.
87     */
88    private Statement body = null;
89
90    /**
91     * Creates a new AST node for a labeled statement owned by the given
92     * AST. By default, the statement has an unspecified (but legal) label
93     * and an unspecified (but legal) statement.
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    LabeledStatement(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 == LABEL_PROPERTY) {
112            if (get) {
113                return getLabel();
114            } else {
115                setLabel((SimpleNamechild);
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 LABELED_STATEMENT;
134    }
135
136    @Override
137    ASTNode clone0(AST target) {
138        LabeledStatement result = new LabeledStatement(target);
139        result.setSourceRange(getStartPosition(), getLength());
140        result.setLabel(
141            (SimpleNameASTNode.copySubtree(targetgetLabel()));
142        result.setBody(
143            (StatementASTNode.copySubtree(targetgetBody()));
144        return result;
145    }
146
147    @Override
148    final boolean subtreeMatch0(ASTMatcher matcherObject other) {
149        // dispatch to correct overloaded match method
150        return matcher.match(this, other);
151    }
152
153    @Override
154    void accept0(ASTVisitor visitor) {
155        boolean visitChildren = visitor.visit(this);
156        if (visitChildren) {
157            // visit children in normal left to right reading order
158            acceptChild(visitorgetLabel());
159            acceptChild(visitorgetBody());
160        }
161        visitor.endVisit(this);
162    }
163
164    /**
165     * Returns the label of this labeled statement.
166     *
167     * @return the variable name node
168     */
169    public SimpleName getLabel() {
170        if (this.labelName == null) {
171            // lazy init must be thread-safe for readers
172            synchronized (this) {
173                if (this.labelName == null) {
174                    preLazyInit();
175                    this.labelName= new SimpleName(this.ast);
176                    postLazyInit(this.labelNameLABEL_PROPERTY);
177                }
178            }
179        }
180        return this.labelName;
181    }
182
183    /**
184     * Sets the label of this labeled statement.
185     *
186     * @param label the new label
187     * @exception IllegalArgumentException if:
188     * <ul>
189     * <li>the node belongs to a different AST</li>
190     * <li>the node already has a parent</li>
191     * </ul>
192     */
193    public void setLabel(SimpleName label) {
194        if (label == null) {
195            throw new IllegalArgumentException();
196        }
197        ASTNode oldChild = this.labelName;
198        preReplaceChild(oldChildlabelLABEL_PROPERTY);
199        this.labelName = label;
200        postReplaceChild(oldChildlabelLABEL_PROPERTY);
201    }
202
203    /**
204     * Returns the body of this labeled 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 EmptyStatement(this.ast);
215                    postLazyInit(this.bodyBODY_PROPERTY);
216                }
217            }
218        }
219        return this.body;
220    }
221
222    /**
223     * Sets the body of this labeled statement.
224     * <p>
225     * Special note: The Java language does not allow a local variable declaration
226     * to appear as the body of a labeled 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>LabeledStatement</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.labelName == null ? 0 : getLabel().treeSize())
261            + (this.body == null ? 0 : getBody().treeSize());
262    }
263}
264
265
MembersX
LabeledStatement:setBody:Block:oldChild
LabeledStatement:memSize
LabeledStatement:LabeledStatement
LabeledStatement:body
LabeledStatement:clone0
LabeledStatement:LABEL_PROPERTY
LabeledStatement:getNodeType0
LabeledStatement:Block:propertyList
LabeledStatement:treeSize
LabeledStatement:setLabel
LabeledStatement:propertyDescriptors
LabeledStatement:internalStructuralPropertiesForType
LabeledStatement:PROPERTY_DESCRIPTORS
LabeledStatement:accept0
LabeledStatement:setLabel:Block:oldChild
LabeledStatement:BODY_PROPERTY
LabeledStatement:clone0:Block:result
LabeledStatement:accept0:Block:visitChildren
LabeledStatement:subtreeMatch0
LabeledStatement:getLabel
LabeledStatement:setBody
LabeledStatement:getBody
LabeledStatement:labelName
LabeledStatement:internalGetSetChildProperty
Members
X