1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | package com.github.javaparser.ast.stmt; |
22 | |
23 | import com.github.javaparser.ast.AllFieldsConstructor; |
24 | import com.github.javaparser.ast.NodeList; |
25 | import com.github.javaparser.ast.expr.*; |
26 | import com.github.javaparser.ast.nodeTypes.NodeWithArguments; |
27 | import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments; |
28 | import com.github.javaparser.ast.observer.ObservableProperty; |
29 | import com.github.javaparser.ast.type.Type; |
30 | import com.github.javaparser.ast.visitor.GenericVisitor; |
31 | import com.github.javaparser.ast.visitor.VoidVisitor; |
32 | import java.util.Optional; |
33 | import static com.github.javaparser.utils.Utils.assertNotNull; |
34 | import com.github.javaparser.ast.Node; |
35 | import com.github.javaparser.ast.visitor.CloneVisitor; |
36 | import com.github.javaparser.metamodel.ExplicitConstructorInvocationStmtMetaModel; |
37 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
38 | import com.github.javaparser.TokenRange; |
39 | import com.github.javaparser.metamodel.OptionalProperty; |
40 | import com.github.javaparser.resolution.Resolvable; |
41 | import com.github.javaparser.resolution.UnsolvedSymbolException; |
42 | import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration; |
43 | import java.util.function.Consumer; |
44 | import 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 | */ |
55 | public class ExplicitConstructorInvocationStmt extends Statement implements NodeWithTypeArguments<ExplicitConstructorInvocationStmt>, NodeWithArguments<ExplicitConstructorInvocationStmt>, Resolvable<ResolvedConstructorDeclaration> { |
56 | |
57 | @OptionalProperty |
58 | private NodeList<Type> typeArguments; |
59 | |
60 | private boolean isThis; |
61 | |
62 | @OptionalProperty |
63 | private Expression expression; |
64 | |
65 | private NodeList<Expression> arguments; |
66 | |
67 | public ExplicitConstructorInvocationStmt() { |
68 | this(null, null, true, null, new NodeList<>()); |
69 | } |
70 | |
71 | public ExplicitConstructorInvocationStmt(final boolean isThis, final Expression expression, final NodeList<Expression> arguments) { |
72 | this(null, null, isThis, expression, arguments); |
73 | } |
74 | |
75 | @AllFieldsConstructor |
76 | public ExplicitConstructorInvocationStmt(final NodeList<Type> typeArguments, final boolean isThis, final Expression expression, final NodeList<Expression> arguments) { |
77 | this(null, typeArguments, isThis, expression, arguments); |
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 tokenRange, NodeList<Type> typeArguments, boolean isThis, Expression expression, NodeList<Expression> arguments) { |
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 <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
96 | return v.visit(this, arg); |
97 | } |
98 | |
99 | @Override |
100 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
101 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
102 | v.visit(this, arg); |
103 | } |
104 | |
105 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
106 | public NodeList<Expression> getArguments() { |
107 | return arguments; |
108 | } |
109 | |
110 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
111 | public Optional<Expression> getExpression() { |
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<Expression> arguments) { |
122 | assertNotNull(arguments); |
123 | if (arguments == this.arguments) { |
124 | return (ExplicitConstructorInvocationStmt) this; |
125 | } |
126 | notifyPropertyChange(ObservableProperty.ARGUMENTS, this.arguments, arguments); |
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.expression, expression); |
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.isThis, isThis); |
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<Type> typeArguments) { |
176 | if (typeArguments == this.typeArguments) { |
177 | return (ExplicitConstructorInvocationStmt) this; |
178 | } |
179 | notifyPropertyChange(ObservableProperty.TYPE_ARGUMENTS, this.typeArguments, typeArguments); |
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 = 0; i < 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 = 0; i < 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((Expression) null); |
218 | } |
219 | |
220 | @Override |
221 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
222 | public ExplicitConstructorInvocationStmt clone() { |
223 | return (ExplicitConstructorInvocationStmt) accept(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 node, Node replacementNode) { |
235 | if (node == null) |
236 | return false; |
237 | for (int i = 0; i < arguments.size(); i++) { |
238 | if (arguments.get(i) == node) { |
239 | arguments.set(i, (Expression) replacementNode); |
240 | return true; |
241 | } |
242 | } |
243 | if (expression != null) { |
244 | if (node == expression) { |
245 | setExpression((Expression) replacementNode); |
246 | return true; |
247 | } |
248 | } |
249 | if (typeArguments != null) { |
250 | for (int i = 0; i < typeArguments.size(); i++) { |
251 | if (typeArguments.get(i) == node) { |
252 | typeArguments.set(i, (Type) replacementNode); |
253 | return true; |
254 | } |
255 | } |
256 | } |
257 | return super.replace(node, replacementNode); |
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<ExplicitConstructorInvocationStmt> action) { |
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<ExplicitConstructorInvocationStmt> toExplicitConstructorInvocationStmt() { |
297 | return Optional.of(this); |
298 | } |
299 | } |
300 |
Members