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.TokenRange; |
24 | import com.github.javaparser.ast.AllFieldsConstructor; |
25 | import com.github.javaparser.ast.Generated; |
26 | import com.github.javaparser.ast.Modifier; |
27 | import com.github.javaparser.ast.Node; |
28 | import com.github.javaparser.ast.body.VariableDeclarator; |
29 | import com.github.javaparser.ast.expr.Expression; |
30 | import com.github.javaparser.ast.expr.NameExpr; |
31 | import com.github.javaparser.ast.expr.VariableDeclarationExpr; |
32 | import com.github.javaparser.ast.nodeTypes.NodeWithBody; |
33 | import com.github.javaparser.ast.observer.ObservableProperty; |
34 | import com.github.javaparser.ast.visitor.CloneVisitor; |
35 | import com.github.javaparser.ast.visitor.GenericVisitor; |
36 | import com.github.javaparser.ast.visitor.VoidVisitor; |
37 | import com.github.javaparser.metamodel.ForEachStmtMetaModel; |
38 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
39 | import java.util.Optional; |
40 | import java.util.function.Consumer; |
41 | import static com.github.javaparser.utils.Utils.assertNotNull; |
42 | |
43 | /** |
44 | * A for-each statement. |
45 | * <br>{@code for(Object o: objects) { ... }} |
46 | * It was introduced in Java 5. |
47 | * |
48 | * @author Julio Vilmar Gesser |
49 | */ |
50 | public class ForEachStmt extends Statement implements NodeWithBody<ForEachStmt> { |
51 | |
52 | private VariableDeclarationExpr variable; |
53 | |
54 | private Expression iterable; |
55 | |
56 | private Statement body; |
57 | |
58 | public ForEachStmt() { |
59 | this(null, new VariableDeclarationExpr(), new NameExpr(), new ReturnStmt()); |
60 | } |
61 | |
62 | @AllFieldsConstructor |
63 | public ForEachStmt(final VariableDeclarationExpr variable, final Expression iterable, final Statement body) { |
64 | this(null, variable, iterable, body); |
65 | } |
66 | |
67 | /** |
68 | * This constructor is used by the parser and is considered private. |
69 | */ |
70 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
71 | public ForEachStmt(TokenRange tokenRange, VariableDeclarationExpr variable, Expression iterable, Statement body) { |
72 | super(tokenRange); |
73 | setVariable(variable); |
74 | setIterable(iterable); |
75 | setBody(body); |
76 | customInitialization(); |
77 | } |
78 | |
79 | public ForEachStmt(VariableDeclarationExpr variable, String iterable, BlockStmt body) { |
80 | this(null, variable, new NameExpr(iterable), body); |
81 | } |
82 | |
83 | @Override |
84 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
85 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
86 | return v.visit(this, arg); |
87 | } |
88 | |
89 | @Override |
90 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
91 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
92 | v.visit(this, arg); |
93 | } |
94 | |
95 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
96 | public Statement getBody() { |
97 | return body; |
98 | } |
99 | |
100 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
101 | public Expression getIterable() { |
102 | return iterable; |
103 | } |
104 | |
105 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
106 | public VariableDeclarationExpr getVariable() { |
107 | return variable; |
108 | } |
109 | |
110 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
111 | public ForEachStmt setBody(final Statement body) { |
112 | assertNotNull(body); |
113 | if (body == this.body) { |
114 | return (ForEachStmt) this; |
115 | } |
116 | notifyPropertyChange(ObservableProperty.BODY, this.body, body); |
117 | if (this.body != null) |
118 | this.body.setParentNode(null); |
119 | this.body = body; |
120 | setAsParentNodeOf(body); |
121 | return this; |
122 | } |
123 | |
124 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
125 | public ForEachStmt setIterable(final Expression iterable) { |
126 | assertNotNull(iterable); |
127 | if (iterable == this.iterable) { |
128 | return (ForEachStmt) this; |
129 | } |
130 | notifyPropertyChange(ObservableProperty.ITERABLE, this.iterable, iterable); |
131 | if (this.iterable != null) |
132 | this.iterable.setParentNode(null); |
133 | this.iterable = iterable; |
134 | setAsParentNodeOf(iterable); |
135 | return this; |
136 | } |
137 | |
138 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
139 | public ForEachStmt setVariable(final VariableDeclarationExpr variable) { |
140 | assertNotNull(variable); |
141 | if (variable == this.variable) { |
142 | return (ForEachStmt) this; |
143 | } |
144 | notifyPropertyChange(ObservableProperty.VARIABLE, this.variable, variable); |
145 | if (this.variable != null) |
146 | this.variable.setParentNode(null); |
147 | this.variable = variable; |
148 | setAsParentNodeOf(variable); |
149 | return this; |
150 | } |
151 | |
152 | /** |
153 | * Convenience method that directly returns this foreach statement's single variable declarator. |
154 | * Note that any foreach statement's variable declaration expression (as returned by {@link #getVariable()}) always |
155 | * has exactly one variable declarator. |
156 | * <p> |
157 | * Calling this method on a foreach statement {@code forEachStmt} is equivalent to calling |
158 | * {@code forEachStmt.getVariable().getVariable(0)}. |
159 | * |
160 | * @return this foreach statement's single variable declarator. |
161 | */ |
162 | public VariableDeclarator getVariableDeclarator() { |
163 | return getVariable().getVariable(0); |
164 | } |
165 | |
166 | /** |
167 | * Convenience method that decides whether this foreach statement's variable is {@code final}. |
168 | * Note that any foreach statement's variable declaration expression (as returned by {@link #getVariable()}) always |
169 | * has either no modifiers, or a single {@code final} modifier. |
170 | * <p> |
171 | * Calling this method on a foreach statement {@code forEachStmt} is equivalent to calling |
172 | * {@code forEachStmt.getVariable().getModifiers().isNonEmpty() && |
173 | * forEachStmt.getVariable().getModifiers().get(0).getKeyword() == Modifier.Keyword.FINAL}. |
174 | * |
175 | * @return {@code true} if this foreach statement's variable is {@code final}, and {@code false} otherwise. |
176 | */ |
177 | public boolean hasFinalVariable() { |
178 | return getVariable().getModifiers().isNonEmpty() && getVariable().getModifiers().get(0).getKeyword() == Modifier.Keyword.FINAL; |
179 | } |
180 | |
181 | @Override |
182 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
183 | public boolean remove(Node node) { |
184 | if (node == null) |
185 | return false; |
186 | return super.remove(node); |
187 | } |
188 | |
189 | @Override |
190 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
191 | public ForEachStmt clone() { |
192 | return (ForEachStmt) accept(new CloneVisitor(), null); |
193 | } |
194 | |
195 | @Override |
196 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
197 | public boolean replace(Node node, Node replacementNode) { |
198 | if (node == null) |
199 | return false; |
200 | if (node == body) { |
201 | setBody((Statement) replacementNode); |
202 | return true; |
203 | } |
204 | if (node == iterable) { |
205 | setIterable((Expression) replacementNode); |
206 | return true; |
207 | } |
208 | if (node == variable) { |
209 | setVariable((VariableDeclarationExpr) replacementNode); |
210 | return true; |
211 | } |
212 | return super.replace(node, replacementNode); |
213 | } |
214 | |
215 | @Override |
216 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
217 | public boolean isForEachStmt() { |
218 | return true; |
219 | } |
220 | |
221 | @Override |
222 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
223 | public ForEachStmt asForEachStmt() { |
224 | return this; |
225 | } |
226 | |
227 | @Override |
228 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
229 | public Optional<ForEachStmt> toForEachStmt() { |
230 | return Optional.of(this); |
231 | } |
232 | |
233 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
234 | public void ifForEachStmt(Consumer<ForEachStmt> action) { |
235 | action.accept(this); |
236 | } |
237 | |
238 | @Override |
239 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
240 | public ForEachStmtMetaModel getMetaModel() { |
241 | return JavaParserMetaModel.forEachStmtMetaModel; |
242 | } |
243 | } |
244 |
Members