This answer contains sample code showing how to convert a MethodHandle into a functional implementation of the interface using the same function, Java 8s lambda expressions, and using method references.
All about calling LambdaMetafactory.metafactory using a method handle, the required interface, and the name of a single abstract method and the required signature.
Both, the documentation of the methods and its documentation for the class are very detailed.
So, for your request, an example code might look like this:
MethodType methodType = MethodType.methodType(Integer.class, String.class); MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle handle = lookup.findStatic(Integer.class, "valueOf", methodType); Function<String,Integer> f=(Function<String,Integer>) LambdaMetafactory.metafactory(lookup, "apply", MethodType.methodType(Function.class), methodType.generic(), handle, methodType).getTarget().invokeExact(); System.out.println(f.apply("123"));
Here you have to take care of the types of signatures. The fourth parameter, samMethodType is of the type of the method of the original signature raw interface s, so for the raw type of Function we must implement Object apply(Object) , and instantiatedMethodType describes the Integer apply(String) method. This is why the .generic() method is called for the Type method for the fourth parameter, which converts (String)Integer to (Object)Object .
This is even more difficult for constructors, because the constructor will look for the type (String)void , while the functional type will be the same as in the case of the static method. Thus, for the static method, MethodType methods correspond to MethodType , while for the constructor we must use a different type for the search:
MethodType methodType = MethodType.methodType(Integer.class, String.class); MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle handle = lookup.findConstructor( Integer.class, MethodType.methodType(void.class, String.class)); Function<String,Integer> f=(Function<String,Integer>) LambdaMetafactory.metafactory(lookup, "apply", MethodType.methodType(Function.class), methodType.generic(), handle, methodType).getTarget().invokeExact();
But this is only for completeness, for the Integer type you should not name the constructor, but it is preferable to use the valueOf method.