Skip to content

Commit 56212a8

Browse files
committed
Added argument expression field to call site
Signed-off-by: Saurabh Sinha <[email protected]>
1 parent 9cdcc47 commit 56212a8

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
894894
+ exception.getMessage());
895895
}
896896
// resolve arguments of the method call to types
897-
List<String> arguments = methodCallExpr.getArguments().stream().map(SymbolTable::resolveExpression)
897+
List<String> argumentTypes = methodCallExpr.getArguments().stream().map(SymbolTable::resolveExpression)
898898
.collect(Collectors.toList());
899899
// Get argument string from the callsite
900900
List<String> listOfArgumentStrings = methodCallExpr.getArguments().stream().map(Expression::toString)
@@ -930,7 +930,7 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
930930

931931

932932
callSites.add(createCallSite(methodCallExpr, methodCallExpr.getNameAsString(), receiverName, declaringType,
933-
arguments, returnType, calleeSignature, isStaticCall, false, crudOperation, crudQuery,
933+
argumentTypes, listOfArgumentStrings, returnType, calleeSignature, isStaticCall, false, crudOperation, crudQuery,
934934
accessSpecifier));
935935
}
936936

@@ -939,7 +939,11 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
939939
String instantiatedType = resolveType(objectCreationExpr.getType());
940940

941941
// resolve arguments of the constructor call to types
942-
List<String> arguments = objectCreationExpr.getArguments().stream().map(SymbolTable::resolveExpression)
942+
List<String> argumentTypes = objectCreationExpr.getArguments().stream().map(SymbolTable::resolveExpression)
943+
.collect(Collectors.toList());
944+
945+
// get argument expressions for constructor call
946+
List<String> argumentExpressions = objectCreationExpr.getArguments().stream().map(Expression::toString)
943947
.collect(Collectors.toList());
944948

945949
// resolve callee and get signature
@@ -955,7 +959,7 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
955959
.add(createCallSite(objectCreationExpr, "<init>",
956960
objectCreationExpr.getScope().isPresent() ? objectCreationExpr.getScope().get().toString()
957961
: "",
958-
instantiatedType, arguments, instantiatedType, calleeSignature, false, true, null, null,
962+
instantiatedType, argumentTypes, argumentExpressions, instantiatedType, calleeSignature, false, true, null, null,
959963
AccessSpecifier.NONE));
960964
}
961965

@@ -1006,17 +1010,24 @@ private static Optional<CRUDOperationType> findCRUDOperation(String declaringTyp
10061010
* @param calleeName
10071011
* @param receiverExpr
10081012
* @param receiverType
1009-
* @param arguments
1013+
* @param argumentTypes
1014+
* @param argumentExpr
1015+
* @param returnType
1016+
* @param calleeSignature
10101017
* @param isStaticCall
10111018
* @param isConstructorCall
1019+
* @param crudOperation,
1020+
* @param crudQuery,
1021+
* @param accessSpecifier
10121022
* @return
10131023
*/
10141024
private static CallSite createCallSite(
10151025
Expression callExpr,
10161026
String calleeName,
10171027
String receiverExpr,
10181028
String receiverType,
1019-
List<String> arguments,
1029+
List<String> argumentTypes,
1030+
List<String> argumentExpr,
10201031
String returnType,
10211032
String calleeSignature,
10221033
boolean isStaticCall,
@@ -1042,7 +1053,8 @@ private static CallSite createCallSite(
10421053
callSite.setMethodName(calleeName);
10431054
callSite.setReceiverExpr(receiverExpr);
10441055
callSite.setReceiverType(receiverType);
1045-
callSite.setArgumentTypes(arguments);
1056+
callSite.setArgumentTypes(argumentTypes);
1057+
callSite.setArgumentExpr(argumentExpr);
10461058
callSite.setReturnType(returnType);
10471059
callSite.setCalleeSignature(calleeSignature);
10481060
callSite.setStaticCall(isStaticCall);

src/main/java/com/ibm/cldk/entities/CallSite.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public class CallSite {
4242
/** List of argument types for the method call */
4343
private List<String> argumentTypes;
4444

45+
/** List of argument expressions for the method call */
46+
private List<String> argumentExpr;
47+
4548
/** Return type of the called method */
4649
private String returnType;
4750

src/test/java/com/ibm/cldk/SymbolTableTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ibm.cldk;
22

3+
import com.ibm.cldk.entities.CallSite;
34
import com.ibm.cldk.entities.Callable;
45
import com.ibm.cldk.entities.JavaCompilationUnit;
56
import com.ibm.cldk.entities.Type;
@@ -11,7 +12,7 @@
1112
import java.io.InputStream;
1213
import java.io.InputStreamReader;
1314
import java.nio.charset.StandardCharsets;
14-
import java.nio.file.Paths;
15+
import java.util.List;
1516
import java.util.Map;
1617
import java.util.stream.Collectors;
1718

@@ -36,4 +37,22 @@ public void testExtractSingleGenricsDuplicateSignature() throws IOException {
3637
Assertions.assertEquals(17, callables.size());
3738
}
3839

40+
@Test
41+
public void testCallSiteArgumentExpression() throws IOException {
42+
String javaCode = getJavaCodeForTestResource("test-applications/generics-varargs-duplicate-signature-test/Validate.java");
43+
Map<String, Type> typeDeclaration = SymbolTable.extractSingle(javaCode).getLeft()
44+
.values().iterator().next().getTypeDeclarations();
45+
Callable callable = typeDeclaration.values().iterator().next().getCallableDeclarations()
46+
.get("notEmpty(java.util.Collection<?>, java.lang.String, java.lang.Object[])");
47+
Assertions.assertNotNull(callable);
48+
for (CallSite callSite : callable.getCallSites()) {
49+
if (callSite.getMethodName().equals("requireNonNull")) {
50+
String[] expectedArgumentExpr = {"collection", "toSupplier(message, values)"};
51+
List<String> argumentExpr = callSite.getArgumentExpr();
52+
Assertions.assertArrayEquals(expectedArgumentExpr, argumentExpr.toArray(new String[0]));
53+
break;
54+
}
55+
}
56+
}
57+
3958
}

0 commit comments

Comments
 (0)