39
39
public class SymbolTable {
40
40
41
41
private static JavaSymbolSolver javaSymbolSolver ;
42
+ private static Set <String > unresolvedTypes = new HashSet <>();
43
+ private static Set <String > unresolvedExpressions = new HashSet <>();
42
44
43
45
/**
44
46
* Processes the given compilation unit to extract information about classes and
@@ -408,6 +410,7 @@ private static List<String> getAccessedFields(Optional<BlockStmt> callableBody,
408
410
// process field access expressions in the callable
409
411
callableBody .ifPresent (cb -> cb .findAll (FieldAccessExpr .class )
410
412
.stream ()
413
+ .filter (faExpr -> faExpr .getParentNode ().isPresent () && !(faExpr .getParentNode ().get () instanceof FieldAccessExpr ))
411
414
.map (faExpr -> {
412
415
String fieldDeclaringType = resolveExpression (faExpr .getScope ());
413
416
if (!fieldDeclaringType .isEmpty ()) {
@@ -474,12 +477,8 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
474
477
475
478
for (ObjectCreationExpr objectCreationExpr : callableBody .get ().findAll (ObjectCreationExpr .class )) {
476
479
// resolve declaring type for called method
477
- String instantiatedType = objectCreationExpr .getTypeAsString ();
478
- try {
479
- instantiatedType = objectCreationExpr .getType ().resolve ().describe ();
480
- } catch (UnsolvedSymbolException | IllegalStateException e ) {
481
- Log .warn ("Could not resolve " +instantiatedType +": " +e .getMessage ());
482
- }
480
+ String instantiatedType = resolveType (objectCreationExpr .getType ());
481
+
483
482
// resolve arguments of the constructor call to types
484
483
List <String > arguments = objectCreationExpr .getArguments ().stream ()
485
484
.map (arg -> resolveExpression (arg )).collect (Collectors .toList ());
@@ -538,13 +537,17 @@ private static CallSite createCallSite(Expression callExpr, String calleeName, S
538
537
* @return Resolved type name or empty string if type resolution fails
539
538
*/
540
539
private static String resolveExpression (Expression expression ) {
541
- try {
542
- ResolvedType resolvedType = javaSymbolSolver .calculateType (expression );
543
- if (resolvedType .isReferenceType () || resolvedType .isUnionType ()) {
544
- return resolvedType .describe ();
540
+ // perform expression resolution if resolution of this expression did not fail previously
541
+ if (!unresolvedExpressions .contains (expression .toString ())) {
542
+ try {
543
+ ResolvedType resolvedType = javaSymbolSolver .calculateType (expression );
544
+ if (resolvedType .isReferenceType () || resolvedType .isUnionType ()) {
545
+ return resolvedType .describe ();
546
+ }
547
+ } catch (RuntimeException exception ) {
548
+ Log .debug ("Could not resolve expression: " + expression + ": " + exception .getMessage ());
549
+ unresolvedExpressions .add (expression .toString ());
545
550
}
546
- } catch (RuntimeException exception ) {
547
- Log .warn ("Could not resolve expression: " +expression +"\n " +exception .getMessage ());
548
551
}
549
552
return "" ;
550
553
}
@@ -556,12 +559,16 @@ private static String resolveExpression(Expression expression) {
556
559
* @return Resolved (qualified) type name
557
560
*/
558
561
private static String resolveType (Type type ) {
559
- try {
560
- return type .resolve ().describe ();
561
- } catch (UnsolvedSymbolException | IllegalStateException | MethodAmbiguityException e ) {
562
- Log .warn ("Could not resolve " +type .asString ()+": " +e .getMessage ());
563
- return type .asString ();
562
+ // perform type resolution if resolution of this type did not fail previously
563
+ if (!unresolvedTypes .contains (type .asString ())) {
564
+ try {
565
+ return type .resolve ().describe ();
566
+ } catch (RuntimeException e ) {
567
+ Log .warn ("Could not resolve type: " + type .asString () + ": " + e .getMessage ());
568
+ unresolvedTypes .add (type .asString ());
569
+ }
564
570
}
571
+ return type .asString ();
565
572
}
566
573
567
574
/**
0 commit comments