Search in sources :

Example 11 with MethodType

use of java.lang.invoke.MethodType in project groovy-core by groovy.

the class TypeTransformers method applyUnsharpFilter.

/**
     * Apply a transformer as filter.
     * The filter may not match exactly in the types. In this case needed
     * additional type transformations are done by {@link MethodHandle#asType(MethodType)} 
     */
public static MethodHandle applyUnsharpFilter(MethodHandle handle, int pos, MethodHandle transformer) {
    MethodType type = transformer.type();
    Class given = handle.type().parameterType(pos);
    if (type.returnType() != given || type.parameterType(0) != given) {
        transformer = transformer.asType(MethodType.methodType(given, type.parameterType(0)));
    }
    return MethodHandles.filterArguments(handle, pos, transformer);
}
Also used : MethodType(java.lang.invoke.MethodType) CachedSAMClass(org.codehaus.groovy.reflection.stdclasses.CachedSAMClass)

Example 12 with MethodType

use of java.lang.invoke.MethodType in project gravel by gravel-st.

the class BaseCallSite method getFallbackMethod.

private MethodHandle getFallbackMethod() {
    try {
        final MethodType fallbackType = MethodType.genericMethodType(type.parameterCount()).insertParameterTypes(0, BaseCallSite.class);
        final MethodHandle fallbackHandle = MethodHandles.insertArguments(MethodHandles.lookup().findStatic(BaseCallSite.class, "invocationFallback", fallbackType), 0, this);
        return fallbackHandle.asType(type);
    } catch (NoSuchMethodException | IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 13 with MethodType

use of java.lang.invoke.MethodType in project invokebinder by headius.

the class Signature method dropArg.

/**
     * Drops the argument at the given index.
     *
     * @param index the index of the argument to drop
     * @return a new signature
     */
public Signature dropArg(int index) {
    assert index < argNames.length;
    String[] newArgNames = new String[argNames.length - 1];
    if (index > 0)
        System.arraycopy(argNames, 0, newArgNames, 0, index);
    if (index < argNames.length - 1)
        System.arraycopy(argNames, index + 1, newArgNames, index, argNames.length - (index + 1));
    MethodType newType = methodType.dropParameterTypes(index, index + 1);
    return new Signature(newType, newArgNames);
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 14 with MethodType

use of java.lang.invoke.MethodType in project invokebinder by headius.

the class Signature method dropArg.

/**
     * Drops the first argument with the given name.
     *
     * @param name the name of the argument to drop
     * @return a new signature
     */
public Signature dropArg(String name) {
    String[] newArgNames = new String[argNames.length - 1];
    MethodType newType = methodType;
    for (int i = 0, j = 0; i < argNames.length; i++) {
        if (argNames[i].equals(name)) {
            newType = newType.dropParameterTypes(j, j + 1);
            continue;
        }
        newArgNames[j++] = argNames[i];
    }
    if (newType == null) {
        // arg name not found; should we error?
        return this;
    }
    return new Signature(newType, newArgNames);
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 15 with MethodType

use of java.lang.invoke.MethodType in project invokebinder by headius.

the class Signature method prependArgs.

/**
     * Prepend arguments (names + types) to the signature.
     *
     * @param names the names of the arguments
     * @param types the types of the arguments
     * @return a new signature with the added arguments
     */
public Signature prependArgs(String[] names, Class<?>... types) {
    String[] newArgNames = new String[argNames.length + names.length];
    System.arraycopy(argNames, 0, newArgNames, names.length, argNames.length);
    System.arraycopy(names, 0, newArgNames, 0, names.length);
    MethodType newMethodType = methodType.insertParameterTypes(0, types);
    return new Signature(newMethodType, newArgNames);
}
Also used : MethodType(java.lang.invoke.MethodType)

Aggregations

MethodType (java.lang.invoke.MethodType)102 MethodHandle (java.lang.invoke.MethodHandle)36 Test (org.junit.Test)6 MethodHandles (java.lang.invoke.MethodHandles)5 ArrayList (java.util.ArrayList)4 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)3 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)3 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)3 CallSite (java.lang.invoke.CallSite)3 LambdaReceiver_A (LambdaReceiver_anotherpkg.LambdaReceiver_A)2 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)2 ConnectorSession (com.facebook.presto.spi.ConnectorSession)2 PrestoException (com.facebook.presto.spi.PrestoException)2 Map (java.util.Map)2 Session (com.facebook.presto.Session)1 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)1 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)1 Parameter (com.facebook.presto.bytecode.Parameter)1 Scope (com.facebook.presto.bytecode.Scope)1 Variable (com.facebook.presto.bytecode.Variable)1