[jdk17u-dev] RFR: 8280377: MethodHandleProxies does not correctly invoke default methods with varags

María Arias de Reyna duke at openjdk.org
Fri Mar 1 09:23:51 UTC 2024


On Mon, 26 Feb 2024 12:52:28 GMT, María Arias de Reyna <duke at openjdk.org> wrote:

> This is a backport of https://bugs.openjdk.org/browse/JDK-8280377 MethodHandleProxies does not correctly invoke default methods with varags
> 
> I applied the same fix that was applied to version 19 in https://github.com/openjdk/jdk/commit/a183bfb436a7dd998e602c2d16486e88c390fca1

I tested with the `Main.java` example that is provided in the bug:


import java.lang.invoke.MethodHandleProxies;
import java.lang.invoke.MethodHandles;

import static java.lang.invoke.MethodType.methodType;

public class Main {

    public interface Interface
    {
        void method(Object... args);
        default void defaultMethod(Object... args) {
            method(args);
        }
        default void defaultMethod2(Object[] args) {
            method(args);
        }
    }

    private static void callMeViaMethodHandles(String foo, int bar) {
        System.out.println("foo=" + foo + ", bar=" + bar);
    }

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException
    {
        var mh = MethodHandles.lookup().findStatic(
                Main.class, "callMeViaMethodHandles", methodType(void.class, String.class, int.class)
        );
        mh = mh.asSpreader(Object[].class, mh.type().parameterCount());
        var proxy = MethodHandleProxies.asInterfaceInstance(Interface.class, mh);

        // throws incorrect exception
        proxy.defaultMethod("should be foo", 3);
        
        // works fine
        proxy.defaultMethod2(new Object[] { "should be foo", 3 });
    }
} 

With the regular Java 17 installed on my Debian, I get the expected error pre-fix (I also tested with a local build of latest master at the time, but as I built with the fix, I cannot provide right now the output for that):


$ javac Main.java ; java --version; java Main
openjdk 17.0.10 2024-01-16
OpenJDK Runtime Environment (build 17.0.10+7-Debian-1deb12u1)
OpenJDK 64-Bit Server VM (build 17.0.10+7-Debian-1deb12u1, mixed mode, sharing)
Exception in thread "main" java.lang.IllegalArgumentException: array is not of length 2
	at java.base/java.lang.invoke.MethodHandleStatics.newIllegalArgumentException(MethodHandleStatics.java:167)
	at java.base/java.lang.invoke.MethodHandleImpl.checkSpreadArgument(MethodHandleImpl.java:584)
	at java.base/java.lang.invoke.MethodHandleProxies$1.invoke(MethodHandleProxies.java:198)
	at jdk.proxy1/com.sun.proxy.jdk.proxy1.$Proxy0.method(Unknown Source)
	at Main$Interface.defaultMethod(Main.java:12)
	at java.base/java.lang.invoke.MethodHandleProxies.callDefaultMethod(MethodHandleProxies.java:354)
	at java.base/java.lang.invoke.MethodHandleProxies$1.invoke(MethodHandleProxies.java:205)
	at jdk.proxy1/com.sun.proxy.jdk.proxy1.$Proxy0.defaultMethod(Unknown Source)
	at Main.main(Main.java:32)


After fixing it, I get the correct answer:


$ ~/git/jdk17u-dev/build/linux-x86_64-server-release/jdk/bin/javac Main.java && ~/git/jdk17u-dev/build/linux-x86_64-server-release/jdk/bin/java --version && ~/git/jdk17u-dev/build/linux-x86_64-server-release/jdk/bin/java Main
openjdk 17.0.11-internal 2024-04-16
OpenJDK Runtime Environment (build 17.0.11-internal+0-adhoc.delawen.jdk17u-dev)
OpenJDK 64-Bit Server VM (build 17.0.11-internal+0-adhoc.delawen.jdk17u-dev, mixed mode)
foo=should be foo, bar=3
foo=should be foo, bar=3

-------------

PR Comment: https://git.openjdk.org/jdk17u-dev/pull/2235#issuecomment-1972812190


More information about the jdk-updates-dev mailing list