| 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.expr.BooleanLiteralExpr; |
| 25 | import com.github.javaparser.ast.expr.Expression; |
| 26 | import com.github.javaparser.ast.nodeTypes.NodeWithBody; |
| 27 | import com.github.javaparser.ast.nodeTypes.NodeWithCondition; |
| 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.DoStmtMetaModel; |
| 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 | * A do-while. |
| 43 | * <br>{@code do { ... } while ( a==0 );} |
| 44 | * |
| 45 | * @author Julio Vilmar Gesser |
| 46 | */ |
| 47 | public class DoStmt extends Statement implements NodeWithBody<DoStmt>, NodeWithCondition<DoStmt> { |
| 48 | |
| 49 | private Statement body; |
| 50 | |
| 51 | private Expression condition; |
| 52 | |
| 53 | public DoStmt() { |
| 54 | this(null, new ReturnStmt(), new BooleanLiteralExpr()); |
| 55 | } |
| 56 | |
| 57 | @AllFieldsConstructor |
| 58 | public DoStmt(final Statement body, final Expression condition) { |
| 59 | this(null, body, condition); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * This constructor is used by the parser and is considered private. |
| 64 | */ |
| 65 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 66 | public DoStmt(TokenRange tokenRange, Statement body, Expression condition) { |
| 67 | super(tokenRange); |
| 68 | setBody(body); |
| 69 | setCondition(condition); |
| 70 | customInitialization(); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 75 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 76 | return v.visit(this, arg); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 81 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 82 | v.visit(this, arg); |
| 83 | } |
| 84 | |
| 85 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 86 | public Statement getBody() { |
| 87 | return body; |
| 88 | } |
| 89 | |
| 90 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 91 | public Expression getCondition() { |
| 92 | return condition; |
| 93 | } |
| 94 | |
| 95 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 96 | public DoStmt setBody(final Statement body) { |
| 97 | assertNotNull(body); |
| 98 | if (body == this.body) { |
| 99 | return (DoStmt) this; |
| 100 | } |
| 101 | notifyPropertyChange(ObservableProperty.BODY, this.body, body); |
| 102 | if (this.body != null) |
| 103 | this.body.setParentNode(null); |
| 104 | this.body = body; |
| 105 | setAsParentNodeOf(body); |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 110 | public DoStmt setCondition(final Expression condition) { |
| 111 | assertNotNull(condition); |
| 112 | if (condition == this.condition) { |
| 113 | return (DoStmt) this; |
| 114 | } |
| 115 | notifyPropertyChange(ObservableProperty.CONDITION, this.condition, condition); |
| 116 | if (this.condition != null) |
| 117 | this.condition.setParentNode(null); |
| 118 | this.condition = condition; |
| 119 | setAsParentNodeOf(condition); |
| 120 | return this; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 125 | public boolean remove(Node node) { |
| 126 | if (node == null) |
| 127 | return false; |
| 128 | return super.remove(node); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 133 | public DoStmt clone() { |
| 134 | return (DoStmt) accept(new CloneVisitor(), null); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 139 | public DoStmtMetaModel getMetaModel() { |
| 140 | return JavaParserMetaModel.doStmtMetaModel; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 145 | public boolean replace(Node node, Node replacementNode) { |
| 146 | if (node == null) |
| 147 | return false; |
| 148 | if (node == body) { |
| 149 | setBody((Statement) replacementNode); |
| 150 | return true; |
| 151 | } |
| 152 | if (node == condition) { |
| 153 | setCondition((Expression) replacementNode); |
| 154 | return true; |
| 155 | } |
| 156 | return super.replace(node, replacementNode); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 161 | public boolean isDoStmt() { |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 167 | public DoStmt asDoStmt() { |
| 168 | return this; |
| 169 | } |
| 170 | |
| 171 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 172 | public void ifDoStmt(Consumer<DoStmt> action) { |
| 173 | action.accept(this); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 178 | public Optional<DoStmt> toDoStmt() { |
| 179 | return Optional.of(this); |
| 180 | } |
| 181 | } |
| 182 |
Members