| 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.expr; |
| 22 | |
| 23 | import com.github.javaparser.ast.AllFieldsConstructor; |
| 24 | import com.github.javaparser.ast.NodeList; |
| 25 | import com.github.javaparser.ast.nodeTypes.NodeWithArguments; |
| 26 | import com.github.javaparser.ast.nodeTypes.NodeWithOptionalScope; |
| 27 | import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName; |
| 28 | import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments; |
| 29 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 30 | import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt; |
| 31 | import com.github.javaparser.ast.type.Type; |
| 32 | import com.github.javaparser.ast.visitor.GenericVisitor; |
| 33 | import com.github.javaparser.ast.visitor.VoidVisitor; |
| 34 | import java.util.Optional; |
| 35 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 36 | import com.github.javaparser.ast.Node; |
| 37 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 38 | import com.github.javaparser.metamodel.MethodCallExprMetaModel; |
| 39 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 40 | import com.github.javaparser.TokenRange; |
| 41 | import com.github.javaparser.metamodel.OptionalProperty; |
| 42 | import com.github.javaparser.resolution.Resolvable; |
| 43 | import com.github.javaparser.resolution.UnsolvedSymbolException; |
| 44 | import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration; |
| 45 | import java.util.function.Consumer; |
| 46 | import 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.<String>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 | */ |
| 54 | public 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<Type> typeArguments; |
| 61 | |
| 62 | private SimpleName name; |
| 63 | |
| 64 | private NodeList<Expression> arguments; |
| 65 | |
| 66 | public MethodCallExpr() { |
| 67 | this(null, null, null, new SimpleName(), new NodeList<>()); |
| 68 | } |
| 69 | |
| 70 | public MethodCallExpr(String name, Expression... arguments) { |
| 71 | this(null, null, null, new SimpleName(name), new NodeList<>(arguments)); |
| 72 | } |
| 73 | |
| 74 | public MethodCallExpr(final Expression scope, final String name) { |
| 75 | this(null, scope, null, new SimpleName(name), new NodeList<>()); |
| 76 | } |
| 77 | |
| 78 | public MethodCallExpr(final Expression scope, final SimpleName name) { |
| 79 | this(null, scope, null, name, new NodeList<>()); |
| 80 | } |
| 81 | |
| 82 | public MethodCallExpr(final Expression scope, final String name, final NodeList<Expression> arguments) { |
| 83 | this(null, scope, null, new SimpleName(name), arguments); |
| 84 | } |
| 85 | |
| 86 | public MethodCallExpr(final Expression scope, final NodeList<Type> typeArguments, final String name, final NodeList<Expression> arguments) { |
| 87 | this(null, scope, typeArguments, new SimpleName(name), arguments); |
| 88 | } |
| 89 | |
| 90 | public MethodCallExpr(final Expression scope, final SimpleName name, final NodeList<Expression> arguments) { |
| 91 | this(null, scope, null, name, arguments); |
| 92 | } |
| 93 | |
| 94 | @AllFieldsConstructor |
| 95 | public MethodCallExpr(final Expression scope, final NodeList<Type> typeArguments, final SimpleName name, final NodeList<Expression> arguments) { |
| 96 | this(null, scope, typeArguments, name, arguments); |
| 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 tokenRange, Expression scope, NodeList<Type> typeArguments, SimpleName name, NodeList<Expression> arguments) { |
| 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 <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 115 | return v.visit(this, arg); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 120 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 121 | v.visit(this, arg); |
| 122 | } |
| 123 | |
| 124 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 125 | public NodeList<Expression> getArguments() { |
| 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<Expression> getScope() { |
| 136 | return Optional.ofNullable(scope); |
| 137 | } |
| 138 | |
| 139 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 140 | public MethodCallExpr setArguments(final NodeList<Expression> arguments) { |
| 141 | assertNotNull(arguments); |
| 142 | if (arguments == this.arguments) { |
| 143 | return (MethodCallExpr) this; |
| 144 | } |
| 145 | notifyPropertyChange(ObservableProperty.ARGUMENTS, this.arguments, arguments); |
| 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.name, name); |
| 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.scope, scope); |
| 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<Type> typeArguments) { |
| 193 | if (typeArguments == this.typeArguments) { |
| 194 | return (MethodCallExpr) this; |
| 195 | } |
| 196 | notifyPropertyChange(ObservableProperty.TYPE_ARGUMENTS, this.typeArguments, typeArguments); |
| 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 = 0; i < 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 = 0; i < 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((Expression) null); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 239 | public MethodCallExpr clone() { |
| 240 | return (MethodCallExpr) accept(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 node, Node replacementNode) { |
| 252 | if (node == null) |
| 253 | return false; |
| 254 | for (int i = 0; i < arguments.size(); i++) { |
| 255 | if (arguments.get(i) == node) { |
| 256 | arguments.set(i, (Expression) replacementNode); |
| 257 | return true; |
| 258 | } |
| 259 | } |
| 260 | if (node == name) { |
| 261 | setName((SimpleName) replacementNode); |
| 262 | return true; |
| 263 | } |
| 264 | if (scope != null) { |
| 265 | if (node == scope) { |
| 266 | setScope((Expression) replacementNode); |
| 267 | return true; |
| 268 | } |
| 269 | } |
| 270 | if (typeArguments != null) { |
| 271 | for (int i = 0; i < typeArguments.size(); i++) { |
| 272 | if (typeArguments.get(i) == node) { |
| 273 | typeArguments.set(i, (Type) replacementNode); |
| 274 | return true; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | return super.replace(node, replacementNode); |
| 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<MethodCallExpr> action) { |
| 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<MethodCallExpr> toMethodCallExpr() { |
| 319 | return Optional.of(this); |
| 320 | } |
| 321 | } |
| 322 |
Members