JDK-8367022 : MethodHandles.foldArguments handle negative pos
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang.invoke
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2025-09-01
  • Updated: 2025-09-06
Related Reports
Causes :  
Description
A DESCRIPTION OF THE PROBLEM :
MethodHandles.foldArguments(MethodHandle, Int, MethodHandle) needs 0 based index pos.
But negative pos triggers ArrayIndexOutOfBoundsException, rather than IAE.

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import org.testng.annotations.*;

public class FoldTest {


    @Test(expectedExceptions = IllegalArgumentException.class)
    public void testNegativePosition() throws Throwable {
        MethodHandle target = MethodHandles.lookup().findStatic(FoldTest.class, "target",
                MethodType.methodType(int.class, int.class, int.class));
        MethodHandle combiner = MethodHandles.lookup().findStatic(FoldTest.class, "combiner",
                MethodType.methodType(int.class, int.class));
        
        MethodHandles.foldArguments(target, -1, combiner);
    }

    private static int target(int x, int y) { return 0; }
    private static int combiner(int x) { return 0; }
}