-
Bug
-
Resolution: Not an Issue
-
P2
-
8
-
b115
-
generic
-
generic
Let's consider following code:
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Arrays;
interface MyFunctionalInterface {
Object invokeMethodReference(Object... args) throws Throwable;
}
public class Test {
public Object m(Object ... args) {
System.out.println("m: " + Arrays.toString(args));
return "m";
}
public static void main(String argv[]) throws Throwable {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType mt = MethodType.methodType(String.class, char.class, char.class);
MethodHandle ms = lookup.findVirtual(String.class, "replace", mt);
System.out.println("result: " + ms.invoke("some string to search", 's', 'o'));
MyFunctionalInterface methodRefSimple = new Test()::m;
methodRefSimple.invokeMethodReference("first", "second");
MyFunctionalInterface methodRefSignPoly = ms::invoke;
System.out.println(methodRefSignPoly.invokeMethodReference("some string to search", 's', 'o'));
}
}
it causes following output:
result: oome otring to oearch
m: [first, second]
Exception in thread "main" java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle(String,char,char)String to (Object[])Object
at java.lang.invoke.MethodHandle.asTypeUncached(MethodHandle.java:776)
at java.lang.invoke.MethodHandle.asType(MethodHandle.java:770)
at java.lang.invoke.Invokers.checkGenericType(Invokers.java:366)
at Test$$Lambda$2/1761291320.invokeMethodReference(Unknown Source)
at Test.main(Test.java:26)
The following line:
"System.out.println(methodRefSignPoly.invokeMethodReference("some string to search", 's', 'o'));"
causes java.lang.invoke.WrongMethodTypeException to be thrown.
JLS doesn't prohibit method references to signature polymorphic methods in any way. So I could believe it's a bug.
The code above is attached for your convenience.
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Arrays;
interface MyFunctionalInterface {
Object invokeMethodReference(Object... args) throws Throwable;
}
public class Test {
public Object m(Object ... args) {
System.out.println("m: " + Arrays.toString(args));
return "m";
}
public static void main(String argv[]) throws Throwable {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType mt = MethodType.methodType(String.class, char.class, char.class);
MethodHandle ms = lookup.findVirtual(String.class, "replace", mt);
System.out.println("result: " + ms.invoke("some string to search", 's', 'o'));
MyFunctionalInterface methodRefSimple = new Test()::m;
methodRefSimple.invokeMethodReference("first", "second");
MyFunctionalInterface methodRefSignPoly = ms::invoke;
System.out.println(methodRefSignPoly.invokeMethodReference("some string to search", 's', 'o'));
}
}
it causes following output:
result: oome otring to oearch
m: [first, second]
Exception in thread "main" java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle(String,char,char)String to (Object[])Object
at java.lang.invoke.MethodHandle.asTypeUncached(MethodHandle.java:776)
at java.lang.invoke.MethodHandle.asType(MethodHandle.java:770)
at java.lang.invoke.Invokers.checkGenericType(Invokers.java:366)
at Test$$Lambda$2/1761291320.invokeMethodReference(Unknown Source)
at Test.main(Test.java:26)
The following line:
"System.out.println(methodRefSignPoly.invokeMethodReference("some string to search", 's', 'o'));"
causes java.lang.invoke.WrongMethodTypeException to be thrown.
JLS doesn't prohibit method references to signature polymorphic methods in any way. So I could believe it's a bug.
The code above is attached for your convenience.
- relates to
-
JDK-8028690 Lambda Spec: Clarify how parameter/return types are derived for MethodHandle::invoke
-
- Closed
-
-
JDK-8028739 javac generates incorrect descriptor for MethodHandle::invoke
-
- Closed
-