Example usage for java.lang.invoke MethodHandle invokeWithArguments

List of usage examples for java.lang.invoke MethodHandle invokeWithArguments

Introduction

In this page you can find the example usage for java.lang.invoke MethodHandle invokeWithArguments.

Prototype

public Object invokeWithArguments(java.util.List<?> arguments) throws Throwable 

Source Link

Document

Performs a variable arity invocation, passing the arguments in the given list to the method handle, as if via an inexact #invoke invoke from a call site which mentions only the type Object , and whose actual argument count is the length of the argument list.

Usage

From source file:org.diorite.config.impl.proxy.ConfigInvocationHandler.java

@Nullable
private Object invoke(Config proxy, Method method, Object[] args) throws Throwable {
    Function<Object[], Object> function = this.basicMethods.get(method);
    if (function != null) {
        return function.apply(args);
    }//from   www.  j av a 2s  .c  o  m
    // check for dispatcher:
    ConfigPropertyActionInstance actionFor = this.template.getActionFor(new MethodSignature(method));
    if (actionFor != null) {
        MethodInvoker methodInvoker = new MethodInvoker(method);
        ConfigPropertyTemplate<?> templateFor = this.template.getTemplateFor(actionFor);
        if (templateFor != null) {
            ConfigPropertyValueImpl<Object> propertyValue = this.getOrCreatePredefinedValue(templateFor);
            if (methodInvoker.getReturnType() == void.class) {
                this.registerVoidMethod(method, arg -> actionFor.perform(methodInvoker, propertyValue, arg));
                actionFor.perform(methodInvoker, propertyValue, args);
                return null;
            } else {
                this.registerMethod(method, arg -> actionFor.perform(methodInvoker, propertyValue, arg));
                return actionFor.perform(methodInvoker, propertyValue, args);
            }
        }
    }

    // check for default implementation
    try {
        MethodHandle methodHandle = DioriteReflectionUtils.createLookup(method.getDeclaringClass(), -1)
                .unreflectSpecial(method, method.getDeclaringClass()).bindTo(proxy);
        Object r = methodHandle.invokeWithArguments(args);
        this.registerMethod(method, (arg) -> {
            try {
                return methodHandle.invokeWithArguments(arg);
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        });
        return r;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(
                "Config class " + this.template.getConfigType() + " executed unknown method: " + method, e);
    }
}

From source file:org.granitemc.granite.utils.Mappings.java

public static Object invoke(Object object, MethodHandle handle, Object... args) {
    try {//from w  w  w.  j  a v  a2 s .c  o m
        return handle.invokeWithArguments(ArrayUtils.add(args, 0, object));
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
    return null;
}