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.TokenRange; |
24 | import com.github.javaparser.ast.AllFieldsConstructor; |
25 | import com.github.javaparser.ast.Generated; |
26 | import com.github.javaparser.ast.Node; |
27 | import com.github.javaparser.ast.NodeList; |
28 | import com.github.javaparser.ast.nodeTypes.NodeWithIdentifier; |
29 | import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments; |
30 | import com.github.javaparser.ast.observer.ObservableProperty; |
31 | import com.github.javaparser.ast.type.Type; |
32 | import com.github.javaparser.ast.visitor.CloneVisitor; |
33 | import com.github.javaparser.ast.visitor.GenericVisitor; |
34 | import com.github.javaparser.ast.visitor.VoidVisitor; |
35 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
36 | import com.github.javaparser.metamodel.MethodReferenceExprMetaModel; |
37 | import com.github.javaparser.metamodel.NonEmptyProperty; |
38 | import com.github.javaparser.metamodel.OptionalProperty; |
39 | import com.github.javaparser.resolution.Resolvable; |
40 | import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration; |
41 | import java.util.Optional; |
42 | import java.util.function.Consumer; |
43 | import static com.github.javaparser.utils.Utils.assertNonEmpty; |
44 | import static com.github.javaparser.utils.Utils.assertNotNull; |
45 | |
46 | /** |
47 | * Method reference expressions introduced in Java 8 specifically designed to simplify lambda Expressions. |
48 | * Note that the field "identifier", indicating the word to the right of the ::, is not always a method name, |
49 | * it can be "new". |
50 | * <br>In {@code System.out::println;} the scope is System.out and the identifier is "println" |
51 | * <br>{@code (test ? stream.map(String::trim) : stream)::toArray;} |
52 | * <br>In {@code Bar<String>::<Integer>new} the String type argument is on the scope, |
53 | * and the Integer type argument is on this MethodReferenceExpr. |
54 | * |
55 | * @author Raquel Pau |
56 | */ |
57 | public class MethodReferenceExpr extends Expression implements NodeWithTypeArguments<MethodReferenceExpr>, NodeWithIdentifier<MethodReferenceExpr>, Resolvable<ResolvedMethodDeclaration> { |
58 | |
59 | private Expression scope; |
60 | |
61 | @OptionalProperty |
62 | private NodeList<Type> typeArguments; |
63 | |
64 | @NonEmptyProperty |
65 | private String identifier; |
66 | |
67 | public MethodReferenceExpr() { |
68 | this(null, new ClassExpr(), null, "empty"); |
69 | } |
70 | |
71 | @AllFieldsConstructor |
72 | public MethodReferenceExpr(Expression scope, NodeList<Type> typeArguments, String identifier) { |
73 | this(null, scope, typeArguments, identifier); |
74 | } |
75 | |
76 | /** |
77 | * This constructor is used by the parser and is considered private. |
78 | */ |
79 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
80 | public MethodReferenceExpr(TokenRange tokenRange, Expression scope, NodeList<Type> typeArguments, String identifier) { |
81 | super(tokenRange); |
82 | setScope(scope); |
83 | setTypeArguments(typeArguments); |
84 | setIdentifier(identifier); |
85 | customInitialization(); |
86 | } |
87 | |
88 | @Override |
89 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
90 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
91 | return v.visit(this, arg); |
92 | } |
93 | |
94 | @Override |
95 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
96 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
97 | v.visit(this, arg); |
98 | } |
99 | |
100 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
101 | public Expression getScope() { |
102 | return scope; |
103 | } |
104 | |
105 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
106 | public MethodReferenceExpr setScope(final Expression scope) { |
107 | assertNotNull(scope); |
108 | if (scope == this.scope) { |
109 | return (MethodReferenceExpr) this; |
110 | } |
111 | notifyPropertyChange(ObservableProperty.SCOPE, this.scope, scope); |
112 | if (this.scope != null) |
113 | this.scope.setParentNode(null); |
114 | this.scope = scope; |
115 | setAsParentNodeOf(scope); |
116 | return this; |
117 | } |
118 | |
119 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
120 | public Optional<NodeList<Type>> getTypeArguments() { |
121 | return Optional.ofNullable(typeArguments); |
122 | } |
123 | |
124 | /** |
125 | * Sets the typeArguments |
126 | * |
127 | * @param typeArguments the typeArguments, can be null |
128 | * @return this, the MethodReferenceExpr |
129 | */ |
130 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
131 | public MethodReferenceExpr setTypeArguments(final NodeList<Type> typeArguments) { |
132 | if (typeArguments == this.typeArguments) { |
133 | return (MethodReferenceExpr) this; |
134 | } |
135 | notifyPropertyChange(ObservableProperty.TYPE_ARGUMENTS, this.typeArguments, typeArguments); |
136 | if (this.typeArguments != null) |
137 | this.typeArguments.setParentNode(null); |
138 | this.typeArguments = typeArguments; |
139 | setAsParentNodeOf(typeArguments); |
140 | return this; |
141 | } |
142 | |
143 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
144 | public String getIdentifier() { |
145 | return identifier; |
146 | } |
147 | |
148 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
149 | public MethodReferenceExpr setIdentifier(final String identifier) { |
150 | assertNonEmpty(identifier); |
151 | if (identifier == this.identifier) { |
152 | return (MethodReferenceExpr) this; |
153 | } |
154 | notifyPropertyChange(ObservableProperty.IDENTIFIER, this.identifier, identifier); |
155 | this.identifier = identifier; |
156 | return this; |
157 | } |
158 | |
159 | @Override |
160 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
161 | public boolean remove(Node node) { |
162 | if (node == null) |
163 | return false; |
164 | if (typeArguments != null) { |
165 | for (int i = 0; i < typeArguments.size(); i++) { |
166 | if (typeArguments.get(i) == node) { |
167 | typeArguments.remove(i); |
168 | return true; |
169 | } |
170 | } |
171 | } |
172 | return super.remove(node); |
173 | } |
174 | |
175 | @Override |
176 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
177 | public MethodReferenceExpr clone() { |
178 | return (MethodReferenceExpr) accept(new CloneVisitor(), null); |
179 | } |
180 | |
181 | @Override |
182 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
183 | public MethodReferenceExprMetaModel getMetaModel() { |
184 | return JavaParserMetaModel.methodReferenceExprMetaModel; |
185 | } |
186 | |
187 | @Override |
188 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
189 | public boolean replace(Node node, Node replacementNode) { |
190 | if (node == null) |
191 | return false; |
192 | if (node == scope) { |
193 | setScope((Expression) replacementNode); |
194 | return true; |
195 | } |
196 | if (typeArguments != null) { |
197 | for (int i = 0; i < typeArguments.size(); i++) { |
198 | if (typeArguments.get(i) == node) { |
199 | typeArguments.set(i, (Type) replacementNode); |
200 | return true; |
201 | } |
202 | } |
203 | } |
204 | return super.replace(node, replacementNode); |
205 | } |
206 | |
207 | @Override |
208 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
209 | public boolean isMethodReferenceExpr() { |
210 | return true; |
211 | } |
212 | |
213 | @Override |
214 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
215 | public MethodReferenceExpr asMethodReferenceExpr() { |
216 | return this; |
217 | } |
218 | |
219 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
220 | public void ifMethodReferenceExpr(Consumer<MethodReferenceExpr> action) { |
221 | action.accept(this); |
222 | } |
223 | |
224 | @Override |
225 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
226 | public Optional<MethodReferenceExpr> toMethodReferenceExpr() { |
227 | return Optional.of(this); |
228 | } |
229 | |
230 | /** |
231 | * @return the method declaration this method reference is referencing. |
232 | */ |
233 | @Override |
234 | public ResolvedMethodDeclaration resolve() { |
235 | return getSymbolResolver().resolveDeclaration(this, ResolvedMethodDeclaration.class); |
236 | } |
237 | } |
238 |
Members