-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ReflectionInterfaceDeclaration.java
328 lines (279 loc) · 11.1 KB
/
ReflectionInterfaceDeclaration.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
/*
* Copyright 2016 Federico Tomassetti
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.javaparser.symbolsolver.reflectionmodel;
import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.resolution.declarations.*;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.core.resolution.Context;
import com.github.javaparser.symbolsolver.javaparsermodel.LambdaArgumentTypePlaceholder;
import com.github.javaparser.symbolsolver.logic.AbstractTypeDeclaration;
import com.github.javaparser.symbolsolver.logic.ConfilictingGenericTypesException;
import com.github.javaparser.symbolsolver.logic.InferenceContext;
import com.github.javaparser.symbolsolver.logic.TypeDeclarationWithResolutionCapabalities;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.model.typesystem.NullType;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Federico Tomassetti
*/
public class ReflectionInterfaceDeclaration extends AbstractTypeDeclaration
implements ResolvedInterfaceDeclaration, TypeDeclarationWithResolutionCapabalities {
///
/// Fields
///
private Class<?> clazz;
private TypeSolver typeSolver;
private ReflectionClassAdapter reflectionClassAdapter;
///
/// Constructor
///
public ReflectionInterfaceDeclaration(Class<?> clazz, TypeSolver typeSolver) {
if (!clazz.isInterface()) {
throw new IllegalArgumentException();
}
this.clazz = clazz;
this.typeSolver = typeSolver;
this.reflectionClassAdapter = new ReflectionClassAdapter(clazz, typeSolver, this);
}
///
/// Public methods
///
@Override
public boolean isAssignableBy(ResolvedReferenceTypeDeclaration other) {
return isAssignableBy(new ReferenceTypeImpl(other, typeSolver));
}
@Override
public String getPackageName() {
if (clazz.getPackage() != null) {
return clazz.getPackage().getName();
}
return null;
}
@Override
public String getClassName() {
String canonicalName = clazz.getCanonicalName();
if (canonicalName != null && getPackageName() != null) {
return canonicalName.substring(getPackageName().length() + 1, canonicalName.length());
}
return null;
}
@Override
public String getQualifiedName() {
return clazz.getCanonicalName();
}
@Deprecated
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> parameterTypes, boolean staticOnly) {
return ReflectionMethodResolutionLogic.solveMethod(name, parameterTypes, staticOnly,
typeSolver,this, clazz);
}
@Override
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes,
boolean staticOnly, TypeSolver typeSolver) {
return solveMethod(name, argumentsTypes, staticOnly);
}
@Override
public String toString() {
return "ReflectionInterfaceDeclaration{" +
"clazz=" + clazz.getCanonicalName() +
'}';
}
public ResolvedType getUsage(Node node) {
return new ReferenceTypeImpl(this, typeSolver);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ReflectionInterfaceDeclaration)) return false;
ReflectionInterfaceDeclaration that = (ReflectionInterfaceDeclaration) o;
if (!clazz.getCanonicalName().equals(that.clazz.getCanonicalName())) return false;
if (!getTypeParameters().equals(that.getTypeParameters())) {
return false;
}
return true;
}
@Override
public int hashCode() {
return clazz.hashCode();
}
public Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> parameterTypes, TypeSolver typeSolver,
Context invokationContext, List<ResolvedType> typeParameterValues) {
Optional<MethodUsage> res = ReflectionMethodResolutionLogic.solveMethodAsUsage(name, parameterTypes, typeSolver, invokationContext,
typeParameterValues, this, clazz);
if (res.isPresent()) {
// We have to replace method type typeParametersValues here
InferenceContext inferenceContext = new InferenceContext(MyObjectProvider.INSTANCE);
MethodUsage methodUsage = res.get();
int i = 0;
List<ResolvedType> parameters = new LinkedList<>();
for (ResolvedType actualType : parameterTypes) {
ResolvedType formalType = methodUsage.getParamType(i);
// We need to replace the class type typeParametersValues (while we derive the method ones)
parameters.add(inferenceContext.addPair(formalType, actualType));
i++;
}
try {
ResolvedType returnType = inferenceContext.addSingle(methodUsage.returnType());
for (int j=0;j<parameters.size();j++) {
methodUsage = methodUsage.replaceParamType(j, inferenceContext.resolve(parameters.get(j)));
}
methodUsage = methodUsage.replaceReturnType(inferenceContext.resolve(returnType));
return Optional.of(methodUsage);
} catch (ConfilictingGenericTypesException e) {
return Optional.empty();
}
} else {
return res;
}
}
@Override
public boolean canBeAssignedTo(ResolvedReferenceTypeDeclaration other) {
if (other instanceof LambdaArgumentTypePlaceholder) {
return isFunctionalInterface();
}
if (other.getQualifiedName().equals(getQualifiedName())) {
return true;
}
if (this.clazz.getSuperclass() != null
&& new ReflectionInterfaceDeclaration(clazz.getSuperclass(), typeSolver).canBeAssignedTo(other)) {
return true;
}
for (Class interfaze : clazz.getInterfaces()) {
if (new ReflectionInterfaceDeclaration(interfaze, typeSolver).canBeAssignedTo(other)) {
return true;
}
}
if (other.getQualifiedName().equals(Object.class.getCanonicalName())) {
return true;
}
return false;
}
@Override
public boolean isAssignableBy(ResolvedType type) {
if (type instanceof NullType) {
return true;
}
if (type instanceof LambdaArgumentTypePlaceholder) {
return isFunctionalInterface();
}
if (type.isArray()) {
return false;
}
if (type.isPrimitive()) {
return false;
}
if (type.describe().equals(getQualifiedName())) {
return true;
}
if (type instanceof ReferenceTypeImpl) {
ReferenceTypeImpl otherTypeDeclaration = (ReferenceTypeImpl) type;
return otherTypeDeclaration.getTypeDeclaration().canBeAssignedTo(this);
}
return false;
}
@Override
public boolean isTypeParameter() {
return false;
}
@Override
public ResolvedFieldDeclaration getField(String name) {
return reflectionClassAdapter.getField(name);
}
@Override
public List<ResolvedFieldDeclaration> getAllFields() {
return reflectionClassAdapter.getAllFields();
}
@Deprecated
public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) {
for (Field field : clazz.getFields()) {
if (field.getName().equals(name)) {
return SymbolReference.solved(new ReflectionFieldDeclaration(field, typeSolver));
}
}
return SymbolReference.unsolved(ResolvedValueDeclaration.class);
}
@Override
public List<ResolvedReferenceType> getAncestors(boolean acceptIncompleteList) {
// we do not attempt to perform any symbol solving when analyzing ancestors in the reflection model, so we can
// simply ignore the boolean parameter here; an UnsolvedSymbolException cannot occur
return reflectionClassAdapter.getAncestors();
}
@Override
public Set<ResolvedMethodDeclaration> getDeclaredMethods() {
return reflectionClassAdapter.getDeclaredMethods();
}
@Override
public boolean hasField(String name) {
return reflectionClassAdapter.hasField(name);
}
@Override
public String getName() {
return clazz.getSimpleName();
}
@Override
public boolean isInterface() {
return true;
}
@Override
public List<ResolvedReferenceType> getInterfacesExtended() {
List<ResolvedReferenceType> res = new ArrayList<>();
for (Class i : clazz.getInterfaces()) {
res.add(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(i, typeSolver), typeSolver));
}
return res;
}
@Override
public Optional<ResolvedReferenceTypeDeclaration> containerType() {
return reflectionClassAdapter.containerType();
}
@Override
public Set<ResolvedReferenceTypeDeclaration> internalTypes() {
return Arrays.stream(this.clazz.getDeclaredClasses())
.map(ic -> ReflectionFactory.typeDeclarationFor(ic, typeSolver))
.collect(Collectors.toSet());
}
@Override
public ResolvedInterfaceDeclaration asInterface() {
return this;
}
@Override
public boolean hasDirectlyAnnotation(String canonicalName) {
return reflectionClassAdapter.hasDirectlyAnnotation(canonicalName);
}
@Override
public List<ResolvedTypeParameterDeclaration> getTypeParameters() {
return reflectionClassAdapter.getTypeParameters();
}
@Override
public AccessSpecifier accessSpecifier() {
return ReflectionFactory.modifiersToAccessLevel(this.clazz.getModifiers());
}
@Override
public List<ResolvedConstructorDeclaration> getConstructors() {
return Collections.emptyList();
}
@Override
public Optional<ClassOrInterfaceDeclaration> toAst() {
return Optional.empty();
}
}