Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ArrayIndexOutOfBoundsException at parameter count validation of Java interop methods #41943

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading