Quantcast
Viewing all articles
Browse latest Browse all 36

What does “MethodHandle::invoke” mean in Java? [duplicate]

Look the follow code:

import java.lang.invoke.MethodHandle;public class Demo {    interface TFunction<T, R> {        R apply(T t) throws Throwable;    }    public class MyMethodHandle {        public final native Object invoke(Object... args) throws Throwable;    }    public static void main(String[] args) {        TFunction<MyMethodHandle, Object> f1 = m -> m.invoke();        TFunction<MyMethodHandle, Object> f2 = MyMethodHandle::invoke;        TFunction<MethodHandle, Object> f3 = m -> m.invoke();        TFunction<MethodHandle, Object> f4 = MethodHandle::invoke; // here is line 19    }}

run it(java 8), and I get a exception at f4:

Exception in thread "main" java.lang.BootstrapMethodError: call site initialization exception    at java.lang.invoke.CallSite.makeSite(CallSite.java:341)    at java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(MethodHandleNatives.java:307)    at java.lang.invoke.MethodHandleNatives.linkCallSite(MethodHandleNatives.java:297)    at x.xxx.Demo.main(Demo.java:19)Caused by: java.lang.invoke.LambdaConversionException: Incorrect number of parameters for instance method invokeVirtual java.lang.invoke.MethodHandle.invoke:(Object)Object; 0 captured parameters, 1 functional interface method parameters, 1 implementation parameters    at java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(AbstractValidatingLambdaMetafactory.java:193)    at java.lang.invoke.LambdaMetafactory.metafactory(LambdaMetafactory.java:303)    at java.lang.invoke.CallSite.makeSite(CallSite.java:302)    ... 3 more

my question is what is the difference between f3 and f4? and why f2 can work but f4 not?

I have no idea, I searched for this but got no answer.


Viewing all articles
Browse latest Browse all 36

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>