Skip to content

Commit 61190ff

Browse files
authored
Merge pull request #26 from sinha108/main
Symbol table model update and type resolution exception handling
2 parents dce2755 + b7a59ee commit 61190ff

File tree

3 files changed

+16
-38
lines changed

3 files changed

+16
-38
lines changed

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

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.github.javaparser.ast.stmt.BlockStmt;
1313
import com.github.javaparser.ast.type.ReferenceType;
1414
import com.github.javaparser.ast.type.Type;
15+
import com.github.javaparser.resolution.MethodAmbiguityException;
1516
import com.github.javaparser.resolution.UnsolvedSymbolException;
1617
import com.github.javaparser.resolution.types.ResolvedType;
1718
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
@@ -299,7 +300,6 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara
299300
callableNode.setReferencedTypes(getReferencedTypes(body));
300301
callableNode.setCode(body.isPresent() ? body.get().toString() : "");
301302

302-
callableNode.setCalledMethodDeclaringTypes(getCalledMethodDeclaringTypes(body));
303303
callableNode.setAccessedFields(getAccessedFields(body, classFields, typeName));
304304
callableNode.setCallSites(getCallSites(body));
305305
callableNode.setVariableDeclarations(getVariableDeclarations(body));
@@ -434,34 +434,6 @@ private static List<String> getAccessedFields(Optional<BlockStmt> callableBody,
434434
return new ArrayList<>(accessedFields);
435435
}
436436

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-
465437
/**
466438
* Returns information about call sites in the given callable. The information includes:
467439
* the method name, the declaring type name, and types of arguments used in method call.
@@ -478,9 +450,11 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
478450
// resolve declaring type for called method
479451
boolean isStaticCall = false;
480452
String declaringType = "";
453+
String receiverName = "";
481454
if (methodCallExpr.getScope().isPresent()) {
482455
Expression scopeExpr = methodCallExpr.getScope().get();
483-
declaringType = resolveExpression(methodCallExpr.getScope().get());
456+
receiverName = scopeExpr.toString();
457+
declaringType = resolveExpression(scopeExpr);
484458
if (declaringType.contains(" | ")) {
485459
declaringType = declaringType.split(" \\| ")[0];
486460
}
@@ -494,7 +468,7 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
494468
List<String> arguments = methodCallExpr.getArguments().stream()
495469
.map(arg -> resolveExpression(arg)).collect(Collectors.toList());
496470
// add a new call site object
497-
callSites.add(createCallSite(methodCallExpr, methodCallExpr.getNameAsString(), declaringType,
471+
callSites.add(createCallSite(methodCallExpr, methodCallExpr.getNameAsString(), receiverName, declaringType,
498472
arguments, isStaticCall, false));
499473
}
500474

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

513487
// add a new call site object
514488
callSites.add(createCallSite(objectCreationExpr, "<init>",
489+
objectCreationExpr.getScope().isPresent() ? objectCreationExpr.getScope().get().toString() : "",
515490
instantiatedType, arguments, false, true));
516491
}
517492

@@ -524,17 +499,20 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
524499
*
525500
* @param callExpr
526501
* @param calleeName
527-
* @param declaringType
502+
* @param receiverExpr
503+
* @param receiverType
528504
* @param arguments
529505
* @param isStaticCall
530506
* @param isConstructorCall
531507
* @return
532508
*/
533-
private static CallSite createCallSite(Expression callExpr, String calleeName, String declaringType,
534-
List<String> arguments, boolean isStaticCall, boolean isConstructorCall) {
509+
private static CallSite createCallSite(Expression callExpr, String calleeName, String receiverExpr,
510+
String receiverType, List<String> arguments, boolean isStaticCall,
511+
boolean isConstructorCall) {
535512
CallSite callSite = new CallSite();
536513
callSite.setMethodName(calleeName);
537-
callSite.setDeclaringType(declaringType);
514+
callSite.setReceiverExpr(receiverExpr);
515+
callSite.setReceiverType(receiverType);
538516
callSite.setArgumentTypes(arguments);
539517
callSite.setStaticCall(isStaticCall);
540518
callSite.setConstructorCall(isConstructorCall);
@@ -580,7 +558,7 @@ private static String resolveExpression(Expression expression) {
580558
private static String resolveType(Type type) {
581559
try {
582560
return type.resolve().describe();
583-
} catch (UnsolvedSymbolException | IllegalStateException e) {
561+
} catch (UnsolvedSymbolException | IllegalStateException | MethodAmbiguityException e) {
584562
Log.warn("Could not resolve "+type.asString()+": "+e.getMessage());
585563
return type.asString();
586564
}

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)