/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.function.Function;
import sun.reflect.MethodAccessor;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main(String[] args) throws Exception {
      Function<String, String> sayHello = name -> "Hello, " + name;
      Method m = getMethodFromLambda(sayHello);
      registerFunction("World", m);
    }

    static void registerFunction(String name, Method method) throws Exception {
      String result = (String) method.invoke(null, name);
      System.out.println("result = " + result);
    }

    private static Method getMethodFromLambda(Function<String, String> lambda) throws Exception {
      Constructor<?> c = Method.class.getDeclaredConstructors()[0];
      c.setAccessible(true);
      Method m = (Method) c.newInstance(null, null, null, null, null, 0, 0, null, null, null, null);
      Field ma = Method.class.getDeclaredField("methodAccessor");
      ma.setAccessible(true);
      ma.set(m, new LambdaAccessor(array -> lambda.apply((String) array[0])));

      return m;
    }

    static class LambdaAccessor implements MethodAccessor {
      private final Function<Object[], Object> lambda;
      public LambdaAccessor(Function<Object[], Object> lambda) {
        this.lambda = lambda;
      }

      @Override public Object invoke(Object o, Object[] os) {
        return lambda.apply(os);
      }
    }
}