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.nodeTypes.NodeWithSimpleName; |
25 | import com.github.javaparser.ast.observer.ObservableProperty; |
26 | import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt; |
27 | import com.github.javaparser.ast.visitor.GenericVisitor; |
28 | import com.github.javaparser.ast.visitor.VoidVisitor; |
29 | import static com.github.javaparser.utils.Utils.assertNotNull; |
30 | import com.github.javaparser.ast.Node; |
31 | import com.github.javaparser.ast.visitor.CloneVisitor; |
32 | import com.github.javaparser.metamodel.NameExprMetaModel; |
33 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
34 | import com.github.javaparser.TokenRange; |
35 | import com.github.javaparser.resolution.Resolvable; |
36 | import com.github.javaparser.resolution.UnsolvedSymbolException; |
37 | import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration; |
38 | import java.util.function.Consumer; |
39 | import java.util.Optional; |
40 | import com.github.javaparser.ast.Generated; |
41 | |
42 | /** |
43 | * Whenever a SimpleName is used in an expression, it is wrapped in NameExpr. |
44 | * <br>In {@code int x = a + 3;} a is a SimpleName inside a NameExpr. |
45 | * |
46 | * @author Julio Vilmar Gesser |
47 | */ |
48 | public class NameExpr extends Expression implements NodeWithSimpleName<NameExpr>, Resolvable<ResolvedValueDeclaration> { |
49 | |
50 | private SimpleName name; |
51 | |
52 | public NameExpr() { |
53 | this(null, new SimpleName()); |
54 | } |
55 | |
56 | public NameExpr(final String name) { |
57 | this(null, new SimpleName(name)); |
58 | } |
59 | |
60 | @AllFieldsConstructor |
61 | public NameExpr(final SimpleName name) { |
62 | this(name.getTokenRange().orElse(null), name); |
63 | setRange(name.getRange().orElse(null)); |
64 | } |
65 | |
66 | /** |
67 | * This constructor is used by the parser and is considered private. |
68 | */ |
69 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
70 | public NameExpr(TokenRange tokenRange, SimpleName name) { |
71 | super(tokenRange); |
72 | setName(name); |
73 | customInitialization(); |
74 | } |
75 | |
76 | @Override |
77 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
78 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
79 | return v.visit(this, arg); |
80 | } |
81 | |
82 | @Override |
83 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
84 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
85 | v.visit(this, arg); |
86 | } |
87 | |
88 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
89 | public SimpleName getName() { |
90 | return name; |
91 | } |
92 | |
93 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
94 | public NameExpr setName(final SimpleName name) { |
95 | assertNotNull(name); |
96 | if (name == this.name) { |
97 | return (NameExpr) this; |
98 | } |
99 | notifyPropertyChange(ObservableProperty.NAME, this.name, name); |
100 | if (this.name != null) |
101 | this.name.setParentNode(null); |
102 | this.name = name; |
103 | setAsParentNodeOf(name); |
104 | return this; |
105 | } |
106 | |
107 | @Override |
108 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
109 | public boolean remove(Node node) { |
110 | if (node == null) |
111 | return false; |
112 | return super.remove(node); |
113 | } |
114 | |
115 | @Override |
116 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
117 | public NameExpr clone() { |
118 | return (NameExpr) accept(new CloneVisitor(), null); |
119 | } |
120 | |
121 | @Override |
122 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
123 | public NameExprMetaModel getMetaModel() { |
124 | return JavaParserMetaModel.nameExprMetaModel; |
125 | } |
126 | |
127 | @Override |
128 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
129 | public boolean replace(Node node, Node replacementNode) { |
130 | if (node == null) |
131 | return false; |
132 | if (node == name) { |
133 | setName((SimpleName) replacementNode); |
134 | return true; |
135 | } |
136 | return super.replace(node, replacementNode); |
137 | } |
138 | |
139 | @Override |
140 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
141 | public boolean isNameExpr() { |
142 | return true; |
143 | } |
144 | |
145 | @Override |
146 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
147 | public NameExpr asNameExpr() { |
148 | return this; |
149 | } |
150 | |
151 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
152 | public void ifNameExpr(Consumer<NameExpr> action) { |
153 | action.accept(this); |
154 | } |
155 | |
156 | /** |
157 | * Attempts to resolve the declaration corresponding to the accessed name. If successful, a |
158 | * {@link ResolvedValueDeclaration} representing the declaration of the value accessed by this {@code NameExpr} is |
159 | * returned. Otherwise, an {@link UnsolvedSymbolException} is thrown. |
160 | * |
161 | * @return a {@link ResolvedValueDeclaration} representing the declaration of the accessed value. |
162 | * @throws UnsolvedSymbolException if the declaration corresponding to the name expression could not be resolved. |
163 | * @see FieldAccessExpr#resolve() |
164 | * @see MethodCallExpr#resolve() |
165 | * @see ObjectCreationExpr#resolve() |
166 | * @see ExplicitConstructorInvocationStmt#resolve() |
167 | */ |
168 | @Override |
169 | public ResolvedValueDeclaration resolve() { |
170 | return getSymbolResolver().resolveDeclaration(this, ResolvedValueDeclaration.class); |
171 | } |
172 | |
173 | @Override |
174 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
175 | public Optional<NameExpr> toNameExpr() { |
176 | return Optional.of(this); |
177 | } |
178 | } |
179 |
Members