JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/stmt/ForStmt.java
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 */
21package com.github.javaparser.ast.stmt;
22
23import com.github.javaparser.TokenRange;
24import com.github.javaparser.ast.AllFieldsConstructor;
25import com.github.javaparser.ast.Node;
26import com.github.javaparser.ast.NodeList;
27import com.github.javaparser.ast.expr.BooleanLiteralExpr;
28import com.github.javaparser.ast.expr.Expression;
29import com.github.javaparser.ast.nodeTypes.NodeWithBody;
30import com.github.javaparser.ast.observer.ObservableProperty;
31import com.github.javaparser.ast.visitor.CloneVisitor;
32import com.github.javaparser.ast.visitor.GenericVisitor;
33import com.github.javaparser.ast.visitor.VoidVisitor;
34import com.github.javaparser.metamodel.ForStmtMetaModel;
35import com.github.javaparser.metamodel.JavaParserMetaModel;
36import com.github.javaparser.metamodel.OptionalProperty;
37import java.util.Optional;
38import java.util.function.Consumer;
39import static com.github.javaparser.utils.Utils.assertNotNull;
40import com.github.javaparser.ast.Generated;
41
42/**
43 * <h1>The classic for statement</h1>
44 * Examples:
45 * <ol>
46 * <li>{@code for(int a=3, b=5; a<99; a++, b++) hello();}</li>
47 * <li>{@code for(a=3, b=5; a<99; a++) { hello(); }} </li>
48 * <li>{@code for(a(),b();;) hello();} </li>
49 * </ol>
50 * <ul>
51 * <li><i>initialization</i> is a list of expressions.
52 * These can be any kind of expression as can be seen in example 3,
53 * but the common ones are a single VariableDeclarationExpr (which declares multiple variables) in example 1,
54 * or a list of AssignExpr's in example 2.</li>
55 * <li><i>compare</i> is an expression,
56 * in example 1 and 2 it is a BinaryExpr.
57 * In example 3 there is no expression, it is empty.</li>
58 * <li><i>update</i> is a list of expressions,
59 * in example 1 and 2 they are UnaryExpr's.
60 * In example 3 there is no expression, the list empty.</li>
61 * <li><i>body</i> is a statement,
62 * in example 1 and 3 it is an ExpressionStmt.
63 * in example 2 it is a BlockStmt.</li>
64 * </ul>
65 *
66 * @author Julio Vilmar Gesser
67 * @see com.github.javaparser.ast.expr.VariableDeclarationExpr
68 */
69public class ForStmt extends Statement implements NodeWithBody<ForStmt> {
70
71    private NodeList<Expressioninitialization;
72
73    @OptionalProperty
74    private Expression compare;
75
76    private NodeList<Expressionupdate;
77
78    private Statement body;
79
80    public ForStmt() {
81        this(null, new NodeList<>(), new BooleanLiteralExpr(), new NodeList<>(), new ReturnStmt());
82    }
83
84    @AllFieldsConstructor
85    public ForStmt(final NodeList<Expressioninitializationfinal Expression comparefinal NodeList<Expressionupdatefinal Statement body) {
86        this(nullinitializationcompareupdatebody);
87    }
88
89    /**
90     * This constructor is used by the parser and is considered private.
91     */
92    @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
93    public ForStmt(TokenRange tokenRangeNodeList<ExpressioninitializationExpression compareNodeList<ExpressionupdateStatement body) {
94        super(tokenRange);
95        setInitialization(initialization);
96        setCompare(compare);
97        setUpdate(update);
98        setBody(body);
99        customInitialization();
100    }
101
102    @Override
103    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
104    public <RAR accept(final GenericVisitor<RAvfinal A arg) {
105        return v.visit(this, arg);
106    }
107
108    @Override
109    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
110    public <Avoid accept(final VoidVisitor<Avfinal A arg) {
111        v.visit(this, arg);
112    }
113
114    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
115    public Statement getBody() {
116        return body;
117    }
118
119    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
120    public Optional<ExpressiongetCompare() {
121        return Optional.ofNullable(compare);
122    }
123
124    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
125    public NodeList<ExpressiongetInitialization() {
126        return initialization;
127    }
128
129    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
130    public NodeList<ExpressiongetUpdate() {
131        return update;
132    }
133
134    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
135    public ForStmt setBody(final Statement body) {
136        assertNotNull(body);
137        if (body == this.body) {
138            return (ForStmt) this;
139        }
140        notifyPropertyChange(ObservableProperty.BODY, this.bodybody);
141        if (this.body != null)
142            this.body.setParentNode(null);
143        this.body = body;
144        setAsParentNodeOf(body);
145        return this;
146    }
147
148    /**
149     * Sets the compare
150     *
151     * @param compare the compare, can be null
152     * @return this, the ForStmt
153     */
154    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
155    public ForStmt setCompare(final Expression compare) {
156        if (compare == this.compare) {
157            return (ForStmt) this;
158        }
159        notifyPropertyChange(ObservableProperty.COMPARE, this.comparecompare);
160        if (this.compare != null)
161            this.compare.setParentNode(null);
162        this.compare = compare;
163        setAsParentNodeOf(compare);
164        return this;
165    }
166
167    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
168    public ForStmt setInitialization(final NodeList<Expressioninitialization) {
169        assertNotNull(initialization);
170        if (initialization == this.initialization) {
171            return (ForStmt) this;
172        }
173        notifyPropertyChange(ObservableProperty.INITIALIZATION, this.initializationinitialization);
174        if (this.initialization != null)
175            this.initialization.setParentNode(null);
176        this.initialization = initialization;
177        setAsParentNodeOf(initialization);
178        return this;
179    }
180
181    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
182    public ForStmt setUpdate(final NodeList<Expressionupdate) {
183        assertNotNull(update);
184        if (update == this.update) {
185            return (ForStmt) this;
186        }
187        notifyPropertyChange(ObservableProperty.UPDATE, this.updateupdate);
188        if (this.update != null)
189            this.update.setParentNode(null);
190        this.update = update;
191        setAsParentNodeOf(update);
192        return this;
193    }
194
195    @Override
196    @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
197    public boolean remove(Node node) {
198        if (node == null)
199            return false;
200        if (compare != null) {
201            if (node == compare) {
202                removeCompare();
203                return true;
204            }
205        }
206        for (int i = 0i < initialization.size(); i++) {
207            if (initialization.get(i) == node) {
208                initialization.remove(i);
209                return true;
210            }
211        }
212        for (int i = 0i < update.size(); i++) {
213            if (update.get(i) == node) {
214                update.remove(i);
215                return true;
216            }
217        }
218        return super.remove(node);
219    }
220
221    @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
222    public ForStmt removeCompare() {
223        return setCompare((Expressionnull);
224    }
225
226    @Override
227    @Generated("com.github.javaparser.generator.core.node.CloneGenerator")
228    public ForStmt clone() {
229        return (ForStmtaccept(new CloneVisitor(), null);
230    }
231
232    @Override
233    @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
234    public ForStmtMetaModel getMetaModel() {
235        return JavaParserMetaModel.forStmtMetaModel;
236    }
237
238    @Override
239    @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
240    public boolean replace(Node nodeNode replacementNode) {
241        if (node == null)
242            return false;
243        if (node == body) {
244            setBody((StatementreplacementNode);
245            return true;
246        }
247        if (compare != null) {
248            if (node == compare) {
249                setCompare((ExpressionreplacementNode);
250                return true;
251            }
252        }
253        for (int i = 0i < initialization.size(); i++) {
254            if (initialization.get(i) == node) {
255                initialization.set(i, (ExpressionreplacementNode);
256                return true;
257            }
258        }
259        for (int i = 0i < update.size(); i++) {
260            if (update.get(i) == node) {
261                update.set(i, (ExpressionreplacementNode);
262                return true;
263            }
264        }
265        return super.replace(nodereplacementNode);
266    }
267
268    @Override
269    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
270    public boolean isForStmt() {
271        return true;
272    }
273
274    @Override
275    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
276    public ForStmt asForStmt() {
277        return this;
278    }
279
280    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
281    public void ifForStmt(Consumer<ForStmtaction) {
282        action.accept(this);
283    }
284
285    @Override
286    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
287    public Optional<ForStmttoForStmt() {
288        return Optional.of(this);
289    }
290}
291
MembersX
ForStmt:ForStmt
ForStmt:getInitialization
ForStmt:getMetaModel
ForStmt:getCompare
ForStmt:compare
ForStmt:setBody
ForStmt:removeCompare
ForStmt:setUpdate
ForStmt:getUpdate
ForStmt:toForStmt
ForStmt:replace
ForStmt:update
ForStmt:isForStmt
ForStmt:setCompare
ForStmt:setInitialization
ForStmt:clone
ForStmt:getBody
ForStmt:asForStmt
ForStmt:body
ForStmt:ifForStmt
ForStmt:accept
ForStmt:initialization
ForStmt:remove
Members
X