diff --git a/src/main/java/com/ibm/northstar/SymbolTable.java b/src/main/java/com/ibm/northstar/SymbolTable.java index c85c5be..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; @@ -299,7 +300,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 +434,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 +450,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 +468,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 +486,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 +499,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); @@ -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(); } 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;