| 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.body.BodyDeclaration; |
| 26 | import com.github.javaparser.ast.nodeTypes.NodeWithArguments; |
| 27 | import com.github.javaparser.ast.nodeTypes.NodeWithOptionalScope; |
| 28 | import com.github.javaparser.ast.nodeTypes.NodeWithType; |
| 29 | import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments; |
| 30 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 31 | import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt; |
| 32 | import com.github.javaparser.ast.type.ClassOrInterfaceType; |
| 33 | import com.github.javaparser.ast.type.Type; |
| 34 | import com.github.javaparser.ast.visitor.GenericVisitor; |
| 35 | import com.github.javaparser.ast.visitor.VoidVisitor; |
| 36 | import java.util.Optional; |
| 37 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 38 | import com.github.javaparser.ast.Node; |
| 39 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 40 | import com.github.javaparser.metamodel.ObjectCreationExprMetaModel; |
| 41 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 42 | import com.github.javaparser.TokenRange; |
| 43 | import com.github.javaparser.metamodel.OptionalProperty; |
| 44 | import com.github.javaparser.resolution.Resolvable; |
| 45 | import com.github.javaparser.resolution.UnsolvedSymbolException; |
| 46 | import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration; |
| 47 | import java.util.function.Consumer; |
| 48 | import com.github.javaparser.ast.Generated; |
| 49 | |
| 50 | /** |
| 51 | * A constructor call. |
| 52 | * <br>In {@code new HashMap.Entry<String, Long>(15) {public String getKey() {return null;}};} |
| 53 | * HashMap.Entry is the type, String and Long are type arguments, 15 is an argument, and everything in { } |
| 54 | * is the anonymous class body. |
| 55 | * <p/>In {@code class B { class C { public void a() { new B().new C(); } } }} the scope is {@code new B()} |
| 56 | * of ObjectCreationExpr {@code new B().new C()} |
| 57 | * |
| 58 | * @author Julio Vilmar Gesser |
| 59 | */ |
| 60 | public class ObjectCreationExpr extends Expression implements NodeWithTypeArguments<ObjectCreationExpr>, NodeWithType<ObjectCreationExpr, ClassOrInterfaceType>, NodeWithArguments<ObjectCreationExpr>, NodeWithOptionalScope<ObjectCreationExpr>, Resolvable<ResolvedConstructorDeclaration> { |
| 61 | |
| 62 | @OptionalProperty |
| 63 | private Expression scope; |
| 64 | |
| 65 | private ClassOrInterfaceType type; |
| 66 | |
| 67 | @OptionalProperty |
| 68 | private NodeList<Type> typeArguments; |
| 69 | |
| 70 | private NodeList<Expression> arguments; |
| 71 | |
| 72 | @OptionalProperty |
| 73 | private NodeList<BodyDeclaration<?>> anonymousClassBody; |
| 74 | |
| 75 | public ObjectCreationExpr() { |
| 76 | this(null, null, new ClassOrInterfaceType(), new NodeList<>(), new NodeList<>(), null); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Defines a call to a constructor. |
| 81 | * |
| 82 | * @param scope may be null |
| 83 | * @param type this is the class that the constructor is being called for. |
| 84 | * @param arguments Any arguments to pass to the constructor |
| 85 | */ |
| 86 | public ObjectCreationExpr(final Expression scope, final ClassOrInterfaceType type, final NodeList<Expression> arguments) { |
| 87 | this(null, scope, type, null, arguments, null); |
| 88 | } |
| 89 | |
| 90 | @AllFieldsConstructor |
| 91 | public ObjectCreationExpr(final Expression scope, final ClassOrInterfaceType type, final NodeList<Type> typeArguments, final NodeList<Expression> arguments, final NodeList<BodyDeclaration<?>> anonymousClassBody) { |
| 92 | this(null, scope, type, typeArguments, arguments, anonymousClassBody); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * This constructor is used by the parser and is considered private. |
| 97 | */ |
| 98 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 99 | public ObjectCreationExpr(TokenRange tokenRange, Expression scope, ClassOrInterfaceType type, NodeList<Type> typeArguments, NodeList<Expression> arguments, NodeList<BodyDeclaration<?>> anonymousClassBody) { |
| 100 | super(tokenRange); |
| 101 | setScope(scope); |
| 102 | setType(type); |
| 103 | setTypeArguments(typeArguments); |
| 104 | setArguments(arguments); |
| 105 | setAnonymousClassBody(anonymousClassBody); |
| 106 | customInitialization(); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 111 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 112 | return v.visit(this, arg); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 117 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 118 | v.visit(this, arg); |
| 119 | } |
| 120 | |
| 121 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 122 | public Optional<NodeList<BodyDeclaration<?>>> getAnonymousClassBody() { |
| 123 | return Optional.ofNullable(anonymousClassBody); |
| 124 | } |
| 125 | |
| 126 | public void addAnonymousClassBody(BodyDeclaration<?> body) { |
| 127 | if (anonymousClassBody == null) |
| 128 | anonymousClassBody = new NodeList<>(); |
| 129 | anonymousClassBody.add(body); |
| 130 | } |
| 131 | |
| 132 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 133 | public NodeList<Expression> getArguments() { |
| 134 | return arguments; |
| 135 | } |
| 136 | |
| 137 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 138 | public Optional<Expression> getScope() { |
| 139 | return Optional.ofNullable(scope); |
| 140 | } |
| 141 | |
| 142 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 143 | public ClassOrInterfaceType getType() { |
| 144 | return type; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Sets the anonymousClassBody<br> |
| 149 | * Null means no class body<br> |
| 150 | * Empty NodeList means new ClassName(){ } |
| 151 | * |
| 152 | * @param anonymousClassBody the anonymousClassBody, can be null or empty |
| 153 | * @return this, the ObjectCreationExpr |
| 154 | */ |
| 155 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 156 | public ObjectCreationExpr setAnonymousClassBody(final NodeList<BodyDeclaration<?>> anonymousClassBody) { |
| 157 | if (anonymousClassBody == this.anonymousClassBody) { |
| 158 | return (ObjectCreationExpr) this; |
| 159 | } |
| 160 | notifyPropertyChange(ObservableProperty.ANONYMOUS_CLASS_BODY, this.anonymousClassBody, anonymousClassBody); |
| 161 | if (this.anonymousClassBody != null) |
| 162 | this.anonymousClassBody.setParentNode(null); |
| 163 | this.anonymousClassBody = anonymousClassBody; |
| 164 | setAsParentNodeOf(anonymousClassBody); |
| 165 | return this; |
| 166 | } |
| 167 | |
| 168 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 169 | public ObjectCreationExpr setArguments(final NodeList<Expression> arguments) { |
| 170 | assertNotNull(arguments); |
| 171 | if (arguments == this.arguments) { |
| 172 | return (ObjectCreationExpr) this; |
| 173 | } |
| 174 | notifyPropertyChange(ObservableProperty.ARGUMENTS, this.arguments, arguments); |
| 175 | if (this.arguments != null) |
| 176 | this.arguments.setParentNode(null); |
| 177 | this.arguments = arguments; |
| 178 | setAsParentNodeOf(arguments); |
| 179 | return this; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Sets the scope |
| 184 | * |
| 185 | * @param scope the scope, can be null |
| 186 | * @return this, the ObjectCreationExpr |
| 187 | */ |
| 188 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 189 | public ObjectCreationExpr setScope(final Expression scope) { |
| 190 | if (scope == this.scope) { |
| 191 | return (ObjectCreationExpr) this; |
| 192 | } |
| 193 | notifyPropertyChange(ObservableProperty.SCOPE, this.scope, scope); |
| 194 | if (this.scope != null) |
| 195 | this.scope.setParentNode(null); |
| 196 | this.scope = scope; |
| 197 | setAsParentNodeOf(scope); |
| 198 | return this; |
| 199 | } |
| 200 | |
| 201 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 202 | public ObjectCreationExpr setType(final ClassOrInterfaceType type) { |
| 203 | assertNotNull(type); |
| 204 | if (type == this.type) { |
| 205 | return (ObjectCreationExpr) this; |
| 206 | } |
| 207 | notifyPropertyChange(ObservableProperty.TYPE, this.type, type); |
| 208 | if (this.type != null) |
| 209 | this.type.setParentNode(null); |
| 210 | this.type = type; |
| 211 | setAsParentNodeOf(type); |
| 212 | return this; |
| 213 | } |
| 214 | |
| 215 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 216 | public Optional<NodeList<Type>> getTypeArguments() { |
| 217 | return Optional.ofNullable(typeArguments); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Sets the typeArguments |
| 222 | * |
| 223 | * @param typeArguments the typeArguments, can be null |
| 224 | * @return this, the ObjectCreationExpr |
| 225 | */ |
| 226 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 227 | public ObjectCreationExpr setTypeArguments(final NodeList<Type> typeArguments) { |
| 228 | if (typeArguments == this.typeArguments) { |
| 229 | return (ObjectCreationExpr) this; |
| 230 | } |
| 231 | notifyPropertyChange(ObservableProperty.TYPE_ARGUMENTS, this.typeArguments, typeArguments); |
| 232 | if (this.typeArguments != null) |
| 233 | this.typeArguments.setParentNode(null); |
| 234 | this.typeArguments = typeArguments; |
| 235 | setAsParentNodeOf(typeArguments); |
| 236 | return this; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 241 | public boolean remove(Node node) { |
| 242 | if (node == null) |
| 243 | return false; |
| 244 | if (anonymousClassBody != null) { |
| 245 | for (int i = 0; i < anonymousClassBody.size(); i++) { |
| 246 | if (anonymousClassBody.get(i) == node) { |
| 247 | anonymousClassBody.remove(i); |
| 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | for (int i = 0; i < arguments.size(); i++) { |
| 253 | if (arguments.get(i) == node) { |
| 254 | arguments.remove(i); |
| 255 | return true; |
| 256 | } |
| 257 | } |
| 258 | if (scope != null) { |
| 259 | if (node == scope) { |
| 260 | removeScope(); |
| 261 | return true; |
| 262 | } |
| 263 | } |
| 264 | if (typeArguments != null) { |
| 265 | for (int i = 0; i < typeArguments.size(); i++) { |
| 266 | if (typeArguments.get(i) == node) { |
| 267 | typeArguments.remove(i); |
| 268 | return true; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | return super.remove(node); |
| 273 | } |
| 274 | |
| 275 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 276 | public ObjectCreationExpr removeScope() { |
| 277 | return setScope((Expression) null); |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 282 | public ObjectCreationExpr clone() { |
| 283 | return (ObjectCreationExpr) accept(new CloneVisitor(), null); |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 288 | public ObjectCreationExprMetaModel getMetaModel() { |
| 289 | return JavaParserMetaModel.objectCreationExprMetaModel; |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 294 | public boolean replace(Node node, Node replacementNode) { |
| 295 | if (node == null) |
| 296 | return false; |
| 297 | if (anonymousClassBody != null) { |
| 298 | for (int i = 0; i < anonymousClassBody.size(); i++) { |
| 299 | if (anonymousClassBody.get(i) == node) { |
| 300 | anonymousClassBody.set(i, (BodyDeclaration) replacementNode); |
| 301 | return true; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | for (int i = 0; i < arguments.size(); i++) { |
| 306 | if (arguments.get(i) == node) { |
| 307 | arguments.set(i, (Expression) replacementNode); |
| 308 | return true; |
| 309 | } |
| 310 | } |
| 311 | if (scope != null) { |
| 312 | if (node == scope) { |
| 313 | setScope((Expression) replacementNode); |
| 314 | return true; |
| 315 | } |
| 316 | } |
| 317 | if (node == type) { |
| 318 | setType((ClassOrInterfaceType) replacementNode); |
| 319 | return true; |
| 320 | } |
| 321 | if (typeArguments != null) { |
| 322 | for (int i = 0; i < typeArguments.size(); i++) { |
| 323 | if (typeArguments.get(i) == node) { |
| 324 | typeArguments.set(i, (Type) replacementNode); |
| 325 | return true; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | return super.replace(node, replacementNode); |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 334 | public boolean isObjectCreationExpr() { |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 340 | public ObjectCreationExpr asObjectCreationExpr() { |
| 341 | return this; |
| 342 | } |
| 343 | |
| 344 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 345 | public void ifObjectCreationExpr(Consumer<ObjectCreationExpr> action) { |
| 346 | action.accept(this); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Attempts to resolve the declaration corresponding to the invoked constructor. If successful, a |
| 351 | * {@link ResolvedConstructorDeclaration} representing the declaration of the constructor invoked by this |
| 352 | * {@code ObjectCreationExpr} is returned. Otherwise, an {@link UnsolvedSymbolException} is thrown. |
| 353 | * |
| 354 | * @return a {@link ResolvedConstructorDeclaration} representing the declaration of the invoked constructor. |
| 355 | * @throws UnsolvedSymbolException if the declaration corresponding to the object creation expression could not be |
| 356 | * resolved. |
| 357 | * @see NameExpr#resolve() |
| 358 | * @see FieldAccessExpr#resolve() |
| 359 | * @see MethodCallExpr#resolve() |
| 360 | * @see ExplicitConstructorInvocationStmt#resolve() |
| 361 | */ |
| 362 | @Override |
| 363 | public ResolvedConstructorDeclaration resolve() { |
| 364 | return getSymbolResolver().resolveDeclaration(this, ResolvedConstructorDeclaration.class); |
| 365 | } |
| 366 | |
| 367 | @Override |
| 368 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 369 | public Optional<ObjectCreationExpr> toObjectCreationExpr() { |
| 370 | return Optional.of(this); |
| 371 | } |
| 372 | } |
| 373 |
Members