|
7 | 7 | import com.github.javaparser.ast.CompilationUnit; |
8 | 8 | import com.github.javaparser.ast.NodeList; |
9 | 9 | import com.github.javaparser.ast.body.*; |
10 | | -import com.github.javaparser.ast.expr.Expression; |
11 | | -import com.github.javaparser.ast.expr.FieldAccessExpr; |
12 | | -import com.github.javaparser.ast.expr.MethodCallExpr; |
13 | | -import com.github.javaparser.ast.expr.NameExpr; |
| 10 | +import com.github.javaparser.ast.expr.*; |
14 | 11 | import com.github.javaparser.ast.nodeTypes.NodeWithName; |
15 | 12 | import com.github.javaparser.ast.stmt.BlockStmt; |
16 | 13 | import com.github.javaparser.ast.type.ReferenceType; |
@@ -479,39 +476,82 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) { |
479 | 476 | } |
480 | 477 | for (MethodCallExpr methodCallExpr : callableBody.get().findAll(MethodCallExpr.class)) { |
481 | 478 | // resolve declaring type for called method |
| 479 | + boolean isStaticCall = false; |
482 | 480 | String declaringType = ""; |
483 | 481 | if (methodCallExpr.getScope().isPresent()) { |
| 482 | + Expression scopeExpr = methodCallExpr.getScope().get(); |
484 | 483 | declaringType = resolveExpression(methodCallExpr.getScope().get()); |
485 | 484 | if (declaringType.contains(" | ")) { |
486 | 485 | declaringType = declaringType.split(" \\| ")[0]; |
487 | 486 | } |
| 487 | + String declaringTypeName = declaringType.contains(".") ? |
| 488 | + declaringType.substring(declaringType.lastIndexOf(".")+1) : declaringType; |
| 489 | + if (declaringTypeName.equals(scopeExpr.toString())) { |
| 490 | + isStaticCall = true; |
| 491 | + } |
488 | 492 | } |
489 | | - |
490 | 493 | // resolve arguments of the method call to types |
491 | 494 | List<String> arguments = methodCallExpr.getArguments().stream() |
492 | 495 | .map(arg -> resolveExpression(arg)).collect(Collectors.toList()); |
493 | | - |
494 | 496 | // add a new call site object |
495 | | - CallSite callSite = new CallSite(); |
496 | | - callSite.setMethodName(methodCallExpr.getNameAsString()); |
497 | | - callSite.setDeclaringType(declaringType); |
498 | | - callSite.setArgumentTypes(arguments); |
499 | | - if (methodCallExpr.getRange().isPresent()) { |
500 | | - callSite.setStartLine(methodCallExpr.getRange().get().begin.line); |
501 | | - callSite.setStartColumn(methodCallExpr.getRange().get().begin.column); |
502 | | - callSite.setEndLine(methodCallExpr.getRange().get().end.line); |
503 | | - callSite.setEndColumn(methodCallExpr.getRange().get().end.column); |
504 | | - } else { |
505 | | - callSite.setStartLine(-1); |
506 | | - callSite.setStartColumn(-1); |
507 | | - callSite.setEndLine(-1); |
508 | | - callSite.setEndColumn(-1); |
| 497 | + callSites.add(createCallSite(methodCallExpr, methodCallExpr.getNameAsString(), declaringType, |
| 498 | + arguments, isStaticCall, false)); |
| 499 | + } |
| 500 | + |
| 501 | + for (ObjectCreationExpr objectCreationExpr : callableBody.get().findAll(ObjectCreationExpr.class)) { |
| 502 | + // resolve declaring type for called method |
| 503 | + String instantiatedType = objectCreationExpr.getTypeAsString(); |
| 504 | + try { |
| 505 | + instantiatedType = objectCreationExpr.getType().resolve().describe(); |
| 506 | + } catch (UnsolvedSymbolException | IllegalStateException e) { |
| 507 | + Log.warn("Could not resolve "+instantiatedType+": "+e.getMessage()); |
509 | 508 | } |
510 | | - callSites.add(callSite); |
| 509 | + // resolve arguments of the constructor call to types |
| 510 | + List<String> arguments = objectCreationExpr.getArguments().stream() |
| 511 | + .map(arg -> resolveExpression(arg)).collect(Collectors.toList()); |
| 512 | + |
| 513 | + // add a new call site object |
| 514 | + callSites.add(createCallSite(objectCreationExpr, "<init>", |
| 515 | + instantiatedType, arguments, false, true)); |
511 | 516 | } |
| 517 | + |
512 | 518 | return callSites; |
513 | 519 | } |
514 | 520 |
|
| 521 | + /** |
| 522 | + * Creates and returns a new CallSite object for the given expression, which can be a method-call or |
| 523 | + * object-creation expression. |
| 524 | + * |
| 525 | + * @param callExpr |
| 526 | + * @param calleeName |
| 527 | + * @param declaringType |
| 528 | + * @param arguments |
| 529 | + * @param isStaticCall |
| 530 | + * @param isConstructorCall |
| 531 | + * @return |
| 532 | + */ |
| 533 | + private static CallSite createCallSite(Expression callExpr, String calleeName, String declaringType, |
| 534 | + List<String> arguments, boolean isStaticCall, boolean isConstructorCall) { |
| 535 | + CallSite callSite = new CallSite(); |
| 536 | + callSite.setMethodName(calleeName); |
| 537 | + callSite.setDeclaringType(declaringType); |
| 538 | + callSite.setArgumentTypes(arguments); |
| 539 | + callSite.setStaticCall(isStaticCall); |
| 540 | + callSite.setConstructorCall(isConstructorCall); |
| 541 | + if (callExpr.getRange().isPresent()) { |
| 542 | + callSite.setStartLine(callExpr.getRange().get().begin.line); |
| 543 | + callSite.setStartColumn(callExpr.getRange().get().begin.column); |
| 544 | + callSite.setEndLine(callExpr.getRange().get().end.line); |
| 545 | + callSite.setEndColumn(callExpr.getRange().get().end.column); |
| 546 | + } else { |
| 547 | + callSite.setStartLine(-1); |
| 548 | + callSite.setStartColumn(-1); |
| 549 | + callSite.setEndLine(-1); |
| 550 | + callSite.setEndColumn(-1); |
| 551 | + } |
| 552 | + return callSite; |
| 553 | + } |
| 554 | + |
515 | 555 | /** |
516 | 556 | * Calculates type for the given expression and returns the resolved type name, or empty string if |
517 | 557 | * exception occurs during type resolution. |
|
0 commit comments