-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
JavaParserInterfaceDeclaration.java
405 lines (347 loc) · 15.1 KB
/
JavaParserInterfaceDeclaration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2020 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver.javaparsermodel.declarations;
import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.BodyDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.resolution.Context;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.declarations.*;
import com.github.javaparser.resolution.logic.MethodResolutionCapability;
import com.github.javaparser.resolution.model.SymbolReference;
import com.github.javaparser.resolution.model.typesystem.LazyType;
import com.github.javaparser.resolution.model.typesystem.ReferenceTypeImpl;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.core.resolution.MethodUsageResolutionCapability;
import com.github.javaparser.symbolsolver.core.resolution.SymbolResolutionCapability;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory;
import com.github.javaparser.symbolsolver.logic.AbstractTypeDeclaration;
import com.github.javaparser.symbolsolver.resolution.SymbolSolver;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Federico Tomassetti
*/
public class JavaParserInterfaceDeclaration extends AbstractTypeDeclaration
implements ResolvedInterfaceDeclaration, MethodResolutionCapability, MethodUsageResolutionCapability,
SymbolResolutionCapability {
private TypeSolver typeSolver;
private ClassOrInterfaceDeclaration wrappedNode;
private JavaParserTypeAdapter<ClassOrInterfaceDeclaration> javaParserTypeAdapter;
public JavaParserInterfaceDeclaration(ClassOrInterfaceDeclaration wrappedNode, TypeSolver typeSolver) {
if (!wrappedNode.isInterface()) {
throw new IllegalArgumentException();
}
this.wrappedNode = wrappedNode;
this.typeSolver = typeSolver;
this.javaParserTypeAdapter = new JavaParserTypeAdapter<>(wrappedNode, typeSolver);
}
@Override
public Set<ResolvedMethodDeclaration> getDeclaredMethods() {
Set<ResolvedMethodDeclaration> methods = new HashSet<>();
for (BodyDeclaration<?> member : wrappedNode.getMembers()) {
if (member instanceof com.github.javaparser.ast.body.MethodDeclaration) {
methods.add(new JavaParserMethodDeclaration((com.github.javaparser.ast.body.MethodDeclaration) member, typeSolver));
}
}
return methods;
}
public Context getContext() {
return JavaParserFactory.getContext(wrappedNode, typeSolver);
}
public ResolvedType getUsage(Node node) {
throw new UnsupportedOperationException();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JavaParserInterfaceDeclaration that = (JavaParserInterfaceDeclaration) o;
if (!wrappedNode.equals(that.wrappedNode)) return false;
return true;
}
@Override
public int hashCode() {
return wrappedNode.hashCode();
}
@Override
public String getName() {
return wrappedNode.getName().getId();
}
@Override
public ResolvedInterfaceDeclaration asInterface() {
return this;
}
@Override
public boolean hasDirectlyAnnotation(String canonicalName) {
return AstResolutionUtils.hasDirectlyAnnotation(wrappedNode, typeSolver, canonicalName);
}
@Override
public boolean isInterface() {
return true;
}
@Override
public List<ResolvedReferenceType> getInterfacesExtended() {
List<ResolvedReferenceType> interfaces = new ArrayList<>();
for (ClassOrInterfaceType t : wrappedNode.getExtendedTypes()) {
interfaces.add(new ReferenceTypeImpl(
solveType(t.getName().getId()).getCorrespondingDeclaration().asInterface()));
}
return interfaces;
}
@Override
public String getPackageName() {
return javaParserTypeAdapter.getPackageName();
}
@Override
public String getClassName() {
return javaParserTypeAdapter.getClassName();
}
@Override
public String getQualifiedName() {
return javaParserTypeAdapter.getQualifiedName();
}
@Override
public boolean isAssignableBy(ResolvedReferenceTypeDeclaration other) {
return javaParserTypeAdapter.isAssignableBy(other);
}
@Override
public boolean isAssignableBy(ResolvedType type) {
return javaParserTypeAdapter.isAssignableBy(type);
}
@Override
public boolean canBeAssignedTo(ResolvedReferenceTypeDeclaration other) {
// TODO consider generic types
if (this.getQualifiedName().equals(other.getQualifiedName())) {
return true;
}
if (this.wrappedNode.getExtendedTypes() != null) {
for (ClassOrInterfaceType type : wrappedNode.getExtendedTypes()) {
ResolvedReferenceTypeDeclaration ancestor = (ResolvedReferenceTypeDeclaration) new SymbolSolver(typeSolver).solveType(type);
if (ancestor.canBeAssignedTo(other)) {
return true;
}
}
}
// TODO FIXME: Remove null check -- should be an empty list...
if (this.wrappedNode.getImplementedTypes() != null) {
for (ClassOrInterfaceType type : wrappedNode.getImplementedTypes()) {
ResolvedReferenceTypeDeclaration ancestor = (ResolvedReferenceTypeDeclaration) new SymbolSolver(typeSolver).solveType(type);
if (ancestor.canBeAssignedTo(other)) {
return true;
}
}
}
return false;
}
@Override
public boolean isTypeParameter() {
return false;
}
@Override
public List<ResolvedFieldDeclaration> getAllFields() {
List<ResolvedFieldDeclaration> fields = javaParserTypeAdapter.getFieldsForDeclaredVariables();
getAncestors()
.stream()
.filter(ancestor -> ancestor.getTypeDeclaration().isPresent())
.forEach(ancestor -> ancestor.getTypeDeclaration().get()
.getAllFields()
.forEach(f -> {
fields.add(new ResolvedFieldDeclaration() {
@Override
public AccessSpecifier accessSpecifier() {
return f.accessSpecifier();
}
@Override
public String getName() {
return f.getName();
}
@Override
public ResolvedType getType() {
return ancestor.useThisTypeParametersOnTheGivenType(f.getType());
}
@Override
public boolean isStatic() {
return f.isStatic();
}
@Override
public boolean isVolatile() {
return f.isVolatile();
}
@Override
public ResolvedTypeDeclaration declaringType() {
return f.declaringType();
}
@Override
public Optional<Node> toAst() {
return f.toAst();
}
});
})
);
return fields;
}
@Override
public String toString() {
return "JavaParserInterfaceDeclaration{" +
"wrappedNode=" + wrappedNode +
'}';
}
/**
* This method is deprecated because it receives the TypesSolver as a parameter.
* Eventually we would like to remove all usages of TypeSolver as a parameter.
*
* Also, resolution should move out of declarations, so that they are pure declarations and the resolution should
* work for JavaParser, Reflection and Javassist classes in the same way and not be specific to the three
* implementations.
*/
@Deprecated
public SymbolReference<ResolvedTypeDeclaration> solveType(String name) {
if (this.wrappedNode.getName().getId().equals(name)) {
return SymbolReference.solved(this);
}
SymbolReference<ResolvedTypeDeclaration> ref = javaParserTypeAdapter.solveType(name);
if (ref.isSolved()) {
return ref;
}
String prefix = wrappedNode.getName().asString() + ".";
if (name.startsWith(prefix) && name.length() > prefix.length()) {
return new JavaParserInterfaceDeclaration(this.wrappedNode, typeSolver).solveType(name.substring(prefix.length()));
}
return getContext().getParent()
.orElseThrow(() -> new RuntimeException("Parent context unexpectedly empty."))
.solveType(name);
}
@Override
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes,
boolean staticOnly) {
return getContext().solveMethod(name, argumentsTypes, staticOnly);
}
@Override
public Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> argumentTypes,
Context invocationContext, List<ResolvedType> typeParameters) {
return getContext().solveMethodAsUsage(name, argumentTypes);
}
@Override
public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) {
return getContext().solveSymbol(name);
}
@Override
public List<ResolvedReferenceType> getAncestors(boolean acceptIncompleteList) {
List<ResolvedReferenceType> ancestors = new ArrayList<>();
if (wrappedNode.getExtendedTypes() != null) {
for (ClassOrInterfaceType extended : wrappedNode.getExtendedTypes()) {
try {
ancestors.add(toReferenceType(extended));
} catch (UnsolvedSymbolException e) {
if (!acceptIncompleteList) {
// we only throw an exception if we require a complete list; otherwise, we attempt to continue gracefully
throw e;
}
}
}
}
// TODO FIXME: Remove null check -- should be an empty list...
if (wrappedNode.getImplementedTypes() != null) {
for (ClassOrInterfaceType implemented : wrappedNode.getImplementedTypes()) {
try {
ancestors.add(toReferenceType(implemented));
} catch (UnsolvedSymbolException e) {
if (!acceptIncompleteList) {
// we only throw an exception if we require a complete list; otherwise, we attempt to continue gracefully
throw e;
}
}
}
}
return ancestors;
}
@Override
public List<ResolvedTypeParameterDeclaration> getTypeParameters() {
if (this.wrappedNode.getTypeParameters() == null) {
return Collections.emptyList();
} else {
return this.wrappedNode.getTypeParameters().stream().map(
(tp) -> new JavaParserTypeParameter(tp, typeSolver)
).collect(Collectors.toList());
}
}
/**
* Returns the JavaParser node associated with this JavaParserInterfaceDeclaration.
*
* @return A visitable JavaParser node wrapped by this object.
*/
public ClassOrInterfaceDeclaration getWrappedNode() {
return wrappedNode;
}
@Override
public AccessSpecifier accessSpecifier() {
return wrappedNode.getAccessSpecifier();
}
@Override
public Set<ResolvedReferenceTypeDeclaration> internalTypes() {
return javaParserTypeAdapter.internalTypes();
}
@Override
public Optional<ResolvedReferenceTypeDeclaration> containerType() {
return javaParserTypeAdapter.containerType();
}
@Override
public List<ResolvedConstructorDeclaration> getConstructors() {
return Collections.emptyList();
}
@Override
public Optional<Node> toAst() {
return Optional.of(wrappedNode);
}
///
/// Private methods
///
private ResolvedReferenceType toReferenceType(ClassOrInterfaceType classOrInterfaceType) {
SymbolReference<? extends ResolvedTypeDeclaration> ref = null;
String typeName = classOrInterfaceType.getName().getId();
if (classOrInterfaceType.getScope().isPresent()) {
typeName = classOrInterfaceType.getScope().get().asString() + "." + typeName;
}
if (typeName.indexOf('.') > -1) {
ref = typeSolver.tryToSolveType(typeName);
}
if (ref == null || !ref.isSolved()) {
ref = solveType(typeName);
}
if (!ref.isSolved()) {
throw new UnsolvedSymbolException(classOrInterfaceType.getName().getId());
}
if (!classOrInterfaceType.getTypeArguments().isPresent()) {
return new ReferenceTypeImpl(ref.getCorrespondingDeclaration().asReferenceType());
}
List<ResolvedType> superClassTypeParameters = classOrInterfaceType.getTypeArguments().get()
.stream().map(ta -> new LazyType(v -> JavaParserFacade.get(typeSolver).convert(ta, ta)))
.collect(Collectors.toList());
return new ReferenceTypeImpl(ref.getCorrespondingDeclaration().asReferenceType(), superClassTypeParameters);
}
}