All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.vality.woody.api.proxy.HandleMethodCallerFactory Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
package dev.vality.woody.api.proxy;

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

public class HandleMethodCallerFactory implements MethodCallerFactory {

    @Override
    public InstanceMethodCaller getInstance(InvocationTargetProvider targetProvider, Method method) {

        Object target = targetProvider.getTarget();
        try {
            MethodHandle mh = MethodHandles.lookup().findVirtual(target.getClass(), method.getName(),
                            MethodType.methodType(method.getReturnType(), method.getParameterTypes()))
                    .asSpreader(Object[].class, method.getParameterCount());

            return new InstanceMethodCaller(method) {
                @Override
                public Object call(Object source, Object[] args) throws Throwable {

                    return mh.invokeWithArguments(target, args);

                }
            };

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            targetProvider.releaseTarget(target);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy