Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static <FInter, T extends FInter> T makeLambda(Method method, Class<FInter> functionalInterface){
- try{
- var lookup=MethodHandles.privateLookupIn(method.getDeclaringClass(), MethodHandles.lookup());
- method.setAccessible(true);
- var handle=lookup.unreflect(method);
- Method functionalInterfaceFunction=Arrays.stream(functionalInterface.getMethods()).filter(m->!Modifier.isStatic(m.getModifiers())).findAny().orElseThrow();
- MethodType signature=MethodType.methodType(functionalInterfaceFunction.getReturnType(), functionalInterfaceFunction.getParameterTypes());
- CallSite site=LambdaMetafactory.metafactory(lookup,
- functionalInterfaceFunction.getName(),
- MethodType.methodType(functionalInterface),
- signature,
- handle,
- handle.type());
- //noinspection unchecked
- return (T)site.getTarget().invoke();
- }catch(Throwable e){
- throw new RuntimeException("failed to create lambda\n"+method+"\n"+functionalInterface, e);
- }
- }
Add Comment
Please, Sign In to add comment