JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/expr/MethodCallExpr.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.expr;
22
23import com.github.javaparser.ast.AllFieldsConstructor;
24import com.github.javaparser.ast.NodeList;
25import com.github.javaparser.ast.nodeTypes.NodeWithArguments;
26import com.github.javaparser.ast.nodeTypes.NodeWithOptionalScope;
27import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
28import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments;
29import com.github.javaparser.ast.observer.ObservableProperty;
30import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt;
31import com.github.javaparser.ast.type.Type;
32import com.github.javaparser.ast.visitor.GenericVisitor;
33import com.github.javaparser.ast.visitor.VoidVisitor;
34import java.util.Optional;
35import static com.github.javaparser.utils.Utils.assertNotNull;
36import com.github.javaparser.ast.Node;
37import com.github.javaparser.ast.visitor.CloneVisitor;
38import com.github.javaparser.metamodel.MethodCallExprMetaModel;
39import com.github.javaparser.metamodel.JavaParserMetaModel;
40import com.github.javaparser.TokenRange;
41import com.github.javaparser.metamodel.OptionalProperty;
42import com.github.javaparser.resolution.Resolvable;
43import com.github.javaparser.resolution.UnsolvedSymbolException;
44import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
45import java.util.function.Consumer;
46import com.github.javaparser.ast.Generated;
47
48/**
49 * A method call on an object or a class. <br>{@code circle.circumference()} <br>In <code>a.&lt;String&gt;bb(15);</code> a
50 * is the scope, String is a type argument, bb is the name and 15 is an argument.
51 *
52 * @author Julio Vilmar Gesser
53 */
54public class MethodCallExpr extends Expression implements NodeWithTypeArguments<MethodCallExpr>, NodeWithArguments<MethodCallExpr>, NodeWithSimpleName<MethodCallExpr>, NodeWithOptionalScope<MethodCallExpr>, Resolvable<ResolvedMethodDeclaration> {
55
56    @OptionalProperty
57    private Expression scope;
58
59    @OptionalProperty
60    private NodeList<TypetypeArguments;
61
62    private SimpleName name;
63
64    private NodeList<Expressionarguments;
65
66    public MethodCallExpr() {
67        this(nullnullnull, new SimpleName(), new NodeList<>());
68    }
69
70    public MethodCallExpr(String nameExpression... arguments) {
71        this(nullnullnull, new SimpleName(name), new NodeList<>(arguments));
72    }
73
74    public MethodCallExpr(final Expression scopefinal String name) {
75        this(nullscopenull, new SimpleName(name), new NodeList<>());
76    }
77
78    public MethodCallExpr(final Expression scopefinal SimpleName name) {
79        this(nullscopenullname, new NodeList<>());
80    }
81
82    public MethodCallExpr(final Expression scopefinal String namefinal NodeList<Expressionarguments) {
83        this(nullscopenull, new SimpleName(name), arguments);
84    }
85
86    public MethodCallExpr(final Expression scopefinal NodeList<TypetypeArgumentsfinal String namefinal NodeList<Expressionarguments) {
87        this(nullscopetypeArguments, new SimpleName(name), arguments);
88    }
89
90    public MethodCallExpr(final Expression scopefinal SimpleName namefinal NodeList<Expressionarguments) {
91        this(nullscopenullnamearguments);
92    }
93
94    @AllFieldsConstructor
95    public MethodCallExpr(final Expression scopefinal NodeList<TypetypeArgumentsfinal SimpleName namefinal NodeList<Expressionarguments) {
96        this(nullscopetypeArgumentsnamearguments);
97    }
98
99    /**
100     * This constructor is used by the parser and is considered private.
101     */
102    @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
103    public MethodCallExpr(TokenRange tokenRangeExpression scopeNodeList<TypetypeArgumentsSimpleName nameNodeList<Expressionarguments) {
104        super(tokenRange);
105        setScope(scope);
106        setTypeArguments(typeArguments);
107        setName(name);
108        setArguments(arguments);
109        customInitialization();
110    }
111
112    @Override
113    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
114    public <RAR accept(final GenericVisitor<RAvfinal A arg) {
115        return v.visit(this, arg);
116    }
117
118    @Override
119    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
120    public <Avoid accept(final VoidVisitor<Avfinal A arg) {
121        v.visit(this, arg);
122    }
123
124    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
125    public NodeList<ExpressiongetArguments() {
126        return arguments;
127    }
128
129    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
130    public SimpleName getName() {
131        return name;
132    }
133
134    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
135    public Optional<ExpressiongetScope() {
136        return Optional.ofNullable(scope);
137    }
138
139    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
140    public MethodCallExpr setArguments(final NodeList<Expressionarguments) {
141        assertNotNull(arguments);
142        if (arguments == this.arguments) {
143            return (MethodCallExpr) this;
144        }
145        notifyPropertyChange(ObservableProperty.ARGUMENTS, this.argumentsarguments);
146        if (this.arguments != null)
147            this.arguments.setParentNode(null);
148        this.arguments = arguments;
149        setAsParentNodeOf(arguments);
150        return this;
151    }
152
153    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
154    public MethodCallExpr setName(final SimpleName name) {
155        assertNotNull(name);
156        if (name == this.name) {
157            return (MethodCallExpr) this;
158        }
159        notifyPropertyChange(ObservableProperty.NAME, this.namename);
160        if (this.name != null)
161            this.name.setParentNode(null);
162        this.name = name;
163        setAsParentNodeOf(name);
164        return this;
165    }
166
167    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
168    public MethodCallExpr setScope(final Expression scope) {
169        if (scope == this.scope) {
170            return (MethodCallExpr) this;
171        }
172        notifyPropertyChange(ObservableProperty.SCOPE, this.scopescope);
173        if (this.scope != null)
174            this.scope.setParentNode(null);
175        this.scope = scope;
176        setAsParentNodeOf(scope);
177        return this;
178    }
179
180    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
181    public Optional<NodeList<Type>> getTypeArguments() {
182        return Optional.ofNullable(typeArguments);
183    }
184
185    /**
186     * Sets the typeArguments
187     *
188     * @param typeArguments the typeArguments, can be null
189     * @return this, the MethodCallExpr
190     */
191    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
192    public MethodCallExpr setTypeArguments(final NodeList<TypetypeArguments) {
193        if (typeArguments == this.typeArguments) {
194            return (MethodCallExpr) this;
195        }
196        notifyPropertyChange(ObservableProperty.TYPE_ARGUMENTS, this.typeArgumentstypeArguments);
197        if (this.typeArguments != null)
198            this.typeArguments.setParentNode(null);
199        this.typeArguments = typeArguments;
200        setAsParentNodeOf(typeArguments);
201        return this;
202    }
203
204    @Override
205    @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
206    public boolean remove(Node node) {
207        if (node == null)
208            return false;
209        for (int i = 0i < arguments.size(); i++) {
210            if (arguments.get(i) == node) {
211                arguments.remove(i);
212                return true;
213            }
214        }
215        if (scope != null) {
216            if (node == scope) {
217                removeScope();
218                return true;
219            }
220        }
221        if (typeArguments != null) {
222            for (int i = 0i < typeArguments.size(); i++) {
223                if (typeArguments.get(i) == node) {
224                    typeArguments.remove(i);
225                    return true;
226                }
227            }
228        }
229        return super.remove(node);
230    }
231
232    @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
233    public MethodCallExpr removeScope() {
234        return setScope((Expressionnull);
235    }
236
237    @Override
238    @Generated("com.github.javaparser.generator.core.node.CloneGenerator")
239    public MethodCallExpr clone() {
240        return (MethodCallExpraccept(new CloneVisitor(), null);
241    }
242
243    @Override
244    @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
245    public MethodCallExprMetaModel getMetaModel() {
246        return JavaParserMetaModel.methodCallExprMetaModel;
247    }
248
249    @Override
250    @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
251    public boolean replace(Node nodeNode replacementNode) {
252        if (node == null)
253            return false;
254        for (int i = 0i < arguments.size(); i++) {
255            if (arguments.get(i) == node) {
256                arguments.set(i, (ExpressionreplacementNode);
257                return true;
258            }
259        }
260        if (node == name) {
261            setName((SimpleNamereplacementNode);
262            return true;
263        }
264        if (scope != null) {
265            if (node == scope) {
266                setScope((ExpressionreplacementNode);
267                return true;
268            }
269        }
270        if (typeArguments != null) {
271            for (int i = 0i < typeArguments.size(); i++) {
272                if (typeArguments.get(i) == node) {
273                    typeArguments.set(i, (TypereplacementNode);
274                    return true;
275                }
276            }
277        }
278        return super.replace(nodereplacementNode);
279    }
280
281    @Override
282    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
283    public boolean isMethodCallExpr() {
284        return true;
285    }
286
287    @Override
288    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
289    public MethodCallExpr asMethodCallExpr() {
290        return this;
291    }
292
293    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
294    public void ifMethodCallExpr(Consumer<MethodCallExpraction) {
295        action.accept(this);
296    }
297
298    /**
299     * Attempts to resolve the declaration corresponding to the invoked method. If successful, a
300     * {@link ResolvedMethodDeclaration} representing the declaration of the constructor invoked by this
301     * {@code MethodCallExpr} is returned. Otherwise, an {@link UnsolvedSymbolException} is thrown.
302     *
303     * @return a {@link ResolvedMethodDeclaration} representing the declaration of the invoked method.
304     * @throws UnsolvedSymbolException if the declaration corresponding to the method call expression could not be
305     *                                 resolved.
306     * @see NameExpr#resolve()
307     * @see FieldAccessExpr#resolve()
308     * @see ObjectCreationExpr#resolve()
309     * @see ExplicitConstructorInvocationStmt#resolve()
310     */
311    @Override
312    public ResolvedMethodDeclaration resolve() {
313        return getSymbolResolver().resolveDeclaration(this, ResolvedMethodDeclaration.class);
314    }
315
316    @Override
317    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
318    public Optional<MethodCallExprtoMethodCallExpr() {
319        return Optional.of(this);
320    }
321}
322
MembersX
MethodCallExpr:removeScope
MethodCallExpr:ifMethodCallExpr
MethodCallExpr:setTypeArguments
MethodCallExpr:scope
MethodCallExpr:setScope
MethodCallExpr:resolve
MethodCallExpr:getScope
MethodCallExpr:getName
MethodCallExpr:remove
MethodCallExpr:name
MethodCallExpr:arguments
MethodCallExpr:setArguments
MethodCallExpr:getTypeArguments
MethodCallExpr:MethodCallExpr
MethodCallExpr:clone
MethodCallExpr:getArguments
MethodCallExpr:typeArguments
MethodCallExpr:replace
MethodCallExpr:toMethodCallExpr
MethodCallExpr:setName
MethodCallExpr:accept
MethodCallExpr:isMethodCallExpr
MethodCallExpr:asMethodCallExpr
MethodCallExpr:getMetaModel
Members
X