Skip to content

Commit

Permalink
Merge pull request #41943 from gabilang/fix-crash-with-interop
Browse files Browse the repository at this point in the history
Fix ArrayIndexOutOfBoundsException at parameter count validation of Java interop methods
  • Loading branch information
warunalakshitha authored Jan 16, 2024
2 parents a26ad33 + 48941a9 commit c65f084
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,15 @@ private boolean isAcceptingBundledParams(JMethodRequest jMethodRequest, JMethod
return false;
}
Class<?>[] paramTypes = jMethod.getParamTypes();
if (count == reducedParamCount && isParamAssignableToBArray(paramTypes[0])) {
if (count == reducedParamCount && paramTypes.length > 0 && isParamAssignableToBArray(paramTypes[0])) {
return true;
} else if ((count == (reducedParamCount + 1)) && isParamAssignableToBArray(paramTypes[1])) {
} else if ((count == (reducedParamCount + 1)) && paramTypes.length > 1 &&
isParamAssignableToBArray(paramTypes[1])) {
// This is for object interop functions when self is passed as a parameter
jMethod.setReceiverType(jMethodRequest.receiverType);
return jMethodRequest.receiverType != null;
} else if ((count == (reducedParamCount + 2)) && isParamAssignableToBArray(paramTypes[2])) {
} else if ((count == (reducedParamCount + 2)) && paramTypes.length > 2 &&
isParamAssignableToBArray(paramTypes[2])) {
// This is for object interop functions when both BalEnv and self is passed as parameters.
if (jMethodRequest.receiverType != null) {
jMethod.setReceiverType(jMethodRequest.receiverType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void testMethodNotFound6() {
public void testMethodNotFound7() {
String path = "test-src/javainterop/negative/method_not_found7.bal";
CompileResult compileResult = BCompileUtil.compile(path);
Assert.assertEquals(compileResult.getDiagnostics().length, 3);
Assert.assertEquals(compileResult.getDiagnostics().length, 4);
String message = "{ballerina/jballerina.java}METHOD_NOT_FOUND 'No such public static method '%s' with " +
"'%s' parameter(s) found in class '%s''";
BAssertUtil.validateError(compileResult, 0, String.format(message, "getPrintableStackTrace", "1",
Expand All @@ -149,6 +149,10 @@ public void testMethodNotFound7() {
"{ballerina/jballerina.java}METHOD_NOT_FOUND 'No such public method 'concat' " +
"with '3' parameter(s) found in class 'io.ballerina.runtime.api.values.BString''",
"method_not_found7.bal", 27, 1);
BAssertUtil.validateError(compileResult, 3,
"{ballerina/jballerina.java}METHOD_NOT_FOUND 'No such public static method 'indexOf' " +
"with '0' parameter(s) found in class 'java.lang.String''",
"method_not_found7.bal", 32, 1);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ function concatString(handle h, string s1, string s2) returns handle = @java:Met
'class: "io.ballerina.runtime.api.values.BString",
name: "concat"
} external;

function indexOf() returns byte = @java:Method {
'class: "java.lang.String"
} external;

0 comments on commit c65f084

Please sign in to comment.