71
71
public class TypeExtractor extends DefaultVisitorAdapter {
72
72
73
73
private static final String JAVA_LANG_STRING = String .class .getCanonicalName ();
74
-
74
+ private final ReferenceTypeImpl stringReferenceType ;
75
+
75
76
private TypeSolver typeSolver ;
76
77
private JavaParserFacade facade ;
77
78
78
- private ReferenceTypeImpl StringReferenceType ;
79
79
80
80
public TypeExtractor (TypeSolver typeSolver , JavaParserFacade facade ) {
81
81
this .typeSolver = typeSolver ;
82
82
this .facade = facade ;
83
83
//pre-calculate the String reference (optimization)
84
- StringReferenceType = new ReferenceTypeImpl (new ReflectionTypeSolver ().solveType (JAVA_LANG_STRING ), typeSolver );
84
+ stringReferenceType = new ReferenceTypeImpl (new ReflectionTypeSolver ().solveType (JAVA_LANG_STRING ), typeSolver );
85
85
}
86
86
87
87
@ Override
@@ -264,7 +264,7 @@ public ResolvedType visit(FieldAccessExpr node, Boolean solveLambdas) {
264
264
}
265
265
Optional <Value > value = Optional .empty ();
266
266
try {
267
- value = new SymbolSolver ( typeSolver ).solveSymbolAsValue (node .getName ().getId (), node );
267
+ value = createSolver ( ).solveSymbolAsValue (node .getName ().getId (), node );
268
268
} catch (UnsolvedSymbolException use ) {
269
269
// This node may have a package name as part of its fully qualified name.
270
270
// We should solve for the type declaration inside this package.
@@ -286,7 +286,7 @@ public ResolvedType visit(InstanceOfExpr node, Boolean solveLambdas) {
286
286
287
287
@ Override
288
288
public ResolvedType visit (StringLiteralExpr node , Boolean solveLambdas ) {
289
- return StringReferenceType ;
289
+ return stringReferenceType ;
290
290
}
291
291
292
292
@ Override
@@ -336,7 +336,7 @@ public ResolvedType visit(MethodCallExpr node, Boolean solveLambdas) {
336
336
@ Override
337
337
public ResolvedType visit (NameExpr node , Boolean solveLambdas ) {
338
338
Log .trace ("getType on name expr %s" , ()-> node );
339
- Optional <Value > value = new SymbolSolver ( typeSolver ).solveSymbolAsValue (node .getName ().getId (), node );
339
+ Optional <Value > value = createSolver ( ).solveSymbolAsValue (node .getName ().getId (), node );
340
340
if (!value .isPresent ()) {
341
341
throw new UnsolvedSymbolException ("Solving " + node , node .getName ().getId ());
342
342
} else {
@@ -363,7 +363,7 @@ public ResolvedType visit(TypeExpr node, Boolean solveLambdas) {
363
363
}
364
364
365
365
// JLS 15.13 - ExpressionName :: [TypeArguments] Identifier
366
- Optional <Value > value = new SymbolSolver ( typeSolver ).solveSymbolAsValue (nameWithScope , node );
366
+ Optional <Value > value = createSolver ( ).solveSymbolAsValue (nameWithScope , node );
367
367
if (value .isPresent ()) {
368
368
return value .get ().getType ();
369
369
}
@@ -648,4 +648,8 @@ public ResolvedType visit(FieldDeclaration node, Boolean solveLambdas) {
648
648
}
649
649
throw new IllegalArgumentException ("Cannot resolve the type of a field with multiple variable declarations. Pick one" );
650
650
}
651
+
652
+ protected SymbolSolver createSolver () {
653
+ return new SymbolSolver (typeSolver );
654
+ }
651
655
}
0 commit comments