/
MethodUsage.java
111 lines (91 loc) · 3.42 KB
/
MethodUsage.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
package me.tomassetti.symbolsolver.model.typesystem;
import me.tomassetti.symbolsolver.resolution.TypeSolver;
import me.tomassetti.symbolsolver.model.declarations.MethodDeclaration;
import me.tomassetti.symbolsolver.model.declarations.TypeDeclaration;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class MethodUsage {
@Override
public String toString() {
return "MethodUsage{" +
"declaration=" + declaration +
", paramTypes=" + paramTypes +
'}';
}
public MethodDeclaration getDeclaration() {
return declaration;
}
private MethodDeclaration declaration;
public MethodUsage(MethodDeclaration declaration, TypeSolver typeSolver) {
this.declaration = declaration;
for (int i=0;i<declaration.getNoParams();i++){
paramTypes.add(declaration.getParam(i).getType(typeSolver));
}
returnType = declaration.getReturnType(typeSolver);
}
public MethodUsage(MethodDeclaration declaration, List<TypeUsage> paramTypes, TypeUsage returnType) {
this.declaration = declaration;
this.paramTypes = paramTypes;
this.returnType = returnType;
}
private List<TypeUsage> paramTypes = new ArrayList<>();
private TypeUsage returnType;
public String getName() {
return declaration.getName();
}
public TypeDeclaration declaringType() {
return declaration.declaringType();
}
public TypeUsage returnType() {
return returnType;
}
public List<TypeUsage> getParamTypes() {
return paramTypes;
}
public MethodUsage replaceParamType(int i, TypeUsage replaced) {
if (paramTypes.get(i) == replaced) {
return this;
}
List<TypeUsage> newParams = new LinkedList<>(paramTypes);
newParams.set(i, replaced);
return new MethodUsage(declaration, newParams, returnType);
}
public MethodUsage replaceReturnType(TypeUsage returnType) {
if (returnType == this.returnType) {
return this;
} else {
return new MethodUsage(declaration, paramTypes, returnType);
}
}
public int getNoParams() {
return paramTypes.size();
}
public TypeUsage getParamType(int i, TypeSolver typeSolver) {
//TypeUsage typeUsage = declaration.getParam(i).getType(typeSolver);
//return typeUsage;
return paramTypes.get(i);
}
public MethodUsage replaceNameParam(String name, TypeUsage typeUsage) {
// TODO if the method declaration has a type param with that name ignore this call
MethodUsage res = this;
for (int i = 0; i<paramTypes.size(); i++){
TypeUsage originalParamType = paramTypes.get(i);
TypeUsage newParamType = replaceNameParam(name, typeUsage, originalParamType);
res = res.replaceParamType(i, newParamType);
}
res = res.replaceReturnType(replaceNameParam(name, typeUsage, res.returnType));
return res;
}
private static TypeUsage replaceNameParam(String name, TypeUsage newValue, TypeUsage typeToBeExamined) {
if (typeToBeExamined.isTypeVariable()){
if (typeToBeExamined.describe().equals(name)) {
return newValue;
} else {
return typeToBeExamined;
}
}
int i = 0;
return typeToBeExamined;
}
}