12
12
import com .github .javaparser .ast .stmt .BlockStmt ;
13
13
import com .github .javaparser .ast .type .ReferenceType ;
14
14
import com .github .javaparser .ast .type .Type ;
15
+ import com .github .javaparser .resolution .MethodAmbiguityException ;
15
16
import com .github .javaparser .resolution .UnsolvedSymbolException ;
16
17
import com .github .javaparser .resolution .types .ResolvedType ;
17
18
import com .github .javaparser .symbolsolver .JavaSymbolSolver ;
@@ -299,7 +300,6 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara
299
300
callableNode .setReferencedTypes (getReferencedTypes (body ));
300
301
callableNode .setCode (body .isPresent () ? body .get ().toString () : "" );
301
302
302
- callableNode .setCalledMethodDeclaringTypes (getCalledMethodDeclaringTypes (body ));
303
303
callableNode .setAccessedFields (getAccessedFields (body , classFields , typeName ));
304
304
callableNode .setCallSites (getCallSites (body ));
305
305
callableNode .setVariableDeclarations (getVariableDeclarations (body ));
@@ -434,34 +434,6 @@ private static List<String> getAccessedFields(Optional<BlockStmt> callableBody,
434
434
return new ArrayList <>(accessedFields );
435
435
}
436
436
437
- /**
438
- * For method calls occurring in the given callable, computes the set of declaring types and returns
439
- * their qualified names.
440
- *
441
- * @param callableBody Callable to compute declaring types for called methods
442
- * @return List of qualified type names for method calls
443
- */
444
- private static List <String > getCalledMethodDeclaringTypes (Optional <BlockStmt > callableBody ) {
445
- Set <String > calledMethodDeclaringTypes = new HashSet <>();
446
- callableBody .ifPresent (cb -> cb .findAll (MethodCallExpr .class )
447
- .stream ()
448
- .map (expr -> {
449
- String resolvedExpr = "" ;
450
- if (expr .getScope ().isPresent ()) {
451
- resolvedExpr = resolveExpression (expr .getScope ().get ());
452
- if (resolvedExpr .contains (" | " )) {
453
- return resolvedExpr .split (" \\ | " );
454
- }
455
- }
456
- return new String []{resolvedExpr };
457
- })
458
- .flatMap (type -> Arrays .stream (type ))
459
- .filter (type -> !type .isEmpty ())
460
- .forEach (calledMethodDeclaringTypes ::add )
461
- );
462
- return new ArrayList <>(calledMethodDeclaringTypes );
463
- }
464
-
465
437
/**
466
438
* Returns information about call sites in the given callable. The information includes:
467
439
* the method name, the declaring type name, and types of arguments used in method call.
@@ -478,9 +450,11 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
478
450
// resolve declaring type for called method
479
451
boolean isStaticCall = false ;
480
452
String declaringType = "" ;
453
+ String receiverName = "" ;
481
454
if (methodCallExpr .getScope ().isPresent ()) {
482
455
Expression scopeExpr = methodCallExpr .getScope ().get ();
483
- declaringType = resolveExpression (methodCallExpr .getScope ().get ());
456
+ receiverName = scopeExpr .toString ();
457
+ declaringType = resolveExpression (scopeExpr );
484
458
if (declaringType .contains (" | " )) {
485
459
declaringType = declaringType .split (" \\ | " )[0 ];
486
460
}
@@ -494,7 +468,7 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
494
468
List <String > arguments = methodCallExpr .getArguments ().stream ()
495
469
.map (arg -> resolveExpression (arg )).collect (Collectors .toList ());
496
470
// add a new call site object
497
- callSites .add (createCallSite (methodCallExpr , methodCallExpr .getNameAsString (), declaringType ,
471
+ callSites .add (createCallSite (methodCallExpr , methodCallExpr .getNameAsString (), receiverName , declaringType ,
498
472
arguments , isStaticCall , false ));
499
473
}
500
474
@@ -512,6 +486,7 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
512
486
513
487
// add a new call site object
514
488
callSites .add (createCallSite (objectCreationExpr , "<init>" ,
489
+ objectCreationExpr .getScope ().isPresent () ? objectCreationExpr .getScope ().get ().toString () : "" ,
515
490
instantiatedType , arguments , false , true ));
516
491
}
517
492
@@ -524,17 +499,20 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
524
499
*
525
500
* @param callExpr
526
501
* @param calleeName
527
- * @param declaringType
502
+ * @param receiverExpr
503
+ * @param receiverType
528
504
* @param arguments
529
505
* @param isStaticCall
530
506
* @param isConstructorCall
531
507
* @return
532
508
*/
533
- private static CallSite createCallSite (Expression callExpr , String calleeName , String declaringType ,
534
- List <String > arguments , boolean isStaticCall , boolean isConstructorCall ) {
509
+ private static CallSite createCallSite (Expression callExpr , String calleeName , String receiverExpr ,
510
+ String receiverType , List <String > arguments , boolean isStaticCall ,
511
+ boolean isConstructorCall ) {
535
512
CallSite callSite = new CallSite ();
536
513
callSite .setMethodName (calleeName );
537
- callSite .setDeclaringType (declaringType );
514
+ callSite .setReceiverExpr (receiverExpr );
515
+ callSite .setReceiverType (receiverType );
538
516
callSite .setArgumentTypes (arguments );
539
517
callSite .setStaticCall (isStaticCall );
540
518
callSite .setConstructorCall (isConstructorCall );
@@ -580,7 +558,7 @@ private static String resolveExpression(Expression expression) {
580
558
private static String resolveType (Type type ) {
581
559
try {
582
560
return type .resolve ().describe ();
583
- } catch (UnsolvedSymbolException | IllegalStateException e ) {
561
+ } catch (UnsolvedSymbolException | IllegalStateException | MethodAmbiguityException e ) {
584
562
Log .warn ("Could not resolve " +type .asString ()+": " +e .getMessage ());
585
563
return type .asString ();
586
564
}
0 commit comments