| 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.NodeWithScope; |
| 26 | import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName; |
| 27 | import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments; |
| 28 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 29 | import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt; |
| 30 | import com.github.javaparser.ast.type.Type; |
| 31 | import com.github.javaparser.ast.visitor.GenericVisitor; |
| 32 | import com.github.javaparser.ast.visitor.VoidVisitor; |
| 33 | import java.util.Optional; |
| 34 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 35 | import com.github.javaparser.ast.Node; |
| 36 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 37 | import com.github.javaparser.metamodel.FieldAccessExprMetaModel; |
| 38 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 39 | import com.github.javaparser.TokenRange; |
| 40 | import com.github.javaparser.metamodel.OptionalProperty; |
| 41 | import com.github.javaparser.resolution.Resolvable; |
| 42 | import com.github.javaparser.resolution.UnsolvedSymbolException; |
| 43 | import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration; |
| 44 | import java.util.function.Consumer; |
| 45 | import com.github.javaparser.ast.Generated; |
| 46 | |
| 47 | /** |
| 48 | * Access of a field of an object or a class. |
| 49 | * <br>In {@code person.name} "name" is the name and "person" is the scope. |
| 50 | * |
| 51 | * @author Julio Vilmar Gesser |
| 52 | */ |
| 53 | public class FieldAccessExpr extends Expression implements NodeWithSimpleName<FieldAccessExpr>, NodeWithTypeArguments<FieldAccessExpr>, NodeWithScope<FieldAccessExpr>, Resolvable<ResolvedValueDeclaration> { |
| 54 | |
| 55 | private Expression scope; |
| 56 | |
| 57 | @OptionalProperty |
| 58 | private NodeList<Type> typeArguments; |
| 59 | |
| 60 | private SimpleName name; |
| 61 | |
| 62 | public FieldAccessExpr() { |
| 63 | this(null, new ThisExpr(), null, new SimpleName()); |
| 64 | } |
| 65 | |
| 66 | public FieldAccessExpr(final Expression scope, final String name) { |
| 67 | this(null, scope, null, new SimpleName(name)); |
| 68 | } |
| 69 | |
| 70 | @AllFieldsConstructor |
| 71 | public FieldAccessExpr(final Expression scope, final NodeList<Type> typeArguments, final SimpleName name) { |
| 72 | this(null, scope, typeArguments, name); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * This constructor is used by the parser and is considered private. |
| 77 | */ |
| 78 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 79 | public FieldAccessExpr(TokenRange tokenRange, Expression scope, NodeList<Type> typeArguments, SimpleName name) { |
| 80 | super(tokenRange); |
| 81 | setScope(scope); |
| 82 | setTypeArguments(typeArguments); |
| 83 | setName(name); |
| 84 | customInitialization(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 89 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 90 | return v.visit(this, arg); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 95 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 96 | v.visit(this, arg); |
| 97 | } |
| 98 | |
| 99 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 100 | public SimpleName getName() { |
| 101 | return name; |
| 102 | } |
| 103 | |
| 104 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 105 | public FieldAccessExpr setName(final SimpleName name) { |
| 106 | assertNotNull(name); |
| 107 | if (name == this.name) { |
| 108 | return (FieldAccessExpr) this; |
| 109 | } |
| 110 | notifyPropertyChange(ObservableProperty.NAME, this.name, name); |
| 111 | if (this.name != null) |
| 112 | this.name.setParentNode(null); |
| 113 | this.name = name; |
| 114 | setAsParentNodeOf(name); |
| 115 | return this; |
| 116 | } |
| 117 | |
| 118 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 119 | public Expression getScope() { |
| 120 | return scope; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Sets the scope |
| 125 | * |
| 126 | * @param scope the scope, can not be null |
| 127 | * @return this, the FieldAccessExpr |
| 128 | */ |
| 129 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 130 | public FieldAccessExpr setScope(final Expression scope) { |
| 131 | assertNotNull(scope); |
| 132 | if (scope == this.scope) { |
| 133 | return (FieldAccessExpr) this; |
| 134 | } |
| 135 | notifyPropertyChange(ObservableProperty.SCOPE, this.scope, scope); |
| 136 | if (this.scope != null) |
| 137 | this.scope.setParentNode(null); |
| 138 | this.scope = scope; |
| 139 | setAsParentNodeOf(scope); |
| 140 | return this; |
| 141 | } |
| 142 | |
| 143 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 144 | public Optional<NodeList<Type>> getTypeArguments() { |
| 145 | return Optional.ofNullable(typeArguments); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Sets the type arguments |
| 150 | * |
| 151 | * @param typeArguments the type arguments, can be null |
| 152 | * @return this, the FieldAccessExpr |
| 153 | */ |
| 154 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 155 | public FieldAccessExpr setTypeArguments(final NodeList<Type> typeArguments) { |
| 156 | if (typeArguments == this.typeArguments) { |
| 157 | return (FieldAccessExpr) this; |
| 158 | } |
| 159 | notifyPropertyChange(ObservableProperty.TYPE_ARGUMENTS, this.typeArguments, typeArguments); |
| 160 | if (this.typeArguments != null) |
| 161 | this.typeArguments.setParentNode(null); |
| 162 | this.typeArguments = typeArguments; |
| 163 | setAsParentNodeOf(typeArguments); |
| 164 | return this; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 169 | public FieldAccessExpr clone() { |
| 170 | return (FieldAccessExpr) accept(new CloneVisitor(), null); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 175 | public FieldAccessExprMetaModel getMetaModel() { |
| 176 | return JavaParserMetaModel.fieldAccessExprMetaModel; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 181 | public boolean remove(Node node) { |
| 182 | if (node == null) |
| 183 | return false; |
| 184 | if (typeArguments != null) { |
| 185 | for (int i = 0; i < typeArguments.size(); i++) { |
| 186 | if (typeArguments.get(i) == node) { |
| 187 | typeArguments.remove(i); |
| 188 | return true; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | return super.remove(node); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 197 | public boolean replace(Node node, Node replacementNode) { |
| 198 | if (node == null) |
| 199 | return false; |
| 200 | if (node == name) { |
| 201 | setName((SimpleName) replacementNode); |
| 202 | return true; |
| 203 | } |
| 204 | if (node == scope) { |
| 205 | setScope((Expression) replacementNode); |
| 206 | return true; |
| 207 | } |
| 208 | if (typeArguments != null) { |
| 209 | for (int i = 0; i < typeArguments.size(); i++) { |
| 210 | if (typeArguments.get(i) == node) { |
| 211 | typeArguments.set(i, (Type) replacementNode); |
| 212 | return true; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | return super.replace(node, replacementNode); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 221 | public boolean isFieldAccessExpr() { |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 227 | public FieldAccessExpr asFieldAccessExpr() { |
| 228 | return this; |
| 229 | } |
| 230 | |
| 231 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 232 | public void ifFieldAccessExpr(Consumer<FieldAccessExpr> action) { |
| 233 | action.accept(this); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Attempts to resolve the declaration corresponding to the accessed field. If successful, a |
| 238 | * {@link ResolvedValueDeclaration} representing the declaration of the value accessed by this |
| 239 | * {@code FieldAccessExpr} is returned. Otherwise, an {@link UnsolvedSymbolException} is thrown. |
| 240 | * |
| 241 | * @return a {@link ResolvedValueDeclaration} representing the declaration of the accessed value. |
| 242 | * @throws UnsolvedSymbolException if the declaration corresponding to the field access expression could not be |
| 243 | * resolved. |
| 244 | * @see NameExpr#resolve() |
| 245 | * @see MethodCallExpr#resolve() |
| 246 | * @see ObjectCreationExpr#resolve() |
| 247 | * @see ExplicitConstructorInvocationStmt#resolve() |
| 248 | */ |
| 249 | @Override |
| 250 | public ResolvedValueDeclaration resolve() { |
| 251 | return getSymbolResolver().resolveDeclaration(this, ResolvedValueDeclaration.class); |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 256 | public Optional<FieldAccessExpr> toFieldAccessExpr() { |
| 257 | return Optional.of(this); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Indicate if this FieldAccessExpr is an element directly contained in a larger FieldAccessExpr. |
| 262 | */ |
| 263 | public boolean isInternal() { |
| 264 | return this.getParentNode().isPresent() && this.getParentNode().get() instanceof FieldAccessExpr; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Indicate if this FieldAccessExpr is top level, i.e., it is not directly contained in a larger FieldAccessExpr. |
| 269 | */ |
| 270 | public boolean isTopLevel() { |
| 271 | return !isInternal(); |
| 272 | } |
| 273 | } |
| 274 |
Members