Skip to content

Commit

Permalink
Merge pull request #11 from sinha108/callable-callsites-info
Browse files Browse the repository at this point in the history
Added information about call sites to callable
  • Loading branch information
rahlk authored Apr 24, 2024
2 parents 497e5b5 + 036fc67 commit 51a2bbc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/main/java/com/ibm/northstar/SymbolTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara

callableNode.setCalledMethodDeclaringTypes(getCalledMethodDeclaringTypes(body));
callableNode.setAccessedFields(getAccessedFields(body, classFields, typeName));
callableNode.setCallSites(getCallSites(body));

String callableSignature = (callableDecl instanceof MethodDeclaration) ? callableDecl.getSignature().asString() : callableDecl.getSignature().asString().replace(callableDecl.getSignature().getName(), "<init>");
return Pair.of(callableSignature, callableNode);
Expand Down Expand Up @@ -425,6 +426,53 @@ private static List<String> getCalledMethodDeclaringTypes(Optional<BlockStmt> ca
return new ArrayList<>(calledMethodDeclaringTypes);
}

/**
* Returns information about call sites in the given callable. The information includes:
* the method name, the declaring type name, and types of arguments used in method call.
*
* @param callableBody callable to compute call-site information for
* @return list of call sites
*/
private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
List<CallSite> callSites = new ArrayList<>();
if (callableBody.isEmpty()) {
return callSites;
}
for (MethodCallExpr methodCallExpr : callableBody.get().findAll(MethodCallExpr.class)) {
// resolve declaring type for called method
String declaringType = "";
if (methodCallExpr.getScope().isPresent()) {
declaringType = resolveExpression(methodCallExpr.getScope().get());
if (declaringType.contains(" | ")) {
declaringType = declaringType.split(" \\| ")[0];
}
}

// resolve arguments of the method call to types
List<String> arguments = methodCallExpr.getArguments().stream()
.map(arg -> resolveExpression(arg)).collect(Collectors.toList());

// add a new call site object
CallSite callSite = new CallSite();
callSite.setMethodName(methodCallExpr.getNameAsString());
callSite.setDeclaringType(declaringType);
callSite.setArgumentTypes(arguments);
if (methodCallExpr.getRange().isPresent()) {
callSite.setStartLine(methodCallExpr.getRange().get().begin.line);
callSite.setStartColumn(methodCallExpr.getRange().get().begin.column);
callSite.setEndLine(methodCallExpr.getRange().get().end.line);
callSite.setEndColumn(methodCallExpr.getRange().get().end.column);
} else {
callSite.setStartLine(-1);
callSite.setStartColumn(-1);
callSite.setEndLine(-1);
callSite.setEndColumn(-1);
}
callSites.add(callSite);
}
return callSites;
}

/**
* Calculates type for the given expression and returns the resolved type name, or empty string if
* exception occurs during type resolution.
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/ibm/northstar/entities/CallSite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ibm.northstar.entities;

import lombok.Data;

import java.util.List;

@Data
public class CallSite {
private String methodName;
private String declaringType;
private List<String> argumentTypes;
private int startLine;
private int startColumn;
private int endLine;
private int endColumn;
}
1 change: 1 addition & 0 deletions src/main/java/com/ibm/northstar/entities/Callable.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public class Callable {
private List<String> referencedTypes;
private List<String> accessedFields;
private List<String> calledMethodDeclaringTypes;
private List<CallSite> callSites;
}

0 comments on commit 51a2bbc

Please sign in to comment.