如果要将值绑定到您的lamba,则必须将这些参数包括到invokedtype签名中:
SimpleBean simpleBeanInstance = new SimpleBean();MethodHandles.Lookup caller = MethodHandles.lookup();MethodType getter=MethodType.methodType(Object.class);MethodHandle target=caller.findVirtual(SimpleBean.class, "getObj", getter);CallSite site = Lambdametafactory.metafactory(caller, "get", // include types of the values to bind: MethodType.methodType(Supplier.class, SimpleBean.class), getter, target, getter);MethodHandle factory = site.getTarget();factory = factory.bindTo(simpleBeanInstance);Supplier r = (Supplier) factory.invoke();assertEquals( "myCustomObject", r.get());
除了绑定值,您可以使用
Function以bean作为参数的a:
SimpleBean simpleBeanInstance = new SimpleBean();MethodHandles.Lookup caller = MethodHandles.lookup();MethodType getter=MethodType.methodType(Object.class);MethodHandle target=caller.findVirtual(SimpleBean.class, "getObj", getter);MethodType func=target.type();CallSite site = Lambdametafactory.metafactory(caller, "apply", MethodType.methodType(Function.class), func.erase(), target, func);MethodHandle factory = site.getTarget();Function r = (Function)factory.invoke();assertEquals( "myCustomObject", r.apply(simpleBeanInstance));



