JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/stmt/ExplicitConstructorInvocationStmt.java
1/*
2 * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2020 The JavaParser Team.
4 *
5 * This file is part of JavaParser.
6 *
7 * JavaParser can be used either under the terms of
8 * a) the GNU Lesser General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 * b) the terms of the Apache License
12 *
13 * You should have received a copy of both licenses in LICENCE.LGPL and
14 * LICENCE.APACHE. Please refer to those files for details.
15 *
16 * JavaParser is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 */
21package com.github.javaparser.ast.stmt;
22
23import com.github.javaparser.ast.AllFieldsConstructor;
24import com.github.javaparser.ast.NodeList;
25import com.github.javaparser.ast.expr.*;
26import com.github.javaparser.ast.nodeTypes.NodeWithArguments;
27import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments;
28import com.github.javaparser.ast.observer.ObservableProperty;
29import com.github.javaparser.ast.type.Type;
30import com.github.javaparser.ast.visitor.GenericVisitor;
31import com.github.javaparser.ast.visitor.VoidVisitor;
32import java.util.Optional;
33import static com.github.javaparser.utils.Utils.assertNotNull;
34import com.github.javaparser.ast.Node;
35import com.github.javaparser.ast.visitor.CloneVisitor;
36import com.github.javaparser.metamodel.ExplicitConstructorInvocationStmtMetaModel;
37import com.github.javaparser.metamodel.JavaParserMetaModel;
38import com.github.javaparser.TokenRange;
39import com.github.javaparser.metamodel.OptionalProperty;
40import com.github.javaparser.resolution.Resolvable;
41import com.github.javaparser.resolution.UnsolvedSymbolException;
42import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration;
43import java.util.function.Consumer;
44import com.github.javaparser.ast.Generated;
45
46/**
47 * A call to super or this in a constructor or initializer.
48 * <br>{@code class X { X() { super(15); } }}
49 * <br>{@code class X { X() { this(1, 2); } }}
50 *
51 * @author Julio Vilmar Gesser
52 * @see com.github.javaparser.ast.expr.SuperExpr
53 * @see com.github.javaparser.ast.expr.ThisExpr
54 */
55public class ExplicitConstructorInvocationStmt extends Statement implements NodeWithTypeArguments<ExplicitConstructorInvocationStmt>, NodeWithArguments<ExplicitConstructorInvocationStmt>, Resolvable<ResolvedConstructorDeclaration> {
56
57    @OptionalProperty
58    private NodeList<TypetypeArguments;
59
60    private boolean isThis;
61
62    @OptionalProperty
63    private Expression expression;
64
65    private NodeList<Expressionarguments;
66
67    public ExplicitConstructorInvocationStmt() {
68        this(nullnulltruenull, new NodeList<>());
69    }
70
71    public ExplicitConstructorInvocationStmt(final boolean isThisfinal Expression expressionfinal NodeList<Expressionarguments) {
72        this(nullnullisThisexpressionarguments);
73    }
74
75    @AllFieldsConstructor
76    public ExplicitConstructorInvocationStmt(final NodeList<TypetypeArgumentsfinal boolean isThisfinal Expression expressionfinal NodeList<Expressionarguments) {
77        this(nulltypeArgumentsisThisexpressionarguments);
78    }
79
80    /**
81     * This constructor is used by the parser and is considered private.
82     */
83    @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
84    public ExplicitConstructorInvocationStmt(TokenRange tokenRangeNodeList<TypetypeArgumentsboolean isThisExpression expressionNodeList<Expressionarguments) {
85        super(tokenRange);
86        setTypeArguments(typeArguments);
87        setThis(isThis);
88        setExpression(expression);
89        setArguments(arguments);
90        customInitialization();
91    }
92
93    @Override
94    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
95    public <RAR accept(final GenericVisitor<RAvfinal A arg) {
96        return v.visit(this, arg);
97    }
98
99    @Override
100    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
101    public <Avoid accept(final VoidVisitor<Avfinal A arg) {
102        v.visit(this, arg);
103    }
104
105    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
106    public NodeList<ExpressiongetArguments() {
107        return arguments;
108    }
109
110    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
111    public Optional<ExpressiongetExpression() {
112        return Optional.ofNullable(expression);
113    }
114
115    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
116    public boolean isThis() {
117        return isThis;
118    }
119
120    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
121    public ExplicitConstructorInvocationStmt setArguments(final NodeList<Expressionarguments) {
122        assertNotNull(arguments);
123        if (arguments == this.arguments) {
124            return (ExplicitConstructorInvocationStmt) this;
125        }
126        notifyPropertyChange(ObservableProperty.ARGUMENTS, this.argumentsarguments);
127        if (this.arguments != null)
128            this.arguments.setParentNode(null);
129        this.arguments = arguments;
130        setAsParentNodeOf(arguments);
131        return this;
132    }
133
134    /**
135     * Sets the expression
136     *
137     * @param expression the expression, can be null
138     * @return this, the ExplicitConstructorInvocationStmt
139     */
140    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
141    public ExplicitConstructorInvocationStmt setExpression(final Expression expression) {
142        if (expression == this.expression) {
143            return (ExplicitConstructorInvocationStmt) this;
144        }
145        notifyPropertyChange(ObservableProperty.EXPRESSION, this.expressionexpression);
146        if (this.expression != null)
147            this.expression.setParentNode(null);
148        this.expression = expression;
149        setAsParentNodeOf(expression);
150        return this;
151    }
152
153    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
154    public ExplicitConstructorInvocationStmt setThis(final boolean isThis) {
155        if (isThis == this.isThis) {
156            return (ExplicitConstructorInvocationStmt) this;
157        }
158        notifyPropertyChange(ObservableProperty.THIS, this.isThisisThis);
159        this.isThis = isThis;
160        return this;
161    }
162
163    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
164    public Optional<NodeList<Type>> getTypeArguments() {
165        return Optional.ofNullable(typeArguments);
166    }
167
168    /**
169     * Sets the typeArguments
170     *
171     * @param typeArguments the typeArguments, can be null
172     * @return this, the ExplicitConstructorInvocationStmt
173     */
174    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
175    public ExplicitConstructorInvocationStmt setTypeArguments(final NodeList<TypetypeArguments) {
176        if (typeArguments == this.typeArguments) {
177            return (ExplicitConstructorInvocationStmt) this;
178        }
179        notifyPropertyChange(ObservableProperty.TYPE_ARGUMENTS, this.typeArgumentstypeArguments);
180        if (this.typeArguments != null)
181            this.typeArguments.setParentNode(null);
182        this.typeArguments = typeArguments;
183        setAsParentNodeOf(typeArguments);
184        return this;
185    }
186
187    @Override
188    @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
189    public boolean remove(Node node) {
190        if (node == null)
191            return false;
192        for (int i = 0i < arguments.size(); i++) {
193            if (arguments.get(i) == node) {
194                arguments.remove(i);
195                return true;
196            }
197        }
198        if (expression != null) {
199            if (node == expression) {
200                removeExpression();
201                return true;
202            }
203        }
204        if (typeArguments != null) {
205            for (int i = 0i < typeArguments.size(); i++) {
206                if (typeArguments.get(i) == node) {
207                    typeArguments.remove(i);
208                    return true;
209                }
210            }
211        }
212        return super.remove(node);
213    }
214
215    @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
216    public ExplicitConstructorInvocationStmt removeExpression() {
217        return setExpression((Expressionnull);
218    }
219
220    @Override
221    @Generated("com.github.javaparser.generator.core.node.CloneGenerator")
222    public ExplicitConstructorInvocationStmt clone() {
223        return (ExplicitConstructorInvocationStmtaccept(new CloneVisitor(), null);
224    }
225
226    @Override
227    @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
228    public ExplicitConstructorInvocationStmtMetaModel getMetaModel() {
229        return JavaParserMetaModel.explicitConstructorInvocationStmtMetaModel;
230    }
231
232    @Override
233    @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
234    public boolean replace(Node nodeNode replacementNode) {
235        if (node == null)
236            return false;
237        for (int i = 0i < arguments.size(); i++) {
238            if (arguments.get(i) == node) {
239                arguments.set(i, (ExpressionreplacementNode);
240                return true;
241            }
242        }
243        if (expression != null) {
244            if (node == expression) {
245                setExpression((ExpressionreplacementNode);
246                return true;
247            }
248        }
249        if (typeArguments != null) {
250            for (int i = 0i < typeArguments.size(); i++) {
251                if (typeArguments.get(i) == node) {
252                    typeArguments.set(i, (TypereplacementNode);
253                    return true;
254                }
255            }
256        }
257        return super.replace(nodereplacementNode);
258    }
259
260    @Override
261    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
262    public boolean isExplicitConstructorInvocationStmt() {
263        return true;
264    }
265
266    @Override
267    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
268    public ExplicitConstructorInvocationStmt asExplicitConstructorInvocationStmt() {
269        return this;
270    }
271
272    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
273    public void ifExplicitConstructorInvocationStmt(Consumer<ExplicitConstructorInvocationStmtaction) {
274        action.accept(this);
275    }
276
277    /**
278     * Attempts to resolve the declaration corresponding to the invoked constructor. If successful, a
279     * {@link ResolvedConstructorDeclaration} representing the declaration of the constructor invoked by this
280     * {@code ExplicitConstructorInvocationStmt} is returned. Otherwise, an {@link UnsolvedSymbolException} is thrown.
281     *
282     * @return a {@link ResolvedConstructorDeclaration} representing the declaration of the invoked constructor.
283     * @throws UnsolvedSymbolException if the declaration corresponding to the explicit constructor invocation statement
284     *                                 could not be resolved.
285     * @see NameExpr#resolve()
286     * @see FieldAccessExpr#resolve()
287     * @see MethodCallExpr#resolve()
288     * @see ObjectCreationExpr#resolve()
289     */
290    public ResolvedConstructorDeclaration resolve() {
291        return getSymbolResolver().resolveDeclaration(this, ResolvedConstructorDeclaration.class);
292    }
293
294    @Override
295    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
296    public Optional<ExplicitConstructorInvocationStmttoExplicitConstructorInvocationStmt() {
297        return Optional.of(this);
298    }
299}
300
MembersX
ExplicitConstructorInvocationStmt:setExpression
ExplicitConstructorInvocationStmt:setThis
ExplicitConstructorInvocationStmt:asExplicitConstructorInvocationStmt
ExplicitConstructorInvocationStmt:getArguments
ExplicitConstructorInvocationStmt:setArguments
ExplicitConstructorInvocationStmt:typeArguments
ExplicitConstructorInvocationStmt:ExplicitConstructorInvocationStmt
ExplicitConstructorInvocationStmt:getMetaModel
ExplicitConstructorInvocationStmt:arguments
ExplicitConstructorInvocationStmt:setTypeArguments
ExplicitConstructorInvocationStmt:ifExplicitConstructorInvocationStmt
ExplicitConstructorInvocationStmt:getTypeArguments
ExplicitConstructorInvocationStmt:expression
ExplicitConstructorInvocationStmt:getExpression
ExplicitConstructorInvocationStmt:removeExpression
ExplicitConstructorInvocationStmt:replace
ExplicitConstructorInvocationStmt:accept
ExplicitConstructorInvocationStmt:remove
ExplicitConstructorInvocationStmt:clone
ExplicitConstructorInvocationStmt:isExplicitConstructorInvocationStmt
ExplicitConstructorInvocationStmt:toExplicitConstructorInvocationStmt
ExplicitConstructorInvocationStmt:isThis
ExplicitConstructorInvocationStmt:resolve
Members
X