MethodHandle与一般非void返回过滤器?

时间:2018-01-29 16:26:52

标签: java methodhandle

我正在尝试使用MethodHandles.filterReturnValue()建立一个MethodHandle,其中包含返回值的通用过滤器。

我遇到的问题是我不知道(也不关心)返回类型,所以我希望只将Object myfilter(Object obj)连接成MethodHandle来过滤返回对象。但是,MethodHandles.filterReturnValue()来电显然不允许这样做。

这是我希望的工作(但不是)......

package invoke;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.util.Arrays;

public class MethodHandleReturnExample
{
    public static void main(String[] args) throws Throwable
    {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        Testcase test = new Testcase();
        MethodHandle onCount = findMethodByName(lookup, test, "onCount");
        MethodHandle onNada = findMethodByName(lookup, test, "onNada");
        MethodHandle onText = findMethodByName(lookup, test, "onText");

        onNada.invoke("hello");
        onText.invoke("world");
        onCount.invoke();
        onCount.invoke();
        onCount.invoke();
    }

    private static MethodHandle findMethodByName(MethodHandles.Lookup lookup, Object obj, String methodName) throws IllegalAccessException, NoSuchMethodException
    {
        Method method = Arrays.stream(obj.getClass().getDeclaredMethods())
                .filter(m -> m.getName().equalsIgnoreCase(methodName))
                .findFirst().get();
        MethodHandle handle = lookup.unreflect(method);
        handle = handle.bindTo(obj);

        if (handle.type().returnType() != Void.TYPE)
        {
            MethodHandle returnFilter = lookup.findVirtual(Util.class, "filter", MethodType.methodType(Object.class,Object.class));
            returnFilter = returnFilter.bindTo(new Util());
            handle = MethodHandles.filterReturnValue(handle, returnFilter);
        }
        return handle;
    }

    public static class Testcase
    {
        int count = 0;

        public int onCount()
        {
            int ret = ++count;
            System.out.printf("onCount() : %d%n", ret);
            return ret;
        }

        public void onNada(String msg)
        {
            System.out.printf("onNada(%s)%n", msg);
        }

        public String onText(String msg)
        {
            System.out.printf("onText(%s)%n", msg);
            return "[text:" + msg + "]";
        }
    }

    public static class Util
    {
        public Object filter(Object obj)
        {
            System.out.printf("# filter((%s) %s)%n", obj.getClass().getName(), obj);
            return obj;
        }
    }
}

似乎MethodHandles.filterReturnValue()不适用于此目的。

我当时希望我可以制作另一个MethodHandle的{​​{1}},但这开始变得复杂。

例如:

MethodHandle

我试图了解MethodHandleProxies甚至是LamdaMetafactory,但是从网上找到的javadoc和微薄的例子中很难理解。

1 个答案:

答案 0 :(得分:1)

filterReturnValue会调用target句柄是否使用invokeExact,因此返回类型必须完全匹配。

因此,您必须将返回类型调整为Object。最简单的方法是使用asType(这也会自动将int包装起来):

if (handle.type().returnType() != Void.TYPE) {
    handle = handle.asType(handle.type().changeReturnType(Object.class)); // <---

    MethodHandle returnFilter = lookup.findVirtual(Util.class, "filter",
            MethodType.methodType(Object.class, Object.class));
    returnFilter = returnFilter.bindTo(new Util());
    handle = MethodHandles.filterReturnValue(handle, returnFilter);
}