Skip to content

Commit e580cfa

Browse files
committed
Updated call sites information to indicate static calls and include constructor calls.
Signed-off-by: Saurabh Sinha <[email protected]>
1 parent 0efd2fb commit e580cfa

File tree

2 files changed

+62
-21
lines changed

2 files changed

+62
-21
lines changed

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

Lines changed: 60 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,81 @@ 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.substring(declaringType.lastIndexOf(".")+1);
488+
if (declaringTypeName.equals(scopeExpr.toString())) {
489+
isStaticCall = true;
490+
}
488491
}
489-
490492
// resolve arguments of the method call to types
491493
List<String> arguments = methodCallExpr.getArguments().stream()
492494
.map(arg -> resolveExpression(arg)).collect(Collectors.toList());
493-
494495
// 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);
496+
callSites.add(createCallSite(methodCallExpr, methodCallExpr.getNameAsString(), declaringType,
497+
arguments, isStaticCall, false));
498+
}
499+
500+
for (ObjectCreationExpr objectCreationExpr : callableBody.get().findAll(ObjectCreationExpr.class)) {
501+
// resolve declaring type for called method
502+
String instantiatedType = objectCreationExpr.getTypeAsString();
503+
try {
504+
instantiatedType = objectCreationExpr.getType().resolve().describe();
505+
} catch (UnsolvedSymbolException | IllegalStateException e) {
506+
Log.warn("Could not resolve "+instantiatedType+": "+e.getMessage());
509507
}
510-
callSites.add(callSite);
508+
// resolve arguments of the constructor call to types
509+
List<String> arguments = objectCreationExpr.getArguments().stream()
510+
.map(arg -> resolveExpression(arg)).collect(Collectors.toList());
511+
512+
// add a new call site object
513+
callSites.add(createCallSite(objectCreationExpr, "<init>",
514+
instantiatedType, arguments, false, true));
511515
}
516+
512517
return callSites;
513518
}
514519

520+
/**
521+
* Creates and returns a new CallSite object for the given expression, which can be a method-call or
522+
* object-creation expression.
523+
*
524+
* @param callExpr
525+
* @param calleeName
526+
* @param declaringType
527+
* @param arguments
528+
* @param isStaticCall
529+
* @param isConstructorCall
530+
* @return
531+
*/
532+
private static CallSite createCallSite(Expression callExpr, String calleeName, String declaringType,
533+
List<String> arguments, boolean isStaticCall, boolean isConstructorCall) {
534+
CallSite callSite = new CallSite();
535+
callSite.setMethodName(calleeName);
536+
callSite.setDeclaringType(declaringType);
537+
callSite.setArgumentTypes(arguments);
538+
callSite.setStaticCall(isStaticCall);
539+
callSite.setConstructorCall(isConstructorCall);
540+
if (callExpr.getRange().isPresent()) {
541+
callSite.setStartLine(callExpr.getRange().get().begin.line);
542+
callSite.setStartColumn(callExpr.getRange().get().begin.column);
543+
callSite.setEndLine(callExpr.getRange().get().end.line);
544+
callSite.setEndColumn(callExpr.getRange().get().end.column);
545+
} else {
546+
callSite.setStartLine(-1);
547+
callSite.setStartColumn(-1);
548+
callSite.setEndLine(-1);
549+
callSite.setEndColumn(-1);
550+
}
551+
return callSite;
552+
}
553+
515554
/**
516555
* Calculates type for the given expression and returns the resolved type name, or empty string if
517556
* 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)