EclipseJDT Source Viewer

Home|eclipse_jdt/src/org/eclipse/jdt/core/dom/SwitchExpression.java
1/*******************************************************************************
2 * Copyright (c) 2019, 2020 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 * Switch expression AST node type (added in JEP 325).
22 * <pre>
23 * SwitchExpression:
24 *        <b>switch</b> <b>(</b> Expression <b>)</b>
25 *             <b>{</b> { SwitchCase | Statement } <b>}</b>
26 * SwitchCase:
27 *        <b>case</b> [ Expression { <b>,</b> Expression } ]  <b>{ : | ->}</b>
28 *        <b>default</b> <b>{ : | ->}</b>
29 * </pre>
30 * <code>SwitchCase</code> nodes are treated as a kind of
31 * <code>Statement</code>.
32 *
33 * @since 3.22
34 */
35@SuppressWarnings({"rawtypes""unchecked"})
36public class SwitchExpression extends Expression {
37
38    /**
39     * The "expression" structural property of this node type (child type: {@link Expression}).
40     * @since 3.22
41     */
42    public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
43        new ChildPropertyDescriptor(SwitchExpression.class"expression"Expression.classMANDATORYCYCLE_RISK); //$NON-NLS-1$
44
45    /**
46     * The "statements" structural property of this node type (element type: {@link Statement}).
47     * @since 3.22
48     */
49    public static final ChildListPropertyDescriptor STATEMENTS_PROPERTY =
50        new ChildListPropertyDescriptor(SwitchExpression.class"statements"Statement.classCYCLE_RISK); //$NON-NLS-1$
51
52    /**
53     * A list of property descriptors (element type:
54     * {@link StructuralPropertyDescriptor}),
55     * or null if uninitialized.
56     */
57    private static final List PROPERTY_DESCRIPTORS;
58
59    static {
60        List propertyList = new ArrayList(3);
61        createPropertyList(SwitchExpression.classpropertyList);
62        addProperty(EXPRESSION_PROPERTYpropertyList);
63        addProperty(STATEMENTS_PROPERTYpropertyList);
64        PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
65    }
66
67    /**
68     * Returns a list of structural property descriptors for this node type.
69     * Clients must not modify the result.
70     *
71     * @param apiLevel the API level; one of the
72     * <code>AST.JLS*</code> constants
73     * @return a list of property descriptors (element type:
74     * {@link StructuralPropertyDescriptor})
75     * @since 3.22
76     */
77    public static List propertyDescriptors(int apiLevel) {
78        return PROPERTY_DESCRIPTORS;
79    }
80
81    /**
82     * The expression; lazily initialized; defaults to a unspecified, but legal,
83     * expression.
84     */
85    private Expression expression = null;
86
87    /**
88     * The statements and SwitchCase nodes
89     * (element type: {@link Statement}).
90     * Defaults to an empty list.
91     */
92    private ASTNode.NodeList statements =
93        new ASTNode.NodeList(STATEMENTS_PROPERTY);
94
95    /**
96     * Creates a new unparented switch statement node owned by the given
97     * AST. By default, the swicth statement has an unspecified, but legal,
98     * expression, and an empty list of switch groups.
99     * <p>
100     * N.B. This constructor is package-private.
101     * </p>
102     *
103     * @param ast the AST that is to own this node
104     * @exception UnsupportedOperationException if this operation is used below JLS14
105     */
106    SwitchExpression(AST ast) {
107        super(ast);
108        unsupportedBelow14();
109    }
110
111    @Override
112    final List internalStructuralPropertiesForType(int apiLevel) {
113        return propertyDescriptors(apiLevel);
114    }
115
116    @Override
117    final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor propertyboolean getASTNode child) {
118        if (property == EXPRESSION_PROPERTY) {
119            if (get) {
120                return getExpression();
121            } else {
122                setExpression((Expressionchild);
123                return null;
124            }
125        }
126        // allow default implementation to flag the error
127        return super.internalGetSetChildProperty(propertygetchild);
128    }
129
130    @Override
131    final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
132        if (property == STATEMENTS_PROPERTY) {
133            return statements();
134        }
135        // allow default implementation to flag the error
136        return super.internalGetChildListProperty(property);
137    }
138
139    @Override
140    final int getNodeType0() {
141        return SWITCH_EXPRESSION;
142    }
143
144    @Override
145    ASTNode clone0(AST target) {
146        SwitchExpression result = new SwitchExpression(target);
147        result.setSourceRange(getStartPosition(), getLength());
148        result.setExpression((ExpressiongetExpression().clone(target));
149        result.statements().addAll(ASTNode.copySubtrees(targetstatements()));
150        return result;
151    }
152
153    @Override
154    final boolean subtreeMatch0(ASTMatcher matcherObject other) {
155        // dispatch to correct overloaded match method
156        return matcher.match(this, other);
157    }
158
159    @Override
160    void accept0(ASTVisitor visitor) {
161        boolean visitChildren = visitor.visit(this);
162        if (visitChildren) {
163            // visit children in normal left to right reading order
164            acceptChild(visitorgetExpression());
165            acceptChildren(visitor, this.statements);
166        }
167        visitor.endVisit(this);
168    }
169
170    /**
171     * Returns the expression of this switch statement.
172     *
173     * @return the expression node
174     * @since 3.22
175     */
176    public Expression getExpression() {
177        if (this.expression == null) {
178            // lazy init must be thread-safe for readers
179            synchronized (this) {
180                if (this.expression == null) {
181                    preLazyInit();
182                    this.expression = new SimpleName(this.ast);
183                    postLazyInit(this.expressionEXPRESSION_PROPERTY);
184                }
185            }
186        }
187        return this.expression;
188    }
189
190    /**
191     * Sets the expression of this switch statement.
192     *
193     * @param expression the new expression node
194     * @exception IllegalArgumentException if:
195     * <ul>
196     * <li>the node belongs to a different AST</li>
197     * <li>the node already has a parent</li>
198     * <li>a cycle in would be created</li>
199     * </ul>
200     * @since 3.22
201     */
202    public void setExpression(Expression expression) {
203        if (expression == null) {
204            throw new IllegalArgumentException();
205        }
206        ASTNode oldChild = this.expression;
207        preReplaceChild(oldChildexpressionEXPRESSION_PROPERTY);
208        this.expression = expression;
209        postReplaceChild(oldChildexpressionEXPRESSION_PROPERTY);
210    }
211
212    /**
213     * Returns the live ordered list of statements for this switch statement.
214     * Within this list, <code>SwitchCase</code> nodes mark the start of
215     * the switch groups.
216     *
217     * @return the live list of statement nodes
218     *    (element type: {@link Statement})
219     * @since 3.22
220     */
221    public List statements() {
222        return this.statements;
223    }
224
225    @Override
226    int memSize() {
227        return BASE_NODE_SIZE + 3 * 4;
228    }
229
230    @Override
231    int treeSize() {
232        return
233            memSize()
234            + (this.expression == null ? 0 : getExpression().treeSize())
235            + this.statements.listSize();
236    }
237}
238
MembersX
SwitchExpression:propertyDescriptors
SwitchExpression:clone0:Block:result
SwitchExpression:clone0
SwitchExpression:accept0
SwitchExpression:Block:propertyList
SwitchExpression:internalStructuralPropertiesForType
SwitchExpression:internalGetSetChildProperty
SwitchExpression:EXPRESSION_PROPERTY
SwitchExpression:statements
SwitchExpression:internalGetChildListProperty
SwitchExpression:treeSize
SwitchExpression:PROPERTY_DESCRIPTORS
SwitchExpression:accept0:Block:visitChildren
SwitchExpression:setExpression
SwitchExpression:setExpression:Block:oldChild
SwitchExpression:memSize
SwitchExpression:getExpression
SwitchExpression:subtreeMatch0
SwitchExpression:STATEMENTS_PROPERTY
SwitchExpression:expression
SwitchExpression:getNodeType0
SwitchExpression:SwitchExpression
Members
X