Skip to content

Commit

Permalink
Merge pull request #26 from sinha108/main
Browse files Browse the repository at this point in the history
Symbol table model update and type resolution exception handling
  • Loading branch information
rahlk authored Jun 5, 2024
2 parents dce2755 + b7a59ee commit 61190ff
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 38 deletions.
50 changes: 14 additions & 36 deletions src/main/java/com/ibm/northstar/SymbolTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.type.ReferenceType;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.resolution.MethodAmbiguityException;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
Expand Down Expand Up @@ -299,7 +300,6 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara
callableNode.setReferencedTypes(getReferencedTypes(body));
callableNode.setCode(body.isPresent() ? body.get().toString() : "");

callableNode.setCalledMethodDeclaringTypes(getCalledMethodDeclaringTypes(body));
callableNode.setAccessedFields(getAccessedFields(body, classFields, typeName));
callableNode.setCallSites(getCallSites(body));
callableNode.setVariableDeclarations(getVariableDeclarations(body));
Expand Down Expand Up @@ -434,34 +434,6 @@ private static List<String> getAccessedFields(Optional<BlockStmt> callableBody,
return new ArrayList<>(accessedFields);
}

/**
* For method calls occurring in the given callable, computes the set of declaring types and returns
* their qualified names.
*
* @param callableBody Callable to compute declaring types for called methods
* @return List of qualified type names for method calls
*/
private static List<String> getCalledMethodDeclaringTypes(Optional<BlockStmt> callableBody) {
Set<String> calledMethodDeclaringTypes = new HashSet<>();
callableBody.ifPresent(cb -> cb.findAll(MethodCallExpr.class)
.stream()
.map(expr -> {
String resolvedExpr = "";
if (expr.getScope().isPresent()) {
resolvedExpr = resolveExpression(expr.getScope().get());
if (resolvedExpr.contains(" | ")) {
return resolvedExpr.split(" \\| ");
}
}
return new String[]{resolvedExpr};
})
.flatMap(type -> Arrays.stream(type))
.filter(type -> !type.isEmpty())
.forEach(calledMethodDeclaringTypes::add)
);
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.
Expand All @@ -478,9 +450,11 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
// resolve declaring type for called method
boolean isStaticCall = false;
String declaringType = "";
String receiverName = "";
if (methodCallExpr.getScope().isPresent()) {
Expression scopeExpr = methodCallExpr.getScope().get();
declaringType = resolveExpression(methodCallExpr.getScope().get());
receiverName = scopeExpr.toString();
declaringType = resolveExpression(scopeExpr);
if (declaringType.contains(" | ")) {
declaringType = declaringType.split(" \\| ")[0];
}
Expand All @@ -494,7 +468,7 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
List<String> arguments = methodCallExpr.getArguments().stream()
.map(arg -> resolveExpression(arg)).collect(Collectors.toList());
// add a new call site object
callSites.add(createCallSite(methodCallExpr, methodCallExpr.getNameAsString(), declaringType,
callSites.add(createCallSite(methodCallExpr, methodCallExpr.getNameAsString(), receiverName, declaringType,
arguments, isStaticCall, false));
}

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

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

Expand All @@ -524,17 +499,20 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
*
* @param callExpr
* @param calleeName
* @param declaringType
* @param receiverExpr
* @param receiverType
* @param arguments
* @param isStaticCall
* @param isConstructorCall
* @return
*/
private static CallSite createCallSite(Expression callExpr, String calleeName, String declaringType,
List<String> arguments, boolean isStaticCall, boolean isConstructorCall) {
private static CallSite createCallSite(Expression callExpr, String calleeName, String receiverExpr,
String receiverType, List<String> arguments, boolean isStaticCall,
boolean isConstructorCall) {
CallSite callSite = new CallSite();
callSite.setMethodName(calleeName);
callSite.setDeclaringType(declaringType);
callSite.setReceiverExpr(receiverExpr);
callSite.setReceiverType(receiverType);
callSite.setArgumentTypes(arguments);
callSite.setStaticCall(isStaticCall);
callSite.setConstructorCall(isConstructorCall);
Expand Down Expand Up @@ -580,7 +558,7 @@ private static String resolveExpression(Expression expression) {
private static String resolveType(Type type) {
try {
return type.resolve().describe();
} catch (UnsolvedSymbolException | IllegalStateException e) {
} catch (UnsolvedSymbolException | IllegalStateException | MethodAmbiguityException e) {
Log.warn("Could not resolve "+type.asString()+": "+e.getMessage());
return type.asString();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/ibm/northstar/entities/CallSite.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
@Data
public class CallSite {
private String methodName;
private String declaringType;
private String receiverExpr;
private String receiverType;
private List<String> argumentTypes;
private boolean isStaticCall;
private boolean isConstructorCall;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/ibm/northstar/entities/Callable.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class Callable {
private boolean isConstructor = false;
private List<String> referencedTypes;
private List<String> accessedFields;
private List<String> calledMethodDeclaringTypes;
private List<CallSite> callSites;
private List<VariableDeclaration> variableDeclarations;
private int cyclomaticComplexity;
Expand Down

0 comments on commit 61190ff

Please sign in to comment.