JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/stmt/IfStmt.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.ast.AllFieldsConstructor;
24import com.github.javaparser.ast.expr.BooleanLiteralExpr;
25import com.github.javaparser.ast.expr.Expression;
26import com.github.javaparser.ast.nodeTypes.NodeWithCondition;
27import com.github.javaparser.ast.observer.ObservableProperty;
28import com.github.javaparser.ast.visitor.GenericVisitor;
29import com.github.javaparser.ast.visitor.VoidVisitor;
30import java.util.Optional;
31import static com.github.javaparser.utils.Utils.assertNotNull;
32import com.github.javaparser.ast.Node;
33import com.github.javaparser.ast.visitor.CloneVisitor;
34import com.github.javaparser.metamodel.DerivedProperty;
35import com.github.javaparser.metamodel.IfStmtMetaModel;
36import com.github.javaparser.metamodel.JavaParserMetaModel;
37import com.github.javaparser.TokenRange;
38import com.github.javaparser.metamodel.OptionalProperty;
39import java.util.function.Consumer;
40import com.github.javaparser.ast.Generated;
41
42/**
43 * An if-then-else statement. The else is optional.
44 * <br>In {@code if(a==5) hurray() else boo();} the condition is a==5,
45 * hurray() is the thenStmt, and boo() is the elseStmt.
46 *
47 * @author Julio Vilmar Gesser
48 */
49public class IfStmt extends Statement implements NodeWithCondition<IfStmt> {
50
51    private Expression condition;
52
53    private Statement thenStmt;
54
55    @OptionalProperty
56    private Statement elseStmt;
57
58    public IfStmt() {
59        this(null, new BooleanLiteralExpr(), new ReturnStmt(), null);
60    }
61
62    @AllFieldsConstructor
63    public IfStmt(final Expression conditionfinal Statement thenStmtfinal Statement elseStmt) {
64        this(nullconditionthenStmtelseStmt);
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 IfStmt(TokenRange tokenRangeExpression conditionStatement thenStmtStatement elseStmt) {
72        super(tokenRange);
73        setCondition(condition);
74        setThenStmt(thenStmt);
75        setElseStmt(elseStmt);
76        customInitialization();
77    }
78
79    @Override
80    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
81    public <RAR accept(final GenericVisitor<RAvfinal A arg) {
82        return v.visit(this, arg);
83    }
84
85    @Override
86    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
87    public <Avoid accept(final VoidVisitor<Avfinal A arg) {
88        v.visit(this, arg);
89    }
90
91    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
92    public Expression getCondition() {
93        return condition;
94    }
95
96    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
97    public Optional<StatementgetElseStmt() {
98        return Optional.ofNullable(elseStmt);
99    }
100
101    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
102    public Statement getThenStmt() {
103        return thenStmt;
104    }
105
106    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
107    public IfStmt setCondition(final Expression condition) {
108        assertNotNull(condition);
109        if (condition == this.condition) {
110            return (IfStmt) this;
111        }
112        notifyPropertyChange(ObservableProperty.CONDITION, this.conditioncondition);
113        if (this.condition != null)
114            this.condition.setParentNode(null);
115        this.condition = condition;
116        setAsParentNodeOf(condition);
117        return this;
118    }
119
120    /**
121     * Sets the elseStmt
122     *
123     * @param elseStmt the elseStmt, can be null
124     * @return this, the IfStmt
125     */
126    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
127    public IfStmt setElseStmt(final Statement elseStmt) {
128        if (elseStmt == this.elseStmt) {
129            return (IfStmt) this;
130        }
131        notifyPropertyChange(ObservableProperty.ELSE_STMT, this.elseStmtelseStmt);
132        if (this.elseStmt != null)
133            this.elseStmt.setParentNode(null);
134        this.elseStmt = elseStmt;
135        setAsParentNodeOf(elseStmt);
136        return this;
137    }
138
139    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
140    public IfStmt setThenStmt(final Statement thenStmt) {
141        assertNotNull(thenStmt);
142        if (thenStmt == this.thenStmt) {
143            return (IfStmt) this;
144        }
145        notifyPropertyChange(ObservableProperty.THEN_STMT, this.thenStmtthenStmt);
146        if (this.thenStmt != null)
147            this.thenStmt.setParentNode(null);
148        this.thenStmt = thenStmt;
149        setAsParentNodeOf(thenStmt);
150        return this;
151    }
152
153    @Override
154    @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
155    public boolean remove(Node node) {
156        if (node == null)
157            return false;
158        if (elseStmt != null) {
159            if (node == elseStmt) {
160                removeElseStmt();
161                return true;
162            }
163        }
164        return super.remove(node);
165    }
166
167    @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
168    public IfStmt removeElseStmt() {
169        return setElseStmt((Statementnull);
170    }
171
172    /**
173     * This method returns true if the then branch (which should be always present) is a block statement.
174     */
175    @DerivedProperty
176    public boolean hasThenBlock() {
177        return thenStmt instanceof BlockStmt;
178    }
179
180    /**
181     * This method returns true if the If Statement has an else branch and that branch is a block statement.
182     */
183    @DerivedProperty
184    public boolean hasElseBlock() {
185        return elseStmt instanceof BlockStmt;
186    }
187
188    /**
189     * This method returns true if the If Statement has an else branch and that branch is another If Statement.
190     */
191    @DerivedProperty
192    public boolean hasCascadingIfStmt() {
193        return elseStmt instanceof IfStmt;
194    }
195
196    /**
197     * This method returns true if the If Statement has an else branch.
198     */
199    @DerivedProperty
200    public boolean hasElseBranch() {
201        return elseStmt != null;
202    }
203
204    @Override
205    @Generated("com.github.javaparser.generator.core.node.CloneGenerator")
206    public IfStmt clone() {
207        return (IfStmtaccept(new CloneVisitor(), null);
208    }
209
210    @Override
211    @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
212    public IfStmtMetaModel getMetaModel() {
213        return JavaParserMetaModel.ifStmtMetaModel;
214    }
215
216    @Override
217    @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
218    public boolean replace(Node nodeNode replacementNode) {
219        if (node == null)
220            return false;
221        if (node == condition) {
222            setCondition((ExpressionreplacementNode);
223            return true;
224        }
225        if (elseStmt != null) {
226            if (node == elseStmt) {
227                setElseStmt((StatementreplacementNode);
228                return true;
229            }
230        }
231        if (node == thenStmt) {
232            setThenStmt((StatementreplacementNode);
233            return true;
234        }
235        return super.replace(nodereplacementNode);
236    }
237
238    @Override
239    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
240    public boolean isIfStmt() {
241        return true;
242    }
243
244    @Override
245    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
246    public IfStmt asIfStmt() {
247        return this;
248    }
249
250    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
251    public void ifIfStmt(Consumer<IfStmtaction) {
252        action.accept(this);
253    }
254
255    @Override
256    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
257    public Optional<IfStmttoIfStmt() {
258        return Optional.of(this);
259    }
260}
261
MembersX
IfStmt:getThenStmt
IfStmt:replace
IfStmt:condition
IfStmt:hasElseBranch
IfStmt:accept
IfStmt:IfStmt
IfStmt:setCondition
IfStmt:hasCascadingIfStmt
IfStmt:getElseStmt
IfStmt:elseStmt
IfStmt:setElseStmt
IfStmt:hasElseBlock
IfStmt:clone
IfStmt:isIfStmt
IfStmt:toIfStmt
IfStmt:getCondition
IfStmt:remove
IfStmt:thenStmt
IfStmt:getMetaModel
IfStmt:asIfStmt
IfStmt:setThenStmt
IfStmt:removeElseStmt
IfStmt:hasThenBlock
IfStmt:ifIfStmt
Members
X