1 /* 2 * Copyright (C) 2007-2010 Júlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 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.Node; 26 import com.github.javaparser.ast.NodeList; 27 import com.github.javaparser.ast.expr.BooleanLiteralExpr; 28 import com.github.javaparser.ast.expr.Expression; 29 import com.github.javaparser.ast.nodeTypes.NodeWithBody; 30 import com.github.javaparser.ast.observer.ObservableProperty; 31 import com.github.javaparser.ast.visitor.CloneVisitor; 32 import com.github.javaparser.ast.visitor.GenericVisitor; 33 import com.github.javaparser.ast.visitor.VoidVisitor; 34 import com.github.javaparser.metamodel.ForStmtMetaModel; 35 import com.github.javaparser.metamodel.JavaParserMetaModel; 36 import com.github.javaparser.metamodel.OptionalProperty; 37 import java.util.Optional; 38 import java.util.function.Consumer; 39 import static com.github.javaparser.utils.Utils.assertNotNull; 40 import 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();</code></li> 47 * <li><code>for(a=3, b=5; a<99; a++) { hello(); }</code> </li> 48 * <li><code>for(a(),b();;) hello();</code> </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 */ 69 public class ForStmt extends Statement implements NodeWithBody<ForStmt> { 70 71 private NodeList<Expression> initialization; 72 73 @OptionalProperty 74 private Expression compare; 75 76 private NodeList<Expression> update; 77 78 private Statement body; 79 ForStmt()80 public ForStmt() { 81 this(null, new NodeList<>(), new BooleanLiteralExpr(), new NodeList<>(), new ReturnStmt()); 82 } 83 84 @AllFieldsConstructor ForStmt(final NodeList<Expression> initialization, final Expression compare, final NodeList<Expression> update, final Statement body)85 public ForStmt(final NodeList<Expression> initialization, final Expression compare, final NodeList<Expression> update, final Statement body) { 86 this(null, initialization, compare, update, body); 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") ForStmt(TokenRange tokenRange, NodeList<Expression> initialization, Expression compare, NodeList<Expression> update, Statement body)93 public ForStmt(TokenRange tokenRange, NodeList<Expression> initialization, Expression compare, NodeList<Expression> update, Statement 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") accept(final GenericVisitor<R, A> v, final A arg)104 public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { 105 return v.visit(this, arg); 106 } 107 108 @Override 109 @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") accept(final VoidVisitor<A> v, final A arg)110 public <A> void accept(final VoidVisitor<A> v, final A arg) { 111 v.visit(this, arg); 112 } 113 114 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") getBody()115 public Statement getBody() { 116 return body; 117 } 118 119 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") getCompare()120 public Optional<Expression> getCompare() { 121 return Optional.ofNullable(compare); 122 } 123 124 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") getInitialization()125 public NodeList<Expression> getInitialization() { 126 return initialization; 127 } 128 129 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") getUpdate()130 public NodeList<Expression> getUpdate() { 131 return update; 132 } 133 134 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") setBody(final Statement body)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.body, body); 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") setCompare(final Expression compare)155 public ForStmt setCompare(final Expression compare) { 156 if (compare == this.compare) { 157 return (ForStmt) this; 158 } 159 notifyPropertyChange(ObservableProperty.COMPARE, this.compare, compare); 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") setInitialization(final NodeList<Expression> initialization)168 public ForStmt setInitialization(final NodeList<Expression> initialization) { 169 assertNotNull(initialization); 170 if (initialization == this.initialization) { 171 return (ForStmt) this; 172 } 173 notifyPropertyChange(ObservableProperty.INITIALIZATION, this.initialization, initialization); 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") setUpdate(final NodeList<Expression> update)182 public ForStmt setUpdate(final NodeList<Expression> update) { 183 assertNotNull(update); 184 if (update == this.update) { 185 return (ForStmt) this; 186 } 187 notifyPropertyChange(ObservableProperty.UPDATE, this.update, update); 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") remove(Node node)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 = 0; i < initialization.size(); i++) { 207 if (initialization.get(i) == node) { 208 initialization.remove(i); 209 return true; 210 } 211 } 212 for (int i = 0; i < 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") removeCompare()222 public ForStmt removeCompare() { 223 return setCompare((Expression) null); 224 } 225 226 @Override 227 @Generated("com.github.javaparser.generator.core.node.CloneGenerator") clone()228 public ForStmt clone() { 229 return (ForStmt) accept(new CloneVisitor(), null); 230 } 231 232 @Override 233 @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") getMetaModel()234 public ForStmtMetaModel getMetaModel() { 235 return JavaParserMetaModel.forStmtMetaModel; 236 } 237 238 @Override 239 @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") replace(Node node, Node replacementNode)240 public boolean replace(Node node, Node replacementNode) { 241 if (node == null) 242 return false; 243 if (node == body) { 244 setBody((Statement) replacementNode); 245 return true; 246 } 247 if (compare != null) { 248 if (node == compare) { 249 setCompare((Expression) replacementNode); 250 return true; 251 } 252 } 253 for (int i = 0; i < initialization.size(); i++) { 254 if (initialization.get(i) == node) { 255 initialization.set(i, (Expression) replacementNode); 256 return true; 257 } 258 } 259 for (int i = 0; i < update.size(); i++) { 260 if (update.get(i) == node) { 261 update.set(i, (Expression) replacementNode); 262 return true; 263 } 264 } 265 return super.replace(node, replacementNode); 266 } 267 268 @Override 269 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") isForStmt()270 public boolean isForStmt() { 271 return true; 272 } 273 274 @Override 275 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") asForStmt()276 public ForStmt asForStmt() { 277 return this; 278 } 279 280 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") ifForStmt(Consumer<ForStmt> action)281 public void ifForStmt(Consumer<ForStmt> action) { 282 action.accept(this); 283 } 284 285 @Override 286 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") toForStmt()287 public Optional<ForStmt> toForStmt() { 288 return Optional.of(this); 289 } 290 } 291