/
ReferenceTypeUsage.java
295 lines (248 loc) · 10.2 KB
/
ReferenceTypeUsage.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
package me.tomassetti.symbolsolver.model.typesystem;
import me.tomassetti.symbolsolver.resolution.*;
import me.tomassetti.symbolsolver.model.declarations.MethodDeclaration;
import me.tomassetti.symbolsolver.model.declarations.TypeDeclaration;
import me.tomassetti.symbolsolver.resolution.javaparser.declarations.JavaParserTypeVariableDeclaration;
import me.tomassetti.symbolsolver.resolution.reflection.ReflectionClassDeclaration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class ReferenceTypeUsage implements TypeUsage {
//public static final ReferenceTypeUsage OBJECT = new ReferenceTypeUsage(new ReflectionClassDeclaration(Object.class));
//public static final ReferenceTypeUsage STRING = new ReferenceTypeUsage(new ReflectionClassDeclaration(String.class));
private TypeDeclaration typeDeclaration;
private List<TypeUsage> typeParameters;
private TypeSolver typeSolver;
public ReferenceTypeUsage asReferenceTypeUsage() {
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ReferenceTypeUsage that = (ReferenceTypeUsage) o;
if (!typeDeclaration.equals(that.typeDeclaration)) return false;
if (!typeParameters.equals(that.typeParameters)) return false;
return true;
}
@Override
public int hashCode() {
int result = typeDeclaration.hashCode();
result = 31 * result + typeParameters.hashCode();
return result;
}
public TypeDeclaration getTypeDeclaration() {
return typeDeclaration;
}
public void setTypeDeclaration(TypeDeclaration typeDeclaration) {
this.typeDeclaration = typeDeclaration;
if (this.typeDeclaration.isTypeVariable()) {
throw new IllegalArgumentException();
}
}
public ReferenceTypeUsage(TypeDeclaration typeDeclaration, TypeSolver typeSolver) {
this(typeDeclaration, deriveParams(typeDeclaration), typeSolver);
if (this.typeDeclaration.isTypeVariable()) {
throw new IllegalArgumentException();
}
this.typeSolver = typeSolver;
}
private static List<TypeUsage> deriveParams(TypeDeclaration typeDeclaration) {
return typeDeclaration.getTypeParameters().stream().map((tp)->new TypeUsageOfTypeParameter(tp)).collect(Collectors.toList());
}
public boolean isEnum() {
return typeDeclaration.isEnum();
}
public ReferenceTypeUsage(TypeDeclaration typeDeclaration, List<TypeUsage> typeParameters, TypeSolver typeSolver) {
this.typeDeclaration = typeDeclaration;
this.typeParameters = typeParameters;
if (this.typeDeclaration.isTypeVariable()) {
throw new IllegalArgumentException();
}
this.typeSolver = typeSolver;
}
@Override
public boolean isArray() {
return false;
}
@Override
public boolean isPrimitive() {
return false;
}
@Override
public boolean isReferenceType() {
return true;
}
@Override
public String toString() {
return "ReferenceTypeUsage{" +
"declaration=" + typeDeclaration +
", typeParameters=" + typeParameters +
'}';
}
private Optional<TypeUsage> typeParamByName(String name){
List<TypeUsage> typeParameters = this.typeParameters;
if (typeDeclaration.getTypeParameters().size() != typeParameters.size()){
if (typeParameters.size() > 0) {
throw new UnsupportedOperationException();
}
// type parameters not specified, default to Object
typeParameters = new ArrayList<>();
for (int i=0;i<typeDeclaration.getTypeParameters().size();i++){
typeParameters.add(new ReferenceTypeUsage(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver));
}
}
int i = 0;
for (TypeParameter tp : typeDeclaration.getTypeParameters()){
if (tp.getName().equals(name)) {
return Optional.of(typeParameters.get(i));
}
i++;
}
return Optional.empty();
}
public Optional<Value> getField(String name, TypeSolver typeSolver) {
if (!typeDeclaration.hasField(name, typeSolver)){
return Optional.empty();
}
//Qui succede che getField avrebbe il tipo E. Devo vedere se io ho tipi da sostituire.
TypeUsage typeUsage = typeDeclaration.getField(name, typeSolver).getType(typeSolver);
//TypeUsage typeUsage = new TypeUsageOfTypeDeclaration(typeOfField);
//ora io dovrei capire che mi ha restituito una variabile che si riferisce alla classe
//rappresentata da THIS. Per capirlo potremmo associare piu' info alle TypeVariable,
//mettendo dove sono state dichiarate
// Mi pare risolviamo nel punto sbagliato, dovrebbe essere
typeUsage = replaceTypeParams(typeUsage);
return Optional.of(new Value(typeUsage, name, true));
}
public Optional<TypeUsage> solveGenericType(String name) {
int i=0;
for (TypeParameter tp :typeDeclaration.getTypeParameters()){
if (tp.getName().equals(name)){
return Optional.of(this.typeParameters.get(i));
}
i++;
}
return Optional.empty();
}
/**
* Create a copy of the value with the type parameter changed.
* @param i
* @param replaced
* @return
*/
public TypeUsage replaceParam(int i, TypeUsage replaced) {
ArrayList<TypeUsage> typeParametersCorrected = new ArrayList<>(typeParameters);
typeParametersCorrected.set(i, replaced);
return new ReferenceTypeUsage(typeDeclaration, typeParametersCorrected, typeSolver);
}
@Override
public TypeUsage replaceParam(String name, TypeUsage replaced) {
List<TypeUsage> newParams = typeParameters.stream().map((tp)->tp.replaceParam(name, replaced)).collect(Collectors.toList());
if (typeParameters.equals(newParams)) {
return this;
} else {
return new ReferenceTypeUsage(typeDeclaration, newParams, typeSolver);
}
}
public List<ReferenceTypeUsage> getAllAncestors() {
return typeDeclaration.getAllAncestors();
}
public TypeUsage replaceTypeParams(TypeUsage typeUsage){
if (typeUsage.isTypeVariable()) {
TypeParameter typeParameter = typeUsage.asTypeParameter();
if (typeParameter.declaredOnClass()) {
Optional<TypeUsage> typeParam = typeParamByName(typeParameter.getName());
if (typeParam.isPresent()) {
typeUsage = typeParam.get();
}
}
}
if (typeUsage.isReferenceType()) {
for (int i = 0; i < typeUsage.asReferenceTypeUsage().parameters().size(); i++) {
TypeUsage replaced = replaceTypeParams(typeUsage.asReferenceTypeUsage().parameters().get(i));
// Identity comparison on purpose
if (replaced != typeUsage.asReferenceTypeUsage().parameters().get(i)) {
typeUsage = typeUsage.asReferenceTypeUsage().replaceParam(i, replaced);
}
}
}
return typeUsage;
}
@Override
public String describe() {
StringBuffer sb = new StringBuffer();
sb.append(typeDeclaration.getQualifiedName());
if (parameters().size() > 0){
sb.append("<");
boolean first = true;
for (TypeUsage param : parameters()) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(param.describe());
}
sb.append(">");
}
return sb.toString();
}
public SymbolReference<MethodDeclaration> solveMethod(String name, List<TypeUsage> parameterTypes, TypeSolver typeSolver) {
return typeDeclaration.solveMethod(name, parameterTypes, typeSolver);
}
public List<TypeUsage> parameters() {
return typeParameters;
}
@Override
public TypeParameter asTypeParameter() {
if (this.typeDeclaration instanceof JavaParserTypeVariableDeclaration){
JavaParserTypeVariableDeclaration javaParserTypeVariableDeclaration = (JavaParserTypeVariableDeclaration)this.typeDeclaration;
return javaParserTypeVariableDeclaration.asTypeParameter();
}
throw new UnsupportedOperationException(this.typeDeclaration.getClass().getCanonicalName());
}
@Override
public boolean isTypeVariable() {
return typeDeclaration.isTypeVariable();
}
@Override
public boolean isAssignableBy(TypeUsage other) {
if (other instanceof NullTypeUsage){
return !this.isPrimitive();
}
// consider boxing
if (other.isPrimitive()) {
if (this.getQualifiedName().equals(Object.class.getCanonicalName())) {
return true;
} else {
return isCorrespondingBoxingType(other.describe());
}
}
if (other instanceof LambdaTypeUsagePlaceholder) {
return this.getQualifiedName().equals(Predicate.class.getCanonicalName()) || this.getQualifiedName().equals(Function.class.getCanonicalName());
} else if (other instanceof ReferenceTypeUsage) {
ReferenceTypeUsage otherTUOTD = (ReferenceTypeUsage) other;
return typeDeclaration.isAssignableBy(otherTUOTD.typeDeclaration);
} else if (other.isTypeVariable()) {
// TODO look bounds...
return true;
} else {
return false;
}
}
private boolean isCorrespondingBoxingType(String typeName) {
switch (typeName) {
case "char":
return getQualifiedName().equals(Character.class.getCanonicalName());
default:
throw new UnsupportedOperationException(typeName);
}
}
public String getQualifiedName() {
return typeDeclaration.getQualifiedName();
}
}