Search in sources :

Example 51 with MethodType

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

the class Signature method insertArgs.

/**
     * Insert arguments (names + types) into the signature.
     *
     * @param index the index at which to insert
     * @param names the names of the new arguments
     * @param types the types of the new arguments
     * @return a new signature with the added arguments
     */
public Signature insertArgs(int index, String[] names, Class<?>... types) {
    assert names.length == types.length : "names and types must be of the same length";
    String[] newArgNames = new String[argNames.length + names.length];
    System.arraycopy(names, 0, newArgNames, index, names.length);
    if (index != 0)
        System.arraycopy(argNames, 0, newArgNames, 0, index);
    if (argNames.length - index != 0)
        System.arraycopy(argNames, index, newArgNames, index + names.length, argNames.length - index);
    MethodType newMethodType = methodType.insertParameterTypes(index, types);
    return new Signature(newMethodType, newArgNames);
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 52 with MethodType

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

the class Signature method prependArg.

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

Example 53 with MethodType

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

the class Signature method replaceArg.

/**
     * Replace the named argument with a new name and type.
     *
     * @param oldName the old name of the argument
     * @param newName the new name of the argument; can be the same as old
     * @param newType the new type of the argument; can be the same as old
     * @return a new signature with the modified argument
     */
public Signature replaceArg(String oldName, String newName, Class<?> newType) {
    int offset = argOffset(oldName);
    String[] newArgNames = argNames;
    if (!oldName.equals(newName)) {
        newArgNames = Arrays.copyOf(argNames, argNames.length);
        newArgNames[offset] = newName;
    }
    Class<?> oldType = methodType.parameterType(offset);
    MethodType newMethodType = methodType;
    if (!oldType.equals(newType))
        newMethodType = methodType.changeParameterType(offset, newType);
    return new Signature(newMethodType, newArgNames);
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 54 with MethodType

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

the class Signature method collect.

/**
     * Collect sequential arguments matching pattern into an array. They must have the same type.
     *
     * @param newName the name of the new array argument
     * @param oldPattern the pattern of arguments to collect
     * @return a new signature with an array argument where the collected arguments were
     */
public Signature collect(String newName, String oldPattern) {
    int start = -1;
    int newCount = 0;
    int gatherCount = 0;
    Class<?> type = null;
    Pattern pattern = Pattern.compile(oldPattern);
    MethodType newType = type();
    for (int i = 0; i < argNames.length; i++) {
        if (pattern.matcher(argName(i)).matches()) {
            gatherCount++;
            newType = newType.dropParameterTypes(newCount, newCount + 1);
            Class<?> argType = argType(i);
            if (start == -1)
                start = i;
            if (type == null) {
                type = argType;
            } else {
                if (argType != type) {
                    throw new InvalidTransformException("arguments matching " + pattern + " are not all of the same type");
                }
            }
        } else {
            newCount++;
        }
    }
    if (start != -1) {
        String[] newNames = new String[newCount + 1];
        // pre
        System.arraycopy(argNames, 0, newNames, 0, start);
        // vararg
        newNames[start] = newName;
        newType = newType.insertParameterTypes(start, Array.newInstance(type, 0).getClass());
        // post
        if (newCount + 1 > start) {
            // args not at end
            System.arraycopy(argNames, start + gatherCount, newNames, start + 1, newCount - start);
        }
        return new Signature(newType, newNames);
    }
    return this;
}
Also used : Pattern(java.util.regex.Pattern) MethodType(java.lang.invoke.MethodType)

Example 55 with MethodType

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

the class Signature method appendArgs.

/**
     * Append an argument (name + type) to the signature.
     *
     * @param names the names of the arguments
     * @param types the types of the argument
     * @return a new signature with the added arguments
     */
public Signature appendArgs(String[] names, Class<?>... types) {
    assert names.length == types.length : "names and types must be of the same length";
    String[] newArgNames = new String[argNames.length + names.length];
    System.arraycopy(argNames, 0, newArgNames, 0, argNames.length);
    System.arraycopy(names, 0, newArgNames, argNames.length, names.length);
    MethodType newMethodType = methodType.appendParameterTypes(types);
    return new Signature(newMethodType, newArgNames);
}
Also used : MethodType(java.lang.invoke.MethodType)

Aggregations

MethodType (java.lang.invoke.MethodType)103 MethodHandle (java.lang.invoke.MethodHandle)37 MethodHandles (java.lang.invoke.MethodHandles)6 Test (org.junit.Test)6 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 InitializeInvokerException (cn.moyada.dubbo.faker.core.exception.InitializeInvokerException)1 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