1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | package com.github.javaparser.ast.stmt; |
22 | |
23 | import com.github.javaparser.ast.AllFieldsConstructor; |
24 | import com.github.javaparser.ast.NodeList; |
25 | import com.github.javaparser.ast.expr.Expression; |
26 | import com.github.javaparser.ast.expr.NameExpr; |
27 | import com.github.javaparser.ast.nodeTypes.SwitchNode; |
28 | import com.github.javaparser.ast.observer.ObservableProperty; |
29 | import com.github.javaparser.ast.visitor.GenericVisitor; |
30 | import com.github.javaparser.ast.visitor.VoidVisitor; |
31 | import static com.github.javaparser.utils.Utils.assertNotNull; |
32 | import com.github.javaparser.ast.Node; |
33 | import com.github.javaparser.ast.visitor.CloneVisitor; |
34 | import com.github.javaparser.metamodel.SwitchStmtMetaModel; |
35 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
36 | import com.github.javaparser.TokenRange; |
37 | import java.util.function.Consumer; |
38 | import java.util.Optional; |
39 | import com.github.javaparser.ast.Generated; |
40 | |
41 | /** |
42 | * <h1>The switch statement</h1> |
43 | * |
44 | * <h2>Java 1.0-1.4</h2> |
45 | * The basic C-like switch statement. |
46 | * It can switch only on integers. |
47 | * <br>{@code switch(x) { case 5: case 6: a=100; break; case 9: a=33; break; default: throw new IllegalStateException(); };} |
48 | * <br>In {@code switch(a) { ... }} the selector is "a", |
49 | * and the contents of the { ... } are the entries. |
50 | * |
51 | * <h2>Java 5-6</h2> |
52 | * Switching can now also be done on enum constants. |
53 | * |
54 | * <h2>Java 7-11</h2> |
55 | * Switching can now also be done on strings. |
56 | * |
57 | * <h2>Java 12</h2> |
58 | * In preparation for pattern matching, lots of changes are made: |
59 | * <ul> |
60 | * <li>multiple labels per case |
61 | * <li>a -> syntax that does not fall through. |
62 | * <li>break can take any expression (usable in the {@link com.github.javaparser.ast.expr.SwitchExpr}) |
63 | * <li>switch can be used as an expression (it becomes a {@link com.github.javaparser.ast.expr.SwitchExpr}) |
64 | * </ul> |
65 | * {@code switch(x) { case BANANA,PEAR: b=10; break; default: b=5; };} |
66 | * <br>{@code switch(x) { case 5,6 -> println("uhuh"); default -> println("nope"); };} |
67 | * |
68 | * <h2>Java 13</h2> |
69 | * The break statement has been reverted to what it was before Java 12, and break-with-value is now the YieldStatement. |
70 | * |
71 | * @author Julio Vilmar Gesser |
72 | * @see SwitchEntry |
73 | * @see com.github.javaparser.ast.expr.SwitchExpr |
74 | * @see SwitchNode |
75 | * @see BreakStmt |
76 | * @see YieldStmt |
77 | */ |
78 | public class SwitchStmt extends Statement implements SwitchNode { |
79 | |
80 | private Expression selector; |
81 | |
82 | private NodeList<SwitchEntry> entries; |
83 | |
84 | public SwitchStmt() { |
85 | this(null, new NameExpr(), new NodeList<>()); |
86 | } |
87 | |
88 | @AllFieldsConstructor |
89 | public SwitchStmt(final Expression selector, final NodeList<SwitchEntry> entries) { |
90 | this(null, selector, entries); |
91 | } |
92 | |
93 | /** |
94 | * This constructor is used by the parser and is considered private. |
95 | */ |
96 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
97 | public SwitchStmt(TokenRange tokenRange, Expression selector, NodeList<SwitchEntry> entries) { |
98 | super(tokenRange); |
99 | setSelector(selector); |
100 | setEntries(entries); |
101 | customInitialization(); |
102 | } |
103 | |
104 | @Override |
105 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
106 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
107 | return v.visit(this, arg); |
108 | } |
109 | |
110 | @Override |
111 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
112 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
113 | v.visit(this, arg); |
114 | } |
115 | |
116 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
117 | public NodeList<SwitchEntry> getEntries() { |
118 | return entries; |
119 | } |
120 | |
121 | public SwitchEntry getEntry(int i) { |
122 | return getEntries().get(i); |
123 | } |
124 | |
125 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
126 | public Expression getSelector() { |
127 | return selector; |
128 | } |
129 | |
130 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
131 | public SwitchStmt setEntries(final NodeList<SwitchEntry> entries) { |
132 | assertNotNull(entries); |
133 | if (entries == this.entries) { |
134 | return (SwitchStmt) this; |
135 | } |
136 | notifyPropertyChange(ObservableProperty.ENTRIES, this.entries, entries); |
137 | if (this.entries != null) |
138 | this.entries.setParentNode(null); |
139 | this.entries = entries; |
140 | setAsParentNodeOf(entries); |
141 | return this; |
142 | } |
143 | |
144 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
145 | public SwitchStmt setSelector(final Expression selector) { |
146 | assertNotNull(selector); |
147 | if (selector == this.selector) { |
148 | return (SwitchStmt) this; |
149 | } |
150 | notifyPropertyChange(ObservableProperty.SELECTOR, this.selector, selector); |
151 | if (this.selector != null) |
152 | this.selector.setParentNode(null); |
153 | this.selector = selector; |
154 | setAsParentNodeOf(selector); |
155 | return this; |
156 | } |
157 | |
158 | @Override |
159 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
160 | public boolean remove(Node node) { |
161 | if (node == null) |
162 | return false; |
163 | for (int i = 0; i < entries.size(); i++) { |
164 | if (entries.get(i) == node) { |
165 | entries.remove(i); |
166 | return true; |
167 | } |
168 | } |
169 | return super.remove(node); |
170 | } |
171 | |
172 | @Override |
173 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
174 | public SwitchStmt clone() { |
175 | return (SwitchStmt) accept(new CloneVisitor(), null); |
176 | } |
177 | |
178 | @Override |
179 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
180 | public SwitchStmtMetaModel getMetaModel() { |
181 | return JavaParserMetaModel.switchStmtMetaModel; |
182 | } |
183 | |
184 | @Override |
185 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
186 | public boolean replace(Node node, Node replacementNode) { |
187 | if (node == null) |
188 | return false; |
189 | for (int i = 0; i < entries.size(); i++) { |
190 | if (entries.get(i) == node) { |
191 | entries.set(i, (SwitchEntry) replacementNode); |
192 | return true; |
193 | } |
194 | } |
195 | if (node == selector) { |
196 | setSelector((Expression) replacementNode); |
197 | return true; |
198 | } |
199 | return super.replace(node, replacementNode); |
200 | } |
201 | |
202 | @Override |
203 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
204 | public boolean isSwitchStmt() { |
205 | return true; |
206 | } |
207 | |
208 | @Override |
209 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
210 | public SwitchStmt asSwitchStmt() { |
211 | return this; |
212 | } |
213 | |
214 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
215 | public void ifSwitchStmt(Consumer<SwitchStmt> action) { |
216 | action.accept(this); |
217 | } |
218 | |
219 | @Override |
220 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
221 | public Optional<SwitchStmt> toSwitchStmt() { |
222 | return Optional.of(this); |
223 | } |
224 | } |
225 |
Members