From 26db1eade8728d5c4f03761063069a7dfd21e60a Mon Sep 17 00:00:00 2001 From: Saurabh Sinha Date: Tue, 4 Jun 2024 17:18:54 -0400 Subject: [PATCH 1/2] Added receiverExpr field to calls site info; renamed declaringType to receiverType. Removed redundant calledMethodDeclaringType field from callable Signed-off-by: Saurabh Sinha --- .../java/com/ibm/northstar/SymbolTable.java | 47 +++++-------------- .../com/ibm/northstar/entities/CallSite.java | 3 +- .../com/ibm/northstar/entities/Callable.java | 1 - 3 files changed, 14 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/ibm/northstar/SymbolTable.java b/src/main/java/com/ibm/northstar/SymbolTable.java index c85c5be..727a92b 100644 --- a/src/main/java/com/ibm/northstar/SymbolTable.java +++ b/src/main/java/com/ibm/northstar/SymbolTable.java @@ -299,7 +299,6 @@ private static Pair 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)); @@ -434,34 +433,6 @@ private static List getAccessedFields(Optional 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 getCalledMethodDeclaringTypes(Optional callableBody) { - Set 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. @@ -478,9 +449,11 @@ private static List getCallSites(Optional 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]; } @@ -494,7 +467,7 @@ private static List getCallSites(Optional callableBody) { List 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)); } @@ -512,6 +485,7 @@ private static List getCallSites(Optional callableBody) { // add a new call site object callSites.add(createCallSite(objectCreationExpr, "", + objectCreationExpr.getScope().isPresent() ? objectCreationExpr.getScope().get().toString() : "", instantiatedType, arguments, false, true)); } @@ -524,17 +498,20 @@ private static List getCallSites(Optional 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 arguments, boolean isStaticCall, boolean isConstructorCall) { + private static CallSite createCallSite(Expression callExpr, String calleeName, String receiverExpr, + String receiverType, List 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); diff --git a/src/main/java/com/ibm/northstar/entities/CallSite.java b/src/main/java/com/ibm/northstar/entities/CallSite.java index d34015a..53d82f7 100644 --- a/src/main/java/com/ibm/northstar/entities/CallSite.java +++ b/src/main/java/com/ibm/northstar/entities/CallSite.java @@ -7,7 +7,8 @@ @Data public class CallSite { private String methodName; - private String declaringType; + private String receiverExpr; + private String receiverType; private List argumentTypes; private boolean isStaticCall; private boolean isConstructorCall; diff --git a/src/main/java/com/ibm/northstar/entities/Callable.java b/src/main/java/com/ibm/northstar/entities/Callable.java index f8bb059..a165b98 100644 --- a/src/main/java/com/ibm/northstar/entities/Callable.java +++ b/src/main/java/com/ibm/northstar/entities/Callable.java @@ -21,7 +21,6 @@ public class Callable { private boolean isConstructor = false; private List referencedTypes; private List accessedFields; - private List calledMethodDeclaringTypes; private List callSites; private List variableDeclarations; private int cyclomaticComplexity; From b7a59eeab26b45ab53d7546fd6872c76943c8625 Mon Sep 17 00:00:00 2001 From: Saurabh Sinha Date: Tue, 4 Jun 2024 17:26:40 -0400 Subject: [PATCH 2/2] Added handling of MethodAmbiguityException during type resolution Signed-off-by: Saurabh Sinha --- src/main/java/com/ibm/northstar/SymbolTable.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/ibm/northstar/SymbolTable.java b/src/main/java/com/ibm/northstar/SymbolTable.java index 727a92b..8cc5a78 100644 --- a/src/main/java/com/ibm/northstar/SymbolTable.java +++ b/src/main/java/com/ibm/northstar/SymbolTable.java @@ -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; @@ -557,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(); }