Handle method with common non-void inverse filter?

I am trying to set MethodHandlewhich has a common filter for return values, using MethodHandles.filterReturnValue()to do the job.

The problem is that I do not know (and do not care) about the return type, so I was hoping to just connect Object myfilter(Object obj)to the MethodHandlereturned objects to filter. However, this is apparently not allowed in the call MethodHandles.filterReturnValue().

Here, what I was hoping would work (but not) ...

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;
        }
    }
}

It MethodHandles.filterReturnValue()does not seem to be suitable for this purpose.

Then I was hoping that I could do MethodHandleone more MethodHandle, but that started to get complicated.

eg:

public Object filter(MethodHandle handle, Object ... args)
{
    Object ret = handle.invoke(args);
    System.out.printf("# filter((%s) %s)%n", ret.getClass().getName(), ret);
    return ret;
}

MethodHandleProxies LamdaMetafactory, javadoc , .

+4
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);
}
+1

Source: https://habr.com/ru/post/1692884/


All Articles