-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
JavaSymbolSolver.java
188 lines (179 loc) · 9.87 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
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.Expression;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.ThisExpr;
import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt;
import com.github.javaparser.ast.type.ArrayType;
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.ResolvedType;
import com.github.javaparser.symbolsolver.javaparser.Navigator;
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;
import java.util.Optional;
/**
* This implementation of the SymbolResolver wraps the functionalities of the library to make them easily usable
* from JavaParser nodes.
*
* 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.
*
* @author Federico Tomassetti
*/
public class JavaSymbolSolver implements SymbolResolver {
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 = Navigator.findAncestor(node, 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 = Navigator.findAncestor(node, 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) {
ResolvedFieldDeclaration resolved = new JavaParserFieldDeclaration((VariableDeclarator)node, typeSolver);
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 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 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 = Navigator.findAncestor(node, 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);
}
}