diff options
Diffstat (limited to 'javaparser-core/src/main/java/com/github/javaparser/ast/stmt')
23 files changed, 4771 insertions, 0 deletions
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/AssertStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/AssertStmt.java new file mode 100644 index 000000000..a6eb59fe3 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/AssertStmt.java @@ -0,0 +1,202 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.expr.BooleanLiteralExpr; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import java.util.Optional; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.AssertStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import com.github.javaparser.metamodel.OptionalProperty; +import java.util.function.Consumer; + +/** + * A usage of the keyword "assert" + * <br/>In <code>assert dead : "Wasn't expecting to be dead here";</code> the check is "dead" and the message is the string. + * @author Julio Vilmar Gesser + */ +public final class AssertStmt extends Statement { + + private Expression check; + + @OptionalProperty + private Expression message; + + public AssertStmt() { + this(null, new BooleanLiteralExpr(), null); + } + + public AssertStmt(final Expression check) { + this(null, check, null); + } + + @AllFieldsConstructor + public AssertStmt(final Expression check, final Expression message) { + this(null, check, message); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public AssertStmt(TokenRange tokenRange, Expression check, Expression message) { + super(tokenRange); + setCheck(check); + setMessage(message); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Expression getCheck() { + return check; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Optional<Expression> getMessage() { + return Optional.ofNullable(message); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public AssertStmt setCheck(final Expression check) { + assertNotNull(check); + if (check == this.check) { + return (AssertStmt) this; + } + notifyPropertyChange(ObservableProperty.CHECK, this.check, check); + if (this.check != null) + this.check.setParentNode(null); + this.check = check; + setAsParentNodeOf(check); + return this; + } + + /** + * Sets the message + * + * @param message the message, can be null + * @return this, the AssertStmt + */ + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public AssertStmt setMessage(final Expression message) { + if (message == this.message) { + return (AssertStmt) this; + } + notifyPropertyChange(ObservableProperty.MESSAGE, this.message, message); + if (this.message != null) + this.message.setParentNode(null); + this.message = message; + setAsParentNodeOf(message); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + if (message != null) { + if (node == message) { + removeMessage(); + return true; + } + } + return super.remove(node); + } + + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public AssertStmt removeMessage() { + return setMessage((Expression) null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public AssertStmt clone() { + return (AssertStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public AssertStmtMetaModel getMetaModel() { + return JavaParserMetaModel.assertStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (node == check) { + setCheck((Expression) replacementNode); + return true; + } + if (message != null) { + if (node == message) { + setMessage((Expression) replacementNode); + return true; + } + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isAssertStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public AssertStmt asAssertStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifAssertStmt(Consumer<AssertStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<AssertStmt> toAssertStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/BlockStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/BlockStmt.java new file mode 100644 index 000000000..3e25b9647 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/BlockStmt.java @@ -0,0 +1,162 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.nodeTypes.NodeWithStatements; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import java.util.Arrays; +import java.util.List; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.BlockStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import java.util.function.Consumer; +import java.util.Optional; + +/** + * Statements in between { and }. + * + * @author Julio Vilmar Gesser + */ +public final class BlockStmt extends Statement implements NodeWithStatements<BlockStmt> { + + private NodeList<Statement> statements; + + public BlockStmt() { + this(null, new NodeList<>()); + } + + @AllFieldsConstructor + public BlockStmt(final NodeList<Statement> statements) { + this(null, statements); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public BlockStmt(TokenRange tokenRange, NodeList<Statement> statements) { + super(tokenRange); + setStatements(statements); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public NodeList<Statement> getStatements() { + return statements; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public BlockStmt setStatements(final NodeList<Statement> statements) { + assertNotNull(statements); + if (statements == this.statements) { + return (BlockStmt) this; + } + notifyPropertyChange(ObservableProperty.STATEMENTS, this.statements, statements); + if (this.statements != null) + this.statements.setParentNode(null); + this.statements = statements; + setAsParentNodeOf(statements); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + for (int i = 0; i < statements.size(); i++) { + if (statements.get(i) == node) { + statements.remove(i); + return true; + } + } + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public BlockStmt clone() { + return (BlockStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public BlockStmtMetaModel getMetaModel() { + return JavaParserMetaModel.blockStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + for (int i = 0; i < statements.size(); i++) { + if (statements.get(i) == node) { + statements.set(i, (Statement) replacementNode); + return true; + } + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isBlockStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public BlockStmt asBlockStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifBlockStmt(Consumer<BlockStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<BlockStmt> toBlockStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/BreakStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/BreakStmt.java new file mode 100644 index 000000000..4a0b411c4 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/BreakStmt.java @@ -0,0 +1,175 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.expr.SimpleName; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import java.util.Optional; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.BreakStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import com.github.javaparser.metamodel.OptionalProperty; +import java.util.function.Consumer; + +/** + * A usage of the break keyword. + * <br/>In <code>break abc;</code> the label is abc. + * + * @author Julio Vilmar Gesser + */ +public final class BreakStmt extends Statement { + + @OptionalProperty + private SimpleName label; + + public BreakStmt() { + this(null, new SimpleName()); + } + + public BreakStmt(final String label) { + this(null, new SimpleName(label)); + } + + @AllFieldsConstructor + public BreakStmt(final SimpleName label) { + this(null, label); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public BreakStmt(TokenRange tokenRange, SimpleName label) { + super(tokenRange); + setLabel(label); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Optional<SimpleName> getLabel() { + return Optional.ofNullable(label); + } + + /** + * Sets the label + * + * @param label the label, can be null + * @return this, the BreakStmt + */ + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public BreakStmt setLabel(final SimpleName label) { + if (label == this.label) { + return (BreakStmt) this; + } + notifyPropertyChange(ObservableProperty.LABEL, this.label, label); + if (this.label != null) + this.label.setParentNode(null); + this.label = label; + setAsParentNodeOf(label); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + if (label != null) { + if (node == label) { + removeLabel(); + return true; + } + } + return super.remove(node); + } + + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public BreakStmt removeLabel() { + return setLabel((SimpleName) null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public BreakStmt clone() { + return (BreakStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public BreakStmtMetaModel getMetaModel() { + return JavaParserMetaModel.breakStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (label != null) { + if (node == label) { + setLabel((SimpleName) replacementNode); + return true; + } + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isBreakStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public BreakStmt asBreakStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifBreakStmt(Consumer<BreakStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<BreakStmt> toBreakStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/CatchClause.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/CatchClause.java new file mode 100644 index 000000000..9eaa30efe --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/CatchClause.java @@ -0,0 +1,169 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.Modifier; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.body.Parameter; +import com.github.javaparser.ast.expr.AnnotationExpr; +import com.github.javaparser.ast.expr.SimpleName; +import com.github.javaparser.ast.nodeTypes.NodeWithBlockStmt; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.type.ClassOrInterfaceType; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import java.util.EnumSet; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.CatchClauseMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; + +/** + * The catch part of a try-catch-finally. <br/>In <code>try { ... } catch (Exception e) { ... }</code> the CatchClause + * is <code>catch (Exception e) { ... }</code>. Exception e is the parameter. The { ... } is the body. + * + * @author Julio Vilmar Gesser + */ +public final class CatchClause extends Node implements NodeWithBlockStmt<CatchClause> { + + private Parameter parameter; + + private BlockStmt body; + + public CatchClause() { + this(null, new Parameter(), new BlockStmt()); + } + + public CatchClause(final EnumSet<Modifier> exceptModifier, final NodeList<AnnotationExpr> exceptAnnotations, final ClassOrInterfaceType exceptType, final SimpleName exceptName, final BlockStmt body) { + this(null, new Parameter(null, exceptModifier, exceptAnnotations, exceptType, false, new NodeList<>(), exceptName), body); + } + + @AllFieldsConstructor + public CatchClause(final Parameter parameter, final BlockStmt body) { + this(null, parameter, body); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public CatchClause(TokenRange tokenRange, Parameter parameter, BlockStmt body) { + super(tokenRange); + setParameter(parameter); + setBody(body); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + /** + * Note that the type of the Parameter can be a UnionType. In this case, any annotations found at the start of the + * catch(@X A a |...) are found directly in the Parameter. Annotations that are on the second or later type - + * catch(A a | @X B b ...) are found on those types. + */ + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Parameter getParameter() { + return parameter; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public CatchClause setParameter(final Parameter parameter) { + assertNotNull(parameter); + if (parameter == this.parameter) { + return (CatchClause) this; + } + notifyPropertyChange(ObservableProperty.PARAMETER, this.parameter, parameter); + if (this.parameter != null) + this.parameter.setParentNode(null); + this.parameter = parameter; + setAsParentNodeOf(parameter); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public BlockStmt getBody() { + return body; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public CatchClause setBody(final BlockStmt body) { + assertNotNull(body); + if (body == this.body) { + return (CatchClause) this; + } + notifyPropertyChange(ObservableProperty.BODY, this.body, body); + if (this.body != null) + this.body.setParentNode(null); + this.body = body; + setAsParentNodeOf(body); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public CatchClause clone() { + return (CatchClause) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public CatchClauseMetaModel getMetaModel() { + return JavaParserMetaModel.catchClauseMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (node == body) { + setBody((BlockStmt) replacementNode); + return true; + } + if (node == parameter) { + setParameter((Parameter) replacementNode); + return true; + } + return super.replace(node, replacementNode); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ContinueStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ContinueStmt.java new file mode 100644 index 000000000..b82995084 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ContinueStmt.java @@ -0,0 +1,177 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.expr.SimpleName; +import com.github.javaparser.ast.nodeTypes.NodeWithOptionalLabel; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import java.util.Optional; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.ContinueStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import com.github.javaparser.metamodel.OptionalProperty; +import java.util.function.Consumer; + +/** + * A continue statement with an optional label; + * <br/><code>continue brains;</code> + * <br/><code>continue;</code> + * + * @author Julio Vilmar Gesser + */ +public final class ContinueStmt extends Statement implements NodeWithOptionalLabel<ContinueStmt> { + + @OptionalProperty + private SimpleName label; + + public ContinueStmt() { + this(null, null); + } + + public ContinueStmt(final String label) { + this(null, new SimpleName(label)); + } + + @AllFieldsConstructor + public ContinueStmt(final SimpleName label) { + this(null, label); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public ContinueStmt(TokenRange tokenRange, SimpleName label) { + super(tokenRange); + setLabel(label); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Optional<SimpleName> getLabel() { + return Optional.ofNullable(label); + } + + /** + * Sets the label + * + * @param label the label, can be null + * @return this, the ContinueStmt + */ + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ContinueStmt setLabel(final SimpleName label) { + if (label == this.label) { + return (ContinueStmt) this; + } + notifyPropertyChange(ObservableProperty.LABEL, this.label, label); + if (this.label != null) + this.label.setParentNode(null); + this.label = label; + setAsParentNodeOf(label); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + if (label != null) { + if (node == label) { + removeLabel(); + return true; + } + } + return super.remove(node); + } + + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public ContinueStmt removeLabel() { + return setLabel((SimpleName) null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public ContinueStmt clone() { + return (ContinueStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public ContinueStmtMetaModel getMetaModel() { + return JavaParserMetaModel.continueStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (label != null) { + if (node == label) { + setLabel((SimpleName) replacementNode); + return true; + } + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isContinueStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ContinueStmt asContinueStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifContinueStmt(Consumer<ContinueStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ContinueStmt> toContinueStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/DoStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/DoStmt.java new file mode 100644 index 000000000..7c161ff8d --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/DoStmt.java @@ -0,0 +1,181 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.expr.BooleanLiteralExpr; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.nodeTypes.NodeWithBody; +import com.github.javaparser.ast.nodeTypes.NodeWithCondition; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.DoStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import java.util.function.Consumer; +import java.util.Optional; + +/** + * A do-while. + * <br/><code>do { ... } while ( a==0 );</code> + * + * @author Julio Vilmar Gesser + */ +public final class DoStmt extends Statement implements NodeWithBody<DoStmt>, NodeWithCondition<DoStmt> { + + private Statement body; + + private Expression condition; + + public DoStmt() { + this(null, new ReturnStmt(), new BooleanLiteralExpr()); + } + + @AllFieldsConstructor + public DoStmt(final Statement body, final Expression condition) { + this(null, body, condition); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public DoStmt(TokenRange tokenRange, Statement body, Expression condition) { + super(tokenRange); + setBody(body); + setCondition(condition); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Statement getBody() { + return body; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Expression getCondition() { + return condition; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public DoStmt setBody(final Statement body) { + assertNotNull(body); + if (body == this.body) { + return (DoStmt) this; + } + notifyPropertyChange(ObservableProperty.BODY, this.body, body); + if (this.body != null) + this.body.setParentNode(null); + this.body = body; + setAsParentNodeOf(body); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public DoStmt setCondition(final Expression condition) { + assertNotNull(condition); + if (condition == this.condition) { + return (DoStmt) this; + } + notifyPropertyChange(ObservableProperty.CONDITION, this.condition, condition); + if (this.condition != null) + this.condition.setParentNode(null); + this.condition = condition; + setAsParentNodeOf(condition); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public DoStmt clone() { + return (DoStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public DoStmtMetaModel getMetaModel() { + return JavaParserMetaModel.doStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (node == body) { + setBody((Statement) replacementNode); + return true; + } + if (node == condition) { + setCondition((Expression) replacementNode); + return true; + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isDoStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public DoStmt asDoStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifDoStmt(Consumer<DoStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<DoStmt> toDoStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/EmptyStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/EmptyStmt.java new file mode 100644 index 000000000..d73ede61b --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/EmptyStmt.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.TokenRange; +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import com.github.javaparser.metamodel.EmptyStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import java.util.function.Consumer; +import java.util.Optional; + +/** + * An empty statement is a ";" where a statement is expected. + * @author Julio Vilmar Gesser + */ +public final class EmptyStmt extends Statement { + + @AllFieldsConstructor + public EmptyStmt() { + this(null); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public EmptyStmt(TokenRange tokenRange) { + super(tokenRange); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public EmptyStmt clone() { + return (EmptyStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public EmptyStmtMetaModel getMetaModel() { + return JavaParserMetaModel.emptyStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isEmptyStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public EmptyStmt asEmptyStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifEmptyStmt(Consumer<EmptyStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<EmptyStmt> toEmptyStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ExplicitConstructorInvocationStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ExplicitConstructorInvocationStmt.java new file mode 100644 index 000000000..2b9b9c200 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ExplicitConstructorInvocationStmt.java @@ -0,0 +1,299 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.type.Type; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.ExplicitConstructorInvocationStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import com.github.javaparser.metamodel.OptionalProperty; +import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration; +import java.util.function.Consumer; + +/** + * A call to super or this in a constructor or initializer. + * <br/><code>class X { X() { super(15); } }</code> + * <br/><code>class X { X() { this(1, 2); } }</code> + * + * @author Julio Vilmar Gesser + * @see com.github.javaparser.ast.expr.SuperExpr + * @see com.github.javaparser.ast.expr.ThisExpr + */ +public final class ExplicitConstructorInvocationStmt extends Statement implements NodeWithTypeArguments<ExplicitConstructorInvocationStmt> { + + @OptionalProperty + private NodeList<Type> typeArguments; + + private boolean isThis; + + @OptionalProperty + private Expression expression; + + private NodeList<Expression> arguments; + + public ExplicitConstructorInvocationStmt() { + this(null, new NodeList<>(), true, null, new NodeList<>()); + } + + public ExplicitConstructorInvocationStmt(final boolean isThis, final Expression expression, final NodeList<Expression> arguments) { + this(null, new NodeList<>(), isThis, expression, arguments); + } + + @AllFieldsConstructor + public ExplicitConstructorInvocationStmt(final NodeList<Type> typeArguments, final boolean isThis, final Expression expression, final NodeList<Expression> arguments) { + this(null, typeArguments, isThis, expression, arguments); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public ExplicitConstructorInvocationStmt(TokenRange tokenRange, NodeList<Type> typeArguments, boolean isThis, Expression expression, NodeList<Expression> arguments) { + super(tokenRange); + setTypeArguments(typeArguments); + setThis(isThis); + setExpression(expression); + setArguments(arguments); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public NodeList<Expression> getArguments() { + return arguments; + } + + public Expression getArgument(int i) { + return getArguments().get(i); + } + + public ExplicitConstructorInvocationStmt setArgument(int i, Expression argument) { + getArguments().set(i, argument); + return this; + } + + public ExplicitConstructorInvocationStmt addArgument(Expression argument) { + getArguments().add(argument); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Optional<Expression> getExpression() { + return Optional.ofNullable(expression); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public boolean isThis() { + return isThis; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ExplicitConstructorInvocationStmt setArguments(final NodeList<Expression> arguments) { + assertNotNull(arguments); + if (arguments == this.arguments) { + return (ExplicitConstructorInvocationStmt) this; + } + notifyPropertyChange(ObservableProperty.ARGUMENTS, this.arguments, arguments); + if (this.arguments != null) + this.arguments.setParentNode(null); + this.arguments = arguments; + setAsParentNodeOf(arguments); + return this; + } + + /** + * Sets the expression + * + * @param expression the expression, can be null + * @return this, the ExplicitConstructorInvocationStmt + */ + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ExplicitConstructorInvocationStmt setExpression(final Expression expression) { + if (expression == this.expression) { + return (ExplicitConstructorInvocationStmt) this; + } + notifyPropertyChange(ObservableProperty.EXPRESSION, this.expression, expression); + if (this.expression != null) + this.expression.setParentNode(null); + this.expression = expression; + setAsParentNodeOf(expression); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ExplicitConstructorInvocationStmt setThis(final boolean isThis) { + if (isThis == this.isThis) { + return (ExplicitConstructorInvocationStmt) this; + } + notifyPropertyChange(ObservableProperty.THIS, this.isThis, isThis); + this.isThis = isThis; + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Optional<NodeList<Type>> getTypeArguments() { + return Optional.ofNullable(typeArguments); + } + + /** + * Sets the typeArguments + * + * @param typeArguments the typeArguments, can be null + * @return this, the ExplicitConstructorInvocationStmt + */ + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ExplicitConstructorInvocationStmt setTypeArguments(final NodeList<Type> typeArguments) { + if (typeArguments == this.typeArguments) { + return (ExplicitConstructorInvocationStmt) this; + } + notifyPropertyChange(ObservableProperty.TYPE_ARGUMENTS, this.typeArguments, typeArguments); + if (this.typeArguments != null) + this.typeArguments.setParentNode(null); + this.typeArguments = typeArguments; + setAsParentNodeOf(typeArguments); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + for (int i = 0; i < arguments.size(); i++) { + if (arguments.get(i) == node) { + arguments.remove(i); + return true; + } + } + if (expression != null) { + if (node == expression) { + removeExpression(); + return true; + } + } + if (typeArguments != null) { + for (int i = 0; i < typeArguments.size(); i++) { + if (typeArguments.get(i) == node) { + typeArguments.remove(i); + return true; + } + } + } + return super.remove(node); + } + + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public ExplicitConstructorInvocationStmt removeExpression() { + return setExpression((Expression) null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public ExplicitConstructorInvocationStmt clone() { + return (ExplicitConstructorInvocationStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public ExplicitConstructorInvocationStmtMetaModel getMetaModel() { + return JavaParserMetaModel.explicitConstructorInvocationStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + for (int i = 0; i < arguments.size(); i++) { + if (arguments.get(i) == node) { + arguments.set(i, (Expression) replacementNode); + return true; + } + } + if (expression != null) { + if (node == expression) { + setExpression((Expression) replacementNode); + return true; + } + } + if (typeArguments != null) { + for (int i = 0; i < typeArguments.size(); i++) { + if (typeArguments.get(i) == node) { + typeArguments.set(i, (Type) replacementNode); + return true; + } + } + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isExplicitConstructorInvocationStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ExplicitConstructorInvocationStmt asExplicitConstructorInvocationStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifExplicitConstructorInvocationStmt(Consumer<ExplicitConstructorInvocationStmt> action) { + action.accept(this); + } + + public ResolvedConstructorDeclaration resolveInvokedConstructor() { + return getSymbolResolver().resolveDeclaration(this, ResolvedConstructorDeclaration.class); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ExplicitConstructorInvocationStmt> toExplicitConstructorInvocationStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ExpressionStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ExpressionStmt.java new file mode 100644 index 000000000..483c45dee --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ExpressionStmt.java @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.expr.BooleanLiteralExpr; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.nodeTypes.NodeWithExpression; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.ExpressionStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import java.util.function.Consumer; +import java.util.Optional; + +/** + * Used to wrap an expression so that it can take the place of a statement. + * + * @author Julio Vilmar Gesser + */ +public final class ExpressionStmt extends Statement implements NodeWithExpression<ExpressionStmt> { + + private Expression expression; + + public ExpressionStmt() { + this(null, new BooleanLiteralExpr()); + } + + @AllFieldsConstructor + public ExpressionStmt(final Expression expression) { + this(null, expression); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public ExpressionStmt(TokenRange tokenRange, Expression expression) { + super(tokenRange); + setExpression(expression); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Expression getExpression() { + return expression; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ExpressionStmt setExpression(final Expression expression) { + assertNotNull(expression); + if (expression == this.expression) { + return (ExpressionStmt) this; + } + notifyPropertyChange(ObservableProperty.EXPRESSION, this.expression, expression); + if (this.expression != null) + this.expression.setParentNode(null); + this.expression = expression; + setAsParentNodeOf(expression); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public ExpressionStmt clone() { + return (ExpressionStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public ExpressionStmtMetaModel getMetaModel() { + return JavaParserMetaModel.expressionStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (node == expression) { + setExpression((Expression) replacementNode); + return true; + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isExpressionStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ExpressionStmt asExpressionStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifExpressionStmt(Consumer<ExpressionStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ExpressionStmt> toExpressionStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ForStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ForStmt.java new file mode 100644 index 000000000..d288991df --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ForStmt.java @@ -0,0 +1,290 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.TokenRange; +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.expr.BooleanLiteralExpr; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.nodeTypes.NodeWithBody; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import com.github.javaparser.metamodel.ForStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import com.github.javaparser.metamodel.OptionalProperty; +import javax.annotation.Generated; +import java.util.Optional; +import java.util.function.Consumer; +import static com.github.javaparser.utils.Utils.assertNotNull; + +/** + * <h1>The classic for statement</h1> + * Examples: + * <ol> + * <li><code>for(int a=3, b=5; a<99; a++, b++) hello();</code></li> + * <li><code>for(a=3, b=5; a<99; a++) { hello(); }</code> </li> + * <li><code>for(a(),b();;) hello();</code> </li> + * </ol> + * <ul> + * <li><i>initialization</i> is a list of expressions. + * These can be any kind of expression as can be seen in example 3, + * but the common ones are a single VariableDeclarationExpr (which declares multiple variables) in example 1, + * or a list of AssignExpr's in example 2.</li> + * <li><i>compare</i> is an expression, + * in example 1 and 2 it is a BinaryExpr. + * In example 3 there is no expression, it is empty.</li> + * <li><i>update</i> is a list of expressions, + * in example 1 and 2 they are UnaryExpr's. + * In example 3 there is no expression, the list empty.</li> + * <li><i>body</i> is a statement, + * in example 1 and 3 it is an ExpressionStmt. + * in example 2 it is a BlockStmt.</li> + * </ul> + * + * @author Julio Vilmar Gesser + * @see com.github.javaparser.ast.expr.VariableDeclarationExpr + */ +public final class ForStmt extends Statement implements NodeWithBody<ForStmt> { + + private NodeList<Expression> initialization; + + @OptionalProperty + private Expression compare; + + private NodeList<Expression> update; + + private Statement body; + + public ForStmt() { + this(null, new NodeList<>(), new BooleanLiteralExpr(), new NodeList<>(), new ReturnStmt()); + } + + @AllFieldsConstructor + public ForStmt(final NodeList<Expression> initialization, final Expression compare, final NodeList<Expression> update, final Statement body) { + this(null, initialization, compare, update, body); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public ForStmt(TokenRange tokenRange, NodeList<Expression> initialization, Expression compare, NodeList<Expression> update, Statement body) { + super(tokenRange); + setInitialization(initialization); + setCompare(compare); + setUpdate(update); + setBody(body); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Statement getBody() { + return body; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Optional<Expression> getCompare() { + return Optional.ofNullable(compare); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public NodeList<Expression> getInitialization() { + return initialization; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public NodeList<Expression> getUpdate() { + return update; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ForStmt setBody(final Statement body) { + assertNotNull(body); + if (body == this.body) { + return (ForStmt) this; + } + notifyPropertyChange(ObservableProperty.BODY, this.body, body); + if (this.body != null) + this.body.setParentNode(null); + this.body = body; + setAsParentNodeOf(body); + return this; + } + + /** + * Sets the compare + * + * @param compare the compare, can be null + * @return this, the ForStmt + */ + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ForStmt setCompare(final Expression compare) { + if (compare == this.compare) { + return (ForStmt) this; + } + notifyPropertyChange(ObservableProperty.COMPARE, this.compare, compare); + if (this.compare != null) + this.compare.setParentNode(null); + this.compare = compare; + setAsParentNodeOf(compare); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ForStmt setInitialization(final NodeList<Expression> initialization) { + assertNotNull(initialization); + if (initialization == this.initialization) { + return (ForStmt) this; + } + notifyPropertyChange(ObservableProperty.INITIALIZATION, this.initialization, initialization); + if (this.initialization != null) + this.initialization.setParentNode(null); + this.initialization = initialization; + setAsParentNodeOf(initialization); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ForStmt setUpdate(final NodeList<Expression> update) { + assertNotNull(update); + if (update == this.update) { + return (ForStmt) this; + } + notifyPropertyChange(ObservableProperty.UPDATE, this.update, update); + if (this.update != null) + this.update.setParentNode(null); + this.update = update; + setAsParentNodeOf(update); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + if (compare != null) { + if (node == compare) { + removeCompare(); + return true; + } + } + for (int i = 0; i < initialization.size(); i++) { + if (initialization.get(i) == node) { + initialization.remove(i); + return true; + } + } + for (int i = 0; i < update.size(); i++) { + if (update.get(i) == node) { + update.remove(i); + return true; + } + } + return super.remove(node); + } + + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public ForStmt removeCompare() { + return setCompare((Expression) null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public ForStmt clone() { + return (ForStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public ForStmtMetaModel getMetaModel() { + return JavaParserMetaModel.forStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (node == body) { + setBody((Statement) replacementNode); + return true; + } + if (compare != null) { + if (node == compare) { + setCompare((Expression) replacementNode); + return true; + } + } + for (int i = 0; i < initialization.size(); i++) { + if (initialization.get(i) == node) { + initialization.set(i, (Expression) replacementNode); + return true; + } + } + for (int i = 0; i < update.size(); i++) { + if (update.get(i) == node) { + update.set(i, (Expression) replacementNode); + return true; + } + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isForStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ForStmt asForStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifForStmt(Consumer<ForStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ForStmt> toForStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ForeachStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ForeachStmt.java new file mode 100644 index 000000000..7a04e5bcb --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ForeachStmt.java @@ -0,0 +1,211 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.expr.NameExpr; +import com.github.javaparser.ast.expr.VariableDeclarationExpr; +import com.github.javaparser.ast.nodeTypes.NodeWithBody; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.ForeachStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import java.util.function.Consumer; +import java.util.Optional; + +/** + * A for-each statement. + * <br/><code>for(Object o: objects) { ... }</code> + * + * @author Julio Vilmar Gesser + */ +public final class ForeachStmt extends Statement implements NodeWithBody<ForeachStmt> { + + private VariableDeclarationExpr variable; + + private Expression iterable; + + private Statement body; + + public ForeachStmt() { + this(null, new VariableDeclarationExpr(), new NameExpr(), new ReturnStmt()); + } + + @AllFieldsConstructor + public ForeachStmt(final VariableDeclarationExpr variable, final Expression iterable, final Statement body) { + this(null, variable, iterable, body); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public ForeachStmt(TokenRange tokenRange, VariableDeclarationExpr variable, Expression iterable, Statement body) { + super(tokenRange); + setVariable(variable); + setIterable(iterable); + setBody(body); + customInitialization(); + } + + public ForeachStmt(VariableDeclarationExpr variable, String iterable, BlockStmt body) { + this(null, variable, new NameExpr(iterable), body); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Statement getBody() { + return body; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Expression getIterable() { + return iterable; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public VariableDeclarationExpr getVariable() { + return variable; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ForeachStmt setBody(final Statement body) { + assertNotNull(body); + if (body == this.body) { + return (ForeachStmt) this; + } + notifyPropertyChange(ObservableProperty.BODY, this.body, body); + if (this.body != null) + this.body.setParentNode(null); + this.body = body; + setAsParentNodeOf(body); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ForeachStmt setIterable(final Expression iterable) { + assertNotNull(iterable); + if (iterable == this.iterable) { + return (ForeachStmt) this; + } + notifyPropertyChange(ObservableProperty.ITERABLE, this.iterable, iterable); + if (this.iterable != null) + this.iterable.setParentNode(null); + this.iterable = iterable; + setAsParentNodeOf(iterable); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ForeachStmt setVariable(final VariableDeclarationExpr variable) { + assertNotNull(variable); + if (variable == this.variable) { + return (ForeachStmt) this; + } + notifyPropertyChange(ObservableProperty.VARIABLE, this.variable, variable); + if (this.variable != null) + this.variable.setParentNode(null); + this.variable = variable; + setAsParentNodeOf(variable); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public ForeachStmt clone() { + return (ForeachStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public ForeachStmtMetaModel getMetaModel() { + return JavaParserMetaModel.foreachStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (node == body) { + setBody((Statement) replacementNode); + return true; + } + if (node == iterable) { + setIterable((Expression) replacementNode); + return true; + } + if (node == variable) { + setVariable((VariableDeclarationExpr) replacementNode); + return true; + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isForeachStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ForeachStmt asForeachStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifForeachStmt(Consumer<ForeachStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ForeachStmt> toForeachStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/IfStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/IfStmt.java new file mode 100644 index 000000000..f873ff89a --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/IfStmt.java @@ -0,0 +1,260 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.expr.BooleanLiteralExpr; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.nodeTypes.NodeWithCondition; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import java.util.Optional; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.DerivedProperty; +import com.github.javaparser.metamodel.IfStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import com.github.javaparser.metamodel.OptionalProperty; +import java.util.function.Consumer; + +/** + * An if-then-else statement. The else is optional. + * <br/>In <code>if(a==5) hurray() else boo();</code> the condition is a==5, + * hurray() is the thenStmt, and boo() is the elseStmt. + * + * @author Julio Vilmar Gesser + */ +public final class IfStmt extends Statement implements NodeWithCondition<IfStmt> { + + private Expression condition; + + private Statement thenStmt; + + @OptionalProperty + private Statement elseStmt; + + public IfStmt() { + this(null, new BooleanLiteralExpr(), new ReturnStmt(), null); + } + + @AllFieldsConstructor + public IfStmt(final Expression condition, final Statement thenStmt, final Statement elseStmt) { + this(null, condition, thenStmt, elseStmt); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public IfStmt(TokenRange tokenRange, Expression condition, Statement thenStmt, Statement elseStmt) { + super(tokenRange); + setCondition(condition); + setThenStmt(thenStmt); + setElseStmt(elseStmt); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Expression getCondition() { + return condition; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Optional<Statement> getElseStmt() { + return Optional.ofNullable(elseStmt); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Statement getThenStmt() { + return thenStmt; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public IfStmt setCondition(final Expression condition) { + assertNotNull(condition); + if (condition == this.condition) { + return (IfStmt) this; + } + notifyPropertyChange(ObservableProperty.CONDITION, this.condition, condition); + if (this.condition != null) + this.condition.setParentNode(null); + this.condition = condition; + setAsParentNodeOf(condition); + return this; + } + + /** + * Sets the elseStmt + * + * @param elseStmt the elseStmt, can be null + * @return this, the IfStmt + */ + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public IfStmt setElseStmt(final Statement elseStmt) { + if (elseStmt == this.elseStmt) { + return (IfStmt) this; + } + notifyPropertyChange(ObservableProperty.ELSE_STMT, this.elseStmt, elseStmt); + if (this.elseStmt != null) + this.elseStmt.setParentNode(null); + this.elseStmt = elseStmt; + setAsParentNodeOf(elseStmt); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public IfStmt setThenStmt(final Statement thenStmt) { + assertNotNull(thenStmt); + if (thenStmt == this.thenStmt) { + return (IfStmt) this; + } + notifyPropertyChange(ObservableProperty.THEN_STMT, this.thenStmt, thenStmt); + if (this.thenStmt != null) + this.thenStmt.setParentNode(null); + this.thenStmt = thenStmt; + setAsParentNodeOf(thenStmt); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + if (elseStmt != null) { + if (node == elseStmt) { + removeElseStmt(); + return true; + } + } + return super.remove(node); + } + + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public IfStmt removeElseStmt() { + return setElseStmt((Statement) null); + } + + /** + * This method returns true if the then branch (which should be always present) is a block statement. + */ + @DerivedProperty + public boolean hasThenBlock() { + return thenStmt instanceof BlockStmt; + } + + /** + * This method returns true if the If Statement has an else branch and that branch is a block statement. + */ + @DerivedProperty + public boolean hasElseBlock() { + return elseStmt instanceof BlockStmt; + } + + /** + * This method returns true if the If Statement has an else branch and that branch is another If Statement. + */ + @DerivedProperty + public boolean hasCascadingIfStmt() { + return elseStmt instanceof IfStmt; + } + + /** + * This method returns true if the If Statement has an else branch. + */ + @DerivedProperty + public boolean hasElseBranch() { + return elseStmt != null; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public IfStmt clone() { + return (IfStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public IfStmtMetaModel getMetaModel() { + return JavaParserMetaModel.ifStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (node == condition) { + setCondition((Expression) replacementNode); + return true; + } + if (elseStmt != null) { + if (node == elseStmt) { + setElseStmt((Statement) replacementNode); + return true; + } + } + if (node == thenStmt) { + setThenStmt((Statement) replacementNode); + return true; + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isIfStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public IfStmt asIfStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifIfStmt(Consumer<IfStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<IfStmt> toIfStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/LabeledStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/LabeledStmt.java new file mode 100644 index 000000000..7906d0c6a --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/LabeledStmt.java @@ -0,0 +1,181 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.expr.SimpleName; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.LabeledStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import java.util.function.Consumer; +import java.util.Optional; + +/** + * A statement that is labeled, like <code>label123: println("continuing");</code> + * + * @author Julio Vilmar Gesser + */ +public final class LabeledStmt extends Statement { + + private SimpleName label; + + private Statement statement; + + public LabeledStmt() { + this(null, new SimpleName(), new ReturnStmt()); + } + + public LabeledStmt(final String label, final Statement statement) { + this(null, new SimpleName(label), statement); + } + + @AllFieldsConstructor + public LabeledStmt(final SimpleName label, final Statement statement) { + this(null, label, statement); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public LabeledStmt(TokenRange tokenRange, SimpleName label, Statement statement) { + super(tokenRange); + setLabel(label); + setStatement(statement); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Statement getStatement() { + return statement; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public LabeledStmt setStatement(final Statement statement) { + assertNotNull(statement); + if (statement == this.statement) { + return (LabeledStmt) this; + } + notifyPropertyChange(ObservableProperty.STATEMENT, this.statement, statement); + if (this.statement != null) + this.statement.setParentNode(null); + this.statement = statement; + setAsParentNodeOf(statement); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public SimpleName getLabel() { + return label; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public LabeledStmt setLabel(final SimpleName label) { + assertNotNull(label); + if (label == this.label) { + return (LabeledStmt) this; + } + notifyPropertyChange(ObservableProperty.LABEL, this.label, label); + if (this.label != null) + this.label.setParentNode(null); + this.label = label; + setAsParentNodeOf(label); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public LabeledStmt clone() { + return (LabeledStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public LabeledStmtMetaModel getMetaModel() { + return JavaParserMetaModel.labeledStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (node == label) { + setLabel((SimpleName) replacementNode); + return true; + } + if (node == statement) { + setStatement((Statement) replacementNode); + return true; + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isLabeledStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public LabeledStmt asLabeledStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifLabeledStmt(Consumer<LabeledStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<LabeledStmt> toLabeledStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/LocalClassDeclarationStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/LocalClassDeclarationStmt.java new file mode 100644 index 000000000..f78eebc3d --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/LocalClassDeclarationStmt.java @@ -0,0 +1,157 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.LocalClassDeclarationStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import java.util.function.Consumer; +import java.util.Optional; + +/** + * <h1>A class declaration inside a method.</h1> + * <h2>Java 1.0</h2> + * Not available. + * <h2>Java 1.1+</h2> + * A statement consisting of a class declaration. + * <br/><code>class X { void m() { <b>class Y { }</b> } }</code> + * + * @see ClassOrInterfaceDeclaration + * @author Julio Vilmar Gesser + */ +public final class LocalClassDeclarationStmt extends Statement { + + private ClassOrInterfaceDeclaration classDeclaration; + + public LocalClassDeclarationStmt() { + this(null, new ClassOrInterfaceDeclaration()); + } + + @AllFieldsConstructor + public LocalClassDeclarationStmt(final ClassOrInterfaceDeclaration classDeclaration) { + this(null, classDeclaration); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public LocalClassDeclarationStmt(TokenRange tokenRange, ClassOrInterfaceDeclaration classDeclaration) { + super(tokenRange); + setClassDeclaration(classDeclaration); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ClassOrInterfaceDeclaration getClassDeclaration() { + return classDeclaration; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public LocalClassDeclarationStmt setClassDeclaration(final ClassOrInterfaceDeclaration classDeclaration) { + assertNotNull(classDeclaration); + if (classDeclaration == this.classDeclaration) { + return (LocalClassDeclarationStmt) this; + } + notifyPropertyChange(ObservableProperty.CLASS_DECLARATION, this.classDeclaration, classDeclaration); + if (this.classDeclaration != null) + this.classDeclaration.setParentNode(null); + this.classDeclaration = classDeclaration; + setAsParentNodeOf(classDeclaration); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public LocalClassDeclarationStmt clone() { + return (LocalClassDeclarationStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public LocalClassDeclarationStmtMetaModel getMetaModel() { + return JavaParserMetaModel.localClassDeclarationStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (node == classDeclaration) { + setClassDeclaration((ClassOrInterfaceDeclaration) replacementNode); + return true; + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isLocalClassDeclarationStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public LocalClassDeclarationStmt asLocalClassDeclarationStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifLocalClassDeclarationStmt(Consumer<LocalClassDeclarationStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<LocalClassDeclarationStmt> toLocalClassDeclarationStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ReturnStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ReturnStmt.java new file mode 100644 index 000000000..c9bdb4583 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ReturnStmt.java @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.expr.NameExpr; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import java.util.Optional; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.OptionalProperty; +import com.github.javaparser.metamodel.ReturnStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import java.util.function.Consumer; + +/** + * The return statement, with an optional expression to return. + * <br/><code>return 5 * 5;</code> + * @author Julio Vilmar Gesser + */ +public final class ReturnStmt extends Statement { + + @OptionalProperty + private Expression expression; + + public ReturnStmt() { + this(null, null); + } + + @AllFieldsConstructor + public ReturnStmt(final Expression expression) { + this(null, expression); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public ReturnStmt(TokenRange tokenRange, Expression expression) { + super(tokenRange); + setExpression(expression); + customInitialization(); + } + + /** + * Will create a NameExpr with the string param + */ + public ReturnStmt(String expression) { + this(null, new NameExpr(expression)); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Optional<Expression> getExpression() { + return Optional.ofNullable(expression); + } + + /** + * Sets the expression + * + * @param expression the expression, can be null + * @return this, the ReturnStmt + */ + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ReturnStmt setExpression(final Expression expression) { + if (expression == this.expression) { + return (ReturnStmt) this; + } + notifyPropertyChange(ObservableProperty.EXPRESSION, this.expression, expression); + if (this.expression != null) + this.expression.setParentNode(null); + this.expression = expression; + setAsParentNodeOf(expression); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + if (expression != null) { + if (node == expression) { + removeExpression(); + return true; + } + } + return super.remove(node); + } + + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public ReturnStmt removeExpression() { + return setExpression((Expression) null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public ReturnStmt clone() { + return (ReturnStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public ReturnStmtMetaModel getMetaModel() { + return JavaParserMetaModel.returnStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (expression != null) { + if (node == expression) { + setExpression((Expression) replacementNode); + return true; + } + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isReturnStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ReturnStmt asReturnStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifReturnStmt(Consumer<ReturnStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ReturnStmt> toReturnStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/Statement.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/Statement.java new file mode 100644 index 000000000..75100e829 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/Statement.java @@ -0,0 +1,481 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import com.github.javaparser.metamodel.StatementMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import java.util.function.Consumer; +import static com.github.javaparser.utils.CodeGenerationUtils.f; +import java.util.Optional; + +/** + * A base class for all statements. + * + * @author Julio Vilmar Gesser + */ +public abstract class Statement extends Node { + + @AllFieldsConstructor + public Statement() { + this(null); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public Statement(TokenRange tokenRange) { + super(tokenRange); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public Statement clone() { + return (Statement) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public StatementMetaModel getMetaModel() { + return JavaParserMetaModel.statementMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + return super.replace(node, replacementNode); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isAssertStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public AssertStmt asAssertStmt() { + throw new IllegalStateException(f("%s is not an AssertStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isBlockStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public BlockStmt asBlockStmt() { + throw new IllegalStateException(f("%s is not an BlockStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isBreakStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public BreakStmt asBreakStmt() { + throw new IllegalStateException(f("%s is not an BreakStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isContinueStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ContinueStmt asContinueStmt() { + throw new IllegalStateException(f("%s is not an ContinueStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isDoStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public DoStmt asDoStmt() { + throw new IllegalStateException(f("%s is not an DoStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isEmptyStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public EmptyStmt asEmptyStmt() { + throw new IllegalStateException(f("%s is not an EmptyStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isExplicitConstructorInvocationStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ExplicitConstructorInvocationStmt asExplicitConstructorInvocationStmt() { + throw new IllegalStateException(f("%s is not an ExplicitConstructorInvocationStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isExpressionStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ExpressionStmt asExpressionStmt() { + throw new IllegalStateException(f("%s is not an ExpressionStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isForStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ForStmt asForStmt() { + throw new IllegalStateException(f("%s is not an ForStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isForeachStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ForeachStmt asForeachStmt() { + throw new IllegalStateException(f("%s is not an ForeachStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isIfStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public IfStmt asIfStmt() { + throw new IllegalStateException(f("%s is not an IfStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isLabeledStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public LabeledStmt asLabeledStmt() { + throw new IllegalStateException(f("%s is not an LabeledStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isLocalClassDeclarationStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public LocalClassDeclarationStmt asLocalClassDeclarationStmt() { + throw new IllegalStateException(f("%s is not an LocalClassDeclarationStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isReturnStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ReturnStmt asReturnStmt() { + throw new IllegalStateException(f("%s is not an ReturnStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isSwitchEntryStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public SwitchEntryStmt asSwitchEntryStmt() { + throw new IllegalStateException(f("%s is not an SwitchEntryStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isSwitchStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public SwitchStmt asSwitchStmt() { + throw new IllegalStateException(f("%s is not an SwitchStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isSynchronizedStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public SynchronizedStmt asSynchronizedStmt() { + throw new IllegalStateException(f("%s is not an SynchronizedStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isThrowStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ThrowStmt asThrowStmt() { + throw new IllegalStateException(f("%s is not an ThrowStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isTryStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public TryStmt asTryStmt() { + throw new IllegalStateException(f("%s is not an TryStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isUnparsableStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public UnparsableStmt asUnparsableStmt() { + throw new IllegalStateException(f("%s is not an UnparsableStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isWhileStmt() { + return false; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public WhileStmt asWhileStmt() { + throw new IllegalStateException(f("%s is not an WhileStmt", this)); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifAssertStmt(Consumer<AssertStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifBlockStmt(Consumer<BlockStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifBreakStmt(Consumer<BreakStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifContinueStmt(Consumer<ContinueStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifDoStmt(Consumer<DoStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifEmptyStmt(Consumer<EmptyStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifExplicitConstructorInvocationStmt(Consumer<ExplicitConstructorInvocationStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifExpressionStmt(Consumer<ExpressionStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifForStmt(Consumer<ForStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifForeachStmt(Consumer<ForeachStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifIfStmt(Consumer<IfStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifLabeledStmt(Consumer<LabeledStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifLocalClassDeclarationStmt(Consumer<LocalClassDeclarationStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifReturnStmt(Consumer<ReturnStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifSwitchEntryStmt(Consumer<SwitchEntryStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifSwitchStmt(Consumer<SwitchStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifSynchronizedStmt(Consumer<SynchronizedStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifThrowStmt(Consumer<ThrowStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifTryStmt(Consumer<TryStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifUnparsableStmt(Consumer<UnparsableStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifWhileStmt(Consumer<WhileStmt> action) { + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<AssertStmt> toAssertStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<BlockStmt> toBlockStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<BreakStmt> toBreakStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ContinueStmt> toContinueStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<DoStmt> toDoStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<EmptyStmt> toEmptyStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ExplicitConstructorInvocationStmt> toExplicitConstructorInvocationStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ExpressionStmt> toExpressionStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ForStmt> toForStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ForeachStmt> toForeachStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<IfStmt> toIfStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<LabeledStmt> toLabeledStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<LocalClassDeclarationStmt> toLocalClassDeclarationStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ReturnStmt> toReturnStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<SwitchEntryStmt> toSwitchEntryStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<SwitchStmt> toSwitchStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<SynchronizedStmt> toSynchronizedStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ThrowStmt> toThrowStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<TryStmt> toTryStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<UnparsableStmt> toUnparsableStmt() { + return Optional.empty(); + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<WhileStmt> toWhileStmt() { + return Optional.empty(); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/SwitchEntryStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/SwitchEntryStmt.java new file mode 100644 index 000000000..afa8fcecb --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/SwitchEntryStmt.java @@ -0,0 +1,224 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.nodeTypes.NodeWithStatements; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.OptionalProperty; +import com.github.javaparser.metamodel.SwitchEntryStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import java.util.function.Consumer; + +/** + * One case in a switch statement. + * <br/><pre> + * switch (i) { + * case 1: + * case 2: + * System.out.println(444); + * break; + * default: + * System.out.println(0); + * } + * </pre> + * This contains three SwitchEntryStmts. + * <br/>The first one has label 1 and no statements. + * <br/>The second has label 2 and two statements (the println and the break). + * <br/>The third, the default, has no label and one statement. + * + * @author Julio Vilmar Gesser + * @see SwitchStmt + */ +public final class SwitchEntryStmt extends Statement implements NodeWithStatements<SwitchEntryStmt> { + + @OptionalProperty + private Expression label; + + private NodeList<Statement> statements; + + public SwitchEntryStmt() { + this(null, null, new NodeList<>()); + } + + @AllFieldsConstructor + public SwitchEntryStmt(final Expression label, final NodeList<Statement> statements) { + this(null, label, statements); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public SwitchEntryStmt(TokenRange tokenRange, Expression label, NodeList<Statement> statements) { + super(tokenRange); + setLabel(label); + setStatements(statements); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Optional<Expression> getLabel() { + return Optional.ofNullable(label); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public NodeList<Statement> getStatements() { + return statements; + } + + /** + * Sets the label + * + * @param label the label, can be null + * @return this, the SwitchEntryStmt + */ + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public SwitchEntryStmt setLabel(final Expression label) { + if (label == this.label) { + return (SwitchEntryStmt) this; + } + notifyPropertyChange(ObservableProperty.LABEL, this.label, label); + if (this.label != null) + this.label.setParentNode(null); + this.label = label; + setAsParentNodeOf(label); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public SwitchEntryStmt setStatements(final NodeList<Statement> statements) { + assertNotNull(statements); + if (statements == this.statements) { + return (SwitchEntryStmt) this; + } + notifyPropertyChange(ObservableProperty.STATEMENTS, this.statements, statements); + if (this.statements != null) + this.statements.setParentNode(null); + this.statements = statements; + setAsParentNodeOf(statements); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + if (label != null) { + if (node == label) { + removeLabel(); + return true; + } + } + for (int i = 0; i < statements.size(); i++) { + if (statements.get(i) == node) { + statements.remove(i); + return true; + } + } + return super.remove(node); + } + + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public SwitchEntryStmt removeLabel() { + return setLabel((Expression) null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public SwitchEntryStmt clone() { + return (SwitchEntryStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public SwitchEntryStmtMetaModel getMetaModel() { + return JavaParserMetaModel.switchEntryStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (label != null) { + if (node == label) { + setLabel((Expression) replacementNode); + return true; + } + } + for (int i = 0; i < statements.size(); i++) { + if (statements.get(i) == node) { + statements.set(i, (Statement) replacementNode); + return true; + } + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isSwitchEntryStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public SwitchEntryStmt asSwitchEntryStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifSwitchEntryStmt(Consumer<SwitchEntryStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<SwitchEntryStmt> toSwitchEntryStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/SwitchStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/SwitchStmt.java new file mode 100644 index 000000000..36e88b27c --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/SwitchStmt.java @@ -0,0 +1,214 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.expr.NameExpr; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import java.util.Arrays; +import java.util.List; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.SwitchStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import java.util.function.Consumer; +import java.util.Optional; + +/** + * A switch statement. + * <br/>In <code>switch(a) { ... }</code> the selector is "a", + * and the contents of the { ... } are the entries. + * + * @author Julio Vilmar Gesser + * @see SwitchEntryStmt + */ +public final class SwitchStmt extends Statement { + + private Expression selector; + + private NodeList<SwitchEntryStmt> entries; + + public SwitchStmt() { + this(null, new NameExpr(), new NodeList<>()); + } + + @AllFieldsConstructor + public SwitchStmt(final Expression selector, final NodeList<SwitchEntryStmt> entries) { + this(null, selector, entries); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public SwitchStmt(TokenRange tokenRange, Expression selector, NodeList<SwitchEntryStmt> entries) { + super(tokenRange); + setSelector(selector); + setEntries(entries); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public NodeList<SwitchEntryStmt> getEntries() { + return entries; + } + + public SwitchEntryStmt getEntry(int i) { + return getEntries().get(i); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Expression getSelector() { + return selector; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public SwitchStmt setEntries(final NodeList<SwitchEntryStmt> entries) { + assertNotNull(entries); + if (entries == this.entries) { + return (SwitchStmt) this; + } + notifyPropertyChange(ObservableProperty.ENTRIES, this.entries, entries); + if (this.entries != null) + this.entries.setParentNode(null); + this.entries = entries; + setAsParentNodeOf(entries); + return this; + } + + /** + * @deprecated use a method on getEntries instead + */ + @Deprecated + public SwitchStmt setEntry(int i, SwitchEntryStmt entry) { + getEntries().set(i, entry); + return this; + } + + /** + * @deprecated use a method on getEntries instead + */ + @Deprecated + public SwitchStmt addEntry(SwitchEntryStmt entry) { + getEntries().add(entry); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public SwitchStmt setSelector(final Expression selector) { + assertNotNull(selector); + if (selector == this.selector) { + return (SwitchStmt) this; + } + notifyPropertyChange(ObservableProperty.SELECTOR, this.selector, selector); + if (this.selector != null) + this.selector.setParentNode(null); + this.selector = selector; + setAsParentNodeOf(selector); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + for (int i = 0; i < entries.size(); i++) { + if (entries.get(i) == node) { + entries.remove(i); + return true; + } + } + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public SwitchStmt clone() { + return (SwitchStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public SwitchStmtMetaModel getMetaModel() { + return JavaParserMetaModel.switchStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + for (int i = 0; i < entries.size(); i++) { + if (entries.get(i) == node) { + entries.set(i, (SwitchEntryStmt) replacementNode); + return true; + } + } + if (node == selector) { + setSelector((Expression) replacementNode); + return true; + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isSwitchStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public SwitchStmt asSwitchStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifSwitchStmt(Consumer<SwitchStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<SwitchStmt> toSwitchStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/SynchronizedStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/SynchronizedStmt.java new file mode 100644 index 000000000..376d525b7 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/SynchronizedStmt.java @@ -0,0 +1,181 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.expr.NameExpr; +import com.github.javaparser.ast.nodeTypes.NodeWithBlockStmt; +import com.github.javaparser.ast.nodeTypes.NodeWithExpression; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.SynchronizedStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import java.util.function.Consumer; +import java.util.Optional; + +/** + * Usage of the synchronized keyword. + * <br/>In <code>synchronized (a123) { ... }</code> the expression is a123 and { ... } is the body + * + * @author Julio Vilmar Gesser + */ +public final class SynchronizedStmt extends Statement implements NodeWithBlockStmt<SynchronizedStmt>, NodeWithExpression<SynchronizedStmt> { + + private Expression expression; + + private BlockStmt body; + + public SynchronizedStmt() { + this(null, new NameExpr(), new BlockStmt()); + } + + @AllFieldsConstructor + public SynchronizedStmt(final Expression expression, final BlockStmt body) { + this(null, expression, body); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public SynchronizedStmt(TokenRange tokenRange, Expression expression, BlockStmt body) { + super(tokenRange); + setExpression(expression); + setBody(body); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Expression getExpression() { + return expression; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public SynchronizedStmt setExpression(final Expression expression) { + assertNotNull(expression); + if (expression == this.expression) { + return (SynchronizedStmt) this; + } + notifyPropertyChange(ObservableProperty.EXPRESSION, this.expression, expression); + if (this.expression != null) + this.expression.setParentNode(null); + this.expression = expression; + setAsParentNodeOf(expression); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public BlockStmt getBody() { + return body; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public SynchronizedStmt setBody(final BlockStmt body) { + assertNotNull(body); + if (body == this.body) { + return (SynchronizedStmt) this; + } + notifyPropertyChange(ObservableProperty.BODY, this.body, body); + if (this.body != null) + this.body.setParentNode(null); + this.body = body; + setAsParentNodeOf(body); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public SynchronizedStmt clone() { + return (SynchronizedStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public SynchronizedStmtMetaModel getMetaModel() { + return JavaParserMetaModel.synchronizedStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (node == body) { + setBody((BlockStmt) replacementNode); + return true; + } + if (node == expression) { + setExpression((Expression) replacementNode); + return true; + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isSynchronizedStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public SynchronizedStmt asSynchronizedStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifSynchronizedStmt(Consumer<SynchronizedStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<SynchronizedStmt> toSynchronizedStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ThrowStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ThrowStmt.java new file mode 100644 index 000000000..736b8351e --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/ThrowStmt.java @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.expr.NameExpr; +import com.github.javaparser.ast.nodeTypes.NodeWithExpression; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.ThrowStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import java.util.function.Consumer; +import java.util.Optional; + +/** + * Usage of the throw statement. + * <br/><code>throw new Exception()</code> + * + * @author Julio Vilmar Gesser + */ +public final class ThrowStmt extends Statement implements NodeWithExpression<ThrowStmt> { + + private Expression expression; + + public ThrowStmt() { + this(null, new NameExpr()); + } + + @AllFieldsConstructor + public ThrowStmt(final Expression expression) { + this(null, expression); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public ThrowStmt(TokenRange tokenRange, Expression expression) { + super(tokenRange); + setExpression(expression); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Expression getExpression() { + return expression; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public ThrowStmt setExpression(final Expression expression) { + assertNotNull(expression); + if (expression == this.expression) { + return (ThrowStmt) this; + } + notifyPropertyChange(ObservableProperty.EXPRESSION, this.expression, expression); + if (this.expression != null) + this.expression.setParentNode(null); + this.expression = expression; + setAsParentNodeOf(expression); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public ThrowStmt clone() { + return (ThrowStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public ThrowStmtMetaModel getMetaModel() { + return JavaParserMetaModel.throwStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (node == expression) { + setExpression((Expression) replacementNode); + return true; + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isThrowStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public ThrowStmt asThrowStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifThrowStmt(Consumer<ThrowStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<ThrowStmt> toThrowStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/TryStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/TryStmt.java new file mode 100644 index 000000000..6e4a81a42 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/TryStmt.java @@ -0,0 +1,321 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.TokenRange; +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import com.github.javaparser.metamodel.OptionalProperty; +import com.github.javaparser.metamodel.TryStmtMetaModel; +import javax.annotation.Generated; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import static com.github.javaparser.utils.Utils.assertNotNull; +import java.util.function.Consumer; + +/** + * <h1>The try statement</h1> + * <h2>Java 1.0-6</h2> + * <pre> + * try { + * // ... + * } catch (IOException e) { + * // ... + * } finally { + * // ... + * } + * </pre> + * In this code, "// do things" is the content of the tryBlock, there is one catch clause that catches IOException e, + * and there is a finally block. + * <p> + * The catch and finally blocks are optional, but they should not be empty at the same time. + * <h2>Java 7-8</h2> + * <pre> + * try (InputStream i = new FileInputStream("file")) { + * // ... + * } catch (IOException|NullPointerException e) { + * // ... + * } finally { + * // ... + * } + * </pre> + * Java 7 introduced two things: + * <ul> + * <li>Resources can be specified after "try", but only variable declarations (VariableDeclarationExpr.)</li> + * <li>A single catch can catch multiple exception types. This uses the IntersectionType.</li> + * </ul> + * <h2>Java 9+</h2> + * <pre> + * try (r) { + * // ... + * } catch (IOException|NullPointerException e) { + * // ... + * } finally { + * // ... + * } + * </pre> + * Java 9 finishes resources: you can now refer to a resource that was declared somewhere else. + * The following types are allowed: + * <ul> + * <li>VariableDeclarationExpr: "X x = new X()" like in Java 7-8.</li> + * <li>NameExpr: "a".</li> + * <li>FieldAccessExpr: "x.y.z", "super.test" etc.</li> + * </ul> + * + * @author Julio Vilmar Gesser + * @see CatchClause + * @see com.github.javaparser.ast.type.IntersectionType + * @see com.github.javaparser.ast.expr.FieldAccessExpr + * @see com.github.javaparser.ast.expr.NameExpr + */ +public final class TryStmt extends Statement { + + private NodeList<Expression> resources; + + private BlockStmt tryBlock; + + private NodeList<CatchClause> catchClauses; + + @OptionalProperty + private BlockStmt finallyBlock; + + public TryStmt() { + this(null, new NodeList<>(), new BlockStmt(), new NodeList<>(), null); + } + + public TryStmt(final BlockStmt tryBlock, final NodeList<CatchClause> catchClauses, final BlockStmt finallyBlock) { + this(null, new NodeList<>(), tryBlock, catchClauses, finallyBlock); + } + + @AllFieldsConstructor + public TryStmt(NodeList<Expression> resources, final BlockStmt tryBlock, final NodeList<CatchClause> catchClauses, final BlockStmt finallyBlock) { + this(null, resources, tryBlock, catchClauses, finallyBlock); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public TryStmt(TokenRange tokenRange, NodeList<Expression> resources, BlockStmt tryBlock, NodeList<CatchClause> catchClauses, BlockStmt finallyBlock) { + super(tokenRange); + setResources(resources); + setTryBlock(tryBlock); + setCatchClauses(catchClauses); + setFinallyBlock(finallyBlock); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public NodeList<CatchClause> getCatchClauses() { + return catchClauses; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Optional<BlockStmt> getFinallyBlock() { + return Optional.ofNullable(finallyBlock); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public BlockStmt getTryBlock() { + return tryBlock; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public NodeList<Expression> getResources() { + return resources; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public TryStmt setCatchClauses(final NodeList<CatchClause> catchClauses) { + assertNotNull(catchClauses); + if (catchClauses == this.catchClauses) { + return (TryStmt) this; + } + notifyPropertyChange(ObservableProperty.CATCH_CLAUSES, this.catchClauses, catchClauses); + if (this.catchClauses != null) + this.catchClauses.setParentNode(null); + this.catchClauses = catchClauses; + setAsParentNodeOf(catchClauses); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public TryStmt setFinallyBlock(final BlockStmt finallyBlock) { + if (finallyBlock == this.finallyBlock) { + return (TryStmt) this; + } + notifyPropertyChange(ObservableProperty.FINALLY_BLOCK, this.finallyBlock, finallyBlock); + if (this.finallyBlock != null) + this.finallyBlock.setParentNode(null); + this.finallyBlock = finallyBlock; + setAsParentNodeOf(finallyBlock); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public TryStmt setTryBlock(final BlockStmt tryBlock) { + assertNotNull(tryBlock); + if (tryBlock == this.tryBlock) { + return (TryStmt) this; + } + notifyPropertyChange(ObservableProperty.TRY_BLOCK, this.tryBlock, tryBlock); + if (this.tryBlock != null) + this.tryBlock.setParentNode(null); + this.tryBlock = tryBlock; + setAsParentNodeOf(tryBlock); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public TryStmt setResources(final NodeList<Expression> resources) { + assertNotNull(resources); + if (resources == this.resources) { + return (TryStmt) this; + } + notifyPropertyChange(ObservableProperty.RESOURCES, this.resources, resources); + if (this.resources != null) + this.resources.setParentNode(null); + this.resources = resources; + setAsParentNodeOf(resources); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + for (int i = 0; i < catchClauses.size(); i++) { + if (catchClauses.get(i) == node) { + catchClauses.remove(i); + return true; + } + } + if (finallyBlock != null) { + if (node == finallyBlock) { + removeFinallyBlock(); + return true; + } + } + for (int i = 0; i < resources.size(); i++) { + if (resources.get(i) == node) { + resources.remove(i); + return true; + } + } + return super.remove(node); + } + + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public TryStmt removeFinallyBlock() { + return setFinallyBlock((BlockStmt) null); + } + + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public TryStmt removeTryBlock() { + return setTryBlock((BlockStmt) null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public TryStmt clone() { + return (TryStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public TryStmtMetaModel getMetaModel() { + return JavaParserMetaModel.tryStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + for (int i = 0; i < catchClauses.size(); i++) { + if (catchClauses.get(i) == node) { + catchClauses.set(i, (CatchClause) replacementNode); + return true; + } + } + if (finallyBlock != null) { + if (node == finallyBlock) { + setFinallyBlock((BlockStmt) replacementNode); + return true; + } + } + for (int i = 0; i < resources.size(); i++) { + if (resources.get(i) == node) { + resources.set(i, (Expression) replacementNode); + return true; + } + } + if (node == tryBlock) { + setTryBlock((BlockStmt) replacementNode); + return true; + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isTryStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public TryStmt asTryStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifTryStmt(Consumer<TryStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<TryStmt> toTryStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/UnparsableStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/UnparsableStmt.java new file mode 100644 index 000000000..2a7fc011b --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/UnparsableStmt.java @@ -0,0 +1,103 @@ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.TokenRange; +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import com.github.javaparser.ast.Node; +import javax.annotation.Generated; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import com.github.javaparser.metamodel.UnparsableStmtMetaModel; +import static com.github.javaparser.ast.Node.Parsedness.*; +import java.util.function.Consumer; +import java.util.Optional; + +/** + * A statement that had parse errors. + * Nothing is known about it except the tokens it covers. + */ +public final class UnparsableStmt extends Statement { + + @AllFieldsConstructor + public UnparsableStmt() { + this(null); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public UnparsableStmt(TokenRange tokenRange) { + super(tokenRange); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public UnparsableStmt clone() { + return (UnparsableStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public UnparsableStmtMetaModel getMetaModel() { + return JavaParserMetaModel.unparsableStmtMetaModel; + } + + @Override + public Parsedness getParsed() { + return UNPARSABLE; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isUnparsableStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public UnparsableStmt asUnparsableStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifUnparsableStmt(Consumer<UnparsableStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<UnparsableStmt> toUnparsableStmt() { + return Optional.of(this); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/WhileStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/WhileStmt.java new file mode 100644 index 000000000..5fb1f9971 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/stmt/WhileStmt.java @@ -0,0 +1,181 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ +package com.github.javaparser.ast.stmt; + +import com.github.javaparser.ast.AllFieldsConstructor; +import com.github.javaparser.ast.expr.BooleanLiteralExpr; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.nodeTypes.NodeWithBody; +import com.github.javaparser.ast.nodeTypes.NodeWithCondition; +import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; +import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.metamodel.WhileStmtMetaModel; +import com.github.javaparser.metamodel.JavaParserMetaModel; +import javax.annotation.Generated; +import com.github.javaparser.TokenRange; +import java.util.function.Consumer; +import java.util.Optional; + +/** + * A while statement. + * <br/><code>while(true) { ... }</code> + * + * @author Julio Vilmar Gesser + */ +public final class WhileStmt extends Statement implements NodeWithBody<WhileStmt>, NodeWithCondition<WhileStmt> { + + private Expression condition; + + private Statement body; + + public WhileStmt() { + this(null, new BooleanLiteralExpr(), new ReturnStmt()); + } + + @AllFieldsConstructor + public WhileStmt(final Expression condition, final Statement body) { + this(null, condition, body); + } + + /** + * This constructor is used by the parser and is considered private. + */ + @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") + public WhileStmt(TokenRange tokenRange, Expression condition, Statement body) { + super(tokenRange); + setCondition(condition); + setBody(body); + customInitialization(); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Statement getBody() { + return body; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public Expression getCondition() { + return condition; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public WhileStmt setBody(final Statement body) { + assertNotNull(body); + if (body == this.body) { + return (WhileStmt) this; + } + notifyPropertyChange(ObservableProperty.BODY, this.body, body); + if (this.body != null) + this.body.setParentNode(null); + this.body = body; + setAsParentNodeOf(body); + return this; + } + + @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") + public WhileStmt setCondition(final Expression condition) { + assertNotNull(condition); + if (condition == this.condition) { + return (WhileStmt) this; + } + notifyPropertyChange(ObservableProperty.CONDITION, this.condition, condition); + if (this.condition != null) + this.condition.setParentNode(null); + this.condition = condition; + setAsParentNodeOf(condition); + return this; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") + public boolean remove(Node node) { + if (node == null) + return false; + return super.remove(node); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.CloneGenerator") + public WhileStmt clone() { + return (WhileStmt) accept(new CloneVisitor(), null); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") + public WhileStmtMetaModel getMetaModel() { + return JavaParserMetaModel.whileStmtMetaModel; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") + public boolean replace(Node node, Node replacementNode) { + if (node == null) + return false; + if (node == body) { + setBody((Statement) replacementNode); + return true; + } + if (node == condition) { + setCondition((Expression) replacementNode); + return true; + } + return super.replace(node, replacementNode); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public boolean isWhileStmt() { + return true; + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public WhileStmt asWhileStmt() { + return this; + } + + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public void ifWhileStmt(Consumer<WhileStmt> action) { + action.accept(this); + } + + @Override + @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") + public Optional<WhileStmt> toWhileStmt() { + return Optional.of(this); + } +} |