Skip to content

Commit 26db1ea

Browse files
committed
Added receiverExpr field to calls site info; renamed declaringType to receiverType.
Removed redundant calledMethodDeclaringType field from callable Signed-off-by: Saurabh Sinha <sinha108@gmail.com>
1 parent dce2755 commit 26db1ea

File tree

3 files changed

+14
-37
lines changed

3 files changed

+14
-37
lines changed

src/main/java/com/ibm/northstar/SymbolTable.java

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara
299299
callableNode.setReferencedTypes(getReferencedTypes(body));
300300
callableNode.setCode(body.isPresent() ? body.get().toString() : "");
301301

302-
callableNode.setCalledMethodDeclaringTypes(getCalledMethodDeclaringTypes(body));
303302
callableNode.setAccessedFields(getAccessedFields(body, classFields, typeName));
304303
callableNode.setCallSites(getCallSites(body));
305304
callableNode.setVariableDeclarations(getVariableDeclarations(body));
@@ -434,34 +433,6 @@ private static List<String> getAccessedFields(Optional<BlockStmt> callableBody,
434433
return new ArrayList<>(accessedFields);
435434
}
436435

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-
465436
/**
466437
* Returns information about call sites in the given callable. The information includes:
467438
* the method name, the declaring type name, and types of arguments used in method call.
@@ -478,9 +449,11 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
478449
// resolve declaring type for called method
479450
boolean isStaticCall = false;
480451
String declaringType = "";
452+
String receiverName = "";
481453
if (methodCallExpr.getScope().isPresent()) {
482454
Expression scopeExpr = methodCallExpr.getScope().get();
483-
declaringType = resolveExpression(methodCallExpr.getScope().get());
455+
receiverName = scopeExpr.toString();
456+
declaringType = resolveExpression(scopeExpr);
484457
if (declaringType.contains(" | ")) {
485458
declaringType = declaringType.split(" \\| ")[0];
486459
}
@@ -494,7 +467,7 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
494467
List<String> arguments = methodCallExpr.getArguments().stream()
495468
.map(arg -> resolveExpression(arg)).collect(Collectors.toList());
496469
// add a new call site object
497-
callSites.add(createCallSite(methodCallExpr, methodCallExpr.getNameAsString(), declaringType,
470+
callSites.add(createCallSite(methodCallExpr, methodCallExpr.getNameAsString(), receiverName, declaringType,
498471
arguments, isStaticCall, false));
499472
}
500473

@@ -512,6 +485,7 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
512485

513486
// add a new call site object
514487
callSites.add(createCallSite(objectCreationExpr, "<init>",
488+
objectCreationExpr.getScope().isPresent() ? objectCreationExpr.getScope().get().toString() : "",
515489
instantiatedType, arguments, false, true));
516490
}
517491

@@ -524,17 +498,20 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
524498
*
525499
* @param callExpr
526500
* @param calleeName
527-
* @param declaringType
501+
* @param receiverExpr
502+
* @param receiverType
528503
* @param arguments
529504
* @param isStaticCall
530505
* @param isConstructorCall
531506
* @return
532507
*/
533-
private static CallSite createCallSite(Expression callExpr, String calleeName, String declaringType,
534-
List<String> arguments, boolean isStaticCall, boolean isConstructorCall) {
508+
private static CallSite createCallSite(Expression callExpr, String calleeName, String receiverExpr,
509+
String receiverType, List<String> arguments, boolean isStaticCall,
510+
boolean isConstructorCall) {
535511
CallSite callSite = new CallSite();
536512
callSite.setMethodName(calleeName);
537-
callSite.setDeclaringType(declaringType);
513+
callSite.setReceiverExpr(receiverExpr);
514+
callSite.setReceiverType(receiverType);
538515
callSite.setArgumentTypes(arguments);
539516
callSite.setStaticCall(isStaticCall);
540517
callSite.setConstructorCall(isConstructorCall);

src/main/java/com/ibm/northstar/entities/CallSite.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
@Data
88
public class CallSite {
99
private String methodName;
10-
private String declaringType;
10+
private String receiverExpr;
11+
private String receiverType;
1112
private List<String> argumentTypes;
1213
private boolean isStaticCall;
1314
private boolean isConstructorCall;

src/main/java/com/ibm/northstar/entities/Callable.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public class Callable {
2121
private boolean isConstructor = false;
2222
private List<String> referencedTypes;
2323
private List<String> accessedFields;
24-
private List<String> calledMethodDeclaringTypes;
2524
private List<CallSite> callSites;
2625
private List<VariableDeclaration> variableDeclarations;
2726
private int cyclomaticComplexity;

0 commit comments

Comments
 (0)