Skip to content

Commit 6b8e473

Browse files
authored
Merge pull request #25 from sinha108/main
Update call sites information
2 parents 0efd2fb + 6ad7e6c commit 6b8e473

File tree

2 files changed

+63
-21
lines changed

2 files changed

+63
-21
lines changed

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

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import com.github.javaparser.ast.CompilationUnit;
88
import com.github.javaparser.ast.NodeList;
99
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.*;
1411
import com.github.javaparser.ast.nodeTypes.NodeWithName;
1512
import com.github.javaparser.ast.stmt.BlockStmt;
1613
import com.github.javaparser.ast.type.ReferenceType;
@@ -479,39 +476,82 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
479476
}
480477
for (MethodCallExpr methodCallExpr : callableBody.get().findAll(MethodCallExpr.class)) {
481478
// resolve declaring type for called method
479+
boolean isStaticCall = false;
482480
String declaringType = "";
483481
if (methodCallExpr.getScope().isPresent()) {
482+
Expression scopeExpr = methodCallExpr.getScope().get();
484483
declaringType = resolveExpression(methodCallExpr.getScope().get());
485484
if (declaringType.contains(" | ")) {
486485
declaringType = declaringType.split(" \\| ")[0];
487486
}
487+
String declaringTypeName = declaringType.contains(".") ?
488+
declaringType.substring(declaringType.lastIndexOf(".")+1) : declaringType;
489+
if (declaringTypeName.equals(scopeExpr.toString())) {
490+
isStaticCall = true;
491+
}
488492
}
489-
490493
// resolve arguments of the method call to types
491494
List<String> arguments = methodCallExpr.getArguments().stream()
492495
.map(arg -> resolveExpression(arg)).collect(Collectors.toList());
493-
494496
// 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());
509508
}
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));
511516
}
517+
512518
return callSites;
513519
}
514520

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+
515555
/**
516556
* Calculates type for the given expression and returns the resolved type name, or empty string if
517557
* exception occurs during type resolution.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public class CallSite {
99
private String methodName;
1010
private String declaringType;
1111
private List<String> argumentTypes;
12+
private boolean isStaticCall;
13+
private boolean isConstructorCall;
1214
private int startLine;
1315
private int startColumn;
1416
private int endLine;

0 commit comments

Comments
 (0)