-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
JavaSymbolSolver.java
238 lines (224 loc) · 12.3 KB
/
JavaSymbolSolver.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
package com.github.javaparser.symbolsolver;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.*;
import com.github.javaparser.ast.expr.*;
import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.resolution.SymbolResolver;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.declarations.*;
import com.github.javaparser.resolution.types.ResolvedPrimitiveType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.*;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
/**
* This implementation of the SymbolResolver wraps the functionality of the library to make them easily usable
* from JavaParser nodes.
* <p>
* An instance of this class should be created once and then injected in all the CompilationUnit for which we
* want to enable symbol resolution. To do so the method inject can be used, or you can use
* {@link com.github.javaparser.ParserConfiguration#setSymbolResolver(SymbolResolver)} and the parser will do the
* injection for you.
*
* @author Federico Tomassetti
*/
public class JavaSymbolSolver implements SymbolResolver {
private static class ArrayLengthValueDeclaration implements ResolvedValueDeclaration {
private static final ArrayLengthValueDeclaration INSTANCE = new ArrayLengthValueDeclaration();
private ArrayLengthValueDeclaration() {
}
@Override
public String getName() {
return "length";
}
@Override
public ResolvedType getType() {
return ResolvedPrimitiveType.INT;
}
}
private TypeSolver typeSolver;
public JavaSymbolSolver(TypeSolver typeSolver) {
this.typeSolver = typeSolver;
}
/**
* Register this SymbolResolver into a CompilationUnit, so that symbol resolution becomes available to
* all nodes part of the CompilationUnit.
*/
public void inject(CompilationUnit destination) {
destination.setData(Node.SYMBOL_RESOLVER_KEY, this);
}
@Override
public <T> T resolveDeclaration(Node node, Class<T> resultClass) {
if (node instanceof MethodDeclaration) {
return resultClass.cast(new JavaParserMethodDeclaration((MethodDeclaration) node, typeSolver));
}
if (node instanceof ClassOrInterfaceDeclaration) {
ResolvedReferenceTypeDeclaration resolved = JavaParserFactory.toTypeDeclaration(node, typeSolver);
if (resultClass.isInstance(resolved)) {
return resultClass.cast(resolved);
}
}
if (node instanceof EnumDeclaration) {
ResolvedReferenceTypeDeclaration resolved = JavaParserFactory.toTypeDeclaration(node, typeSolver);
if (resultClass.isInstance(resolved)) {
return resultClass.cast(resolved);
}
}
if (node instanceof EnumConstantDeclaration) {
ResolvedEnumDeclaration enumDeclaration = node.findAncestor(EnumDeclaration.class).get().resolve().asEnum();
ResolvedEnumConstantDeclaration resolved = enumDeclaration.getEnumConstants().stream().filter(c -> ((JavaParserEnumConstantDeclaration) c).getWrappedNode() == node).findFirst().get();
if (resultClass.isInstance(resolved)) {
return resultClass.cast(resolved);
}
}
if (node instanceof ConstructorDeclaration) {
ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) node;
ClassOrInterfaceDeclaration classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) node.getParentNode().get();
ResolvedClassDeclaration resolvedClass = resolveDeclaration(classOrInterfaceDeclaration, ResolvedClassDeclaration.class).asClass();
ResolvedConstructorDeclaration resolved = resolvedClass.getConstructors().stream().filter(c -> ((JavaParserConstructorDeclaration) c).getWrappedNode() == constructorDeclaration).findFirst().get();
if (resultClass.isInstance(resolved)) {
return resultClass.cast(resolved);
}
}
if (node instanceof AnnotationDeclaration) {
ResolvedReferenceTypeDeclaration resolved = JavaParserFactory.toTypeDeclaration(node, typeSolver);
if (resultClass.isInstance(resolved)) {
return resultClass.cast(resolved);
}
}
if (node instanceof AnnotationMemberDeclaration) {
ResolvedAnnotationDeclaration annotationDeclaration = node.findAncestor(AnnotationDeclaration.class).get().resolve();
ResolvedAnnotationMemberDeclaration resolved = annotationDeclaration.getAnnotationMembers().stream().filter(c -> ((JavaParserAnnotationMemberDeclaration) c).getWrappedNode() == node).findFirst().get();
if (resultClass.isInstance(resolved)) {
return resultClass.cast(resolved);
}
}
if (node instanceof FieldDeclaration) {
FieldDeclaration fieldDeclaration = (FieldDeclaration) node;
if (fieldDeclaration.getVariables().size() != 1) {
throw new RuntimeException("Cannot resolve a Field Declaration including multiple variable declarators. Resolve the single variable declarators");
}
ResolvedFieldDeclaration resolved = new JavaParserFieldDeclaration(fieldDeclaration.getVariable(0), typeSolver);
if (resultClass.isInstance(resolved)) {
return resultClass.cast(resolved);
}
}
if (node instanceof VariableDeclarator) {
ResolvedValueDeclaration resolved;
if (node.getParentNode().isPresent() && node.getParentNode().get() instanceof FieldDeclaration) {
resolved = new JavaParserFieldDeclaration((VariableDeclarator) node, typeSolver);
} else if (node.getParentNode().isPresent() && node.getParentNode().get() instanceof VariableDeclarationExpr) {
resolved = new JavaParserVariableDeclaration((VariableDeclarator) node, typeSolver);
} else {
throw new UnsupportedOperationException("Parent of VariableDeclarator is: " + node.getParentNode());
}
if (resultClass.isInstance(resolved)) {
return resultClass.cast(resolved);
}
}
if (node instanceof MethodCallExpr) {
SymbolReference<ResolvedMethodDeclaration> result = JavaParserFacade.get(typeSolver).solve((MethodCallExpr) node);
if (result.isSolved()) {
if (resultClass.isInstance(result.getCorrespondingDeclaration())) {
return resultClass.cast(result.getCorrespondingDeclaration());
}
} else {
throw new UnsolvedSymbolException("We are unable to find the method declaration corresponding to " + node);
}
}
if (node instanceof ObjectCreationExpr) {
SymbolReference<ResolvedConstructorDeclaration> result = JavaParserFacade.get(typeSolver).solve((ObjectCreationExpr) node);
if (result.isSolved()) {
if (resultClass.isInstance(result.getCorrespondingDeclaration())) {
return resultClass.cast(result.getCorrespondingDeclaration());
}
} else {
throw new UnsolvedSymbolException("We are unable to find the constructor declaration corresponding to " + node);
}
}
if (node instanceof NameExpr) {
SymbolReference<? extends ResolvedValueDeclaration> result = JavaParserFacade.get(typeSolver).solve((NameExpr) node);
if (result.isSolved()) {
if (resultClass.isInstance(result.getCorrespondingDeclaration())) {
return resultClass.cast(result.getCorrespondingDeclaration());
}
} else {
throw new UnsolvedSymbolException("We are unable to find the value declaration corresponding to " + node);
}
}
if (node instanceof FieldAccessExpr) {
SymbolReference<? extends ResolvedValueDeclaration> result = JavaParserFacade.get(typeSolver).solve((FieldAccessExpr) node);
if (result.isSolved()) {
if (resultClass.isInstance(result.getCorrespondingDeclaration())) {
return resultClass.cast(result.getCorrespondingDeclaration());
}
} else {
if (((FieldAccessExpr) node).getName().getId().equals("length")) {
ResolvedType scopeType = ((FieldAccessExpr) node).getScope().calculateResolvedType();
if (scopeType.isArray()) {
if (resultClass.isInstance(ArrayLengthValueDeclaration.INSTANCE)) {
return resultClass.cast(ArrayLengthValueDeclaration.INSTANCE);
}
}
}
throw new UnsolvedSymbolException("We are unable to find the value declaration corresponding to " + node);
}
}
if (node instanceof ThisExpr) {
SymbolReference<ResolvedTypeDeclaration> result = JavaParserFacade.get(typeSolver).solve((ThisExpr) node);
if (result.isSolved()) {
if (resultClass.isInstance(result.getCorrespondingDeclaration())) {
return resultClass.cast(result.getCorrespondingDeclaration());
}
} else {
throw new UnsolvedSymbolException("We are unable to find the type declaration corresponding to " + node);
}
}
if (node instanceof ExplicitConstructorInvocationStmt) {
SymbolReference<ResolvedConstructorDeclaration> result = JavaParserFacade.get(typeSolver).solve((ExplicitConstructorInvocationStmt) node);
if (result.isSolved()) {
if (resultClass.isInstance(result.getCorrespondingDeclaration())) {
return resultClass.cast(result.getCorrespondingDeclaration());
}
} else {
throw new UnsolvedSymbolException("We are unable to find the constructor declaration corresponding to " + node);
}
}
if (node instanceof Parameter) {
if (ResolvedParameterDeclaration.class.equals(resultClass)) {
Parameter parameter = (Parameter) node;
CallableDeclaration callableDeclaration = node.findAncestor(CallableDeclaration.class).get();
ResolvedMethodLikeDeclaration resolvedMethodLikeDeclaration;
if (callableDeclaration.isConstructorDeclaration()) {
resolvedMethodLikeDeclaration = callableDeclaration.asConstructorDeclaration().resolve();
} else {
resolvedMethodLikeDeclaration = callableDeclaration.asMethodDeclaration().resolve();
}
for (int i = 0; i < resolvedMethodLikeDeclaration.getNumberOfParams(); i++) {
if (resolvedMethodLikeDeclaration.getParam(i).getName().equals(parameter.getNameAsString())) {
return resultClass.cast(resolvedMethodLikeDeclaration.getParam(i));
}
}
}
}
throw new UnsupportedOperationException("Unable to find the declaration of type " + resultClass.getSimpleName()
+ " from " + node.getClass().getSimpleName());
}
@Override
public <T> T toResolvedType(Type javaparserType, Class<T> resultClass) {
ResolvedType resolvedType = JavaParserFacade.get(typeSolver).convertToUsage(javaparserType, javaparserType);
if (resultClass.isInstance(resolvedType)) {
return resultClass.cast(resolvedType);
}
throw new UnsupportedOperationException("Unable to get the resolved type of class "
+ resultClass.getSimpleName() + " from " + javaparserType);
}
@Override
public ResolvedType calculateType(Expression expression) {
return JavaParserFacade.get(typeSolver).getType(expression);
}
}