Search in sources :

Example 56 with MethodType

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

the class JavaSystemMappingCompilerTools method runAstInit_.

@Override
public SystemMappingCompilerTools runAstInit_(JVMClass jvmClass) {
    JVMDefinedObjectType key = jvmClass.type();
    CompiledClass current = jvmClasses.get(key);
    Object[] astConstants = jvmClass.astConstants();
    if (astConstants.length == 0)
        return this;
    MethodType type = MethodType.genericMethodType(astConstants.length).changeReturnType(void.class);
    MethodHandle findStatic;
    try {
        findStatic = MethodHandles.lookup().findStatic(current.javaClass, "_astinit", type);
        findStatic.invokeWithArguments(astConstants);
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
    return this;
}
Also used : JVMMethodType(st.gravel.support.compiler.jvm.JVMMethodType) MethodType(java.lang.invoke.MethodType) JVMDefinedObjectType(st.gravel.support.compiler.jvm.JVMDefinedObjectType) MethodHandle(java.lang.invoke.MethodHandle)

Example 57 with MethodType

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

the class Signature method spread.

/**
     * Spread the trailing [] argument into its component type assigning given names.
     *
     * @param names names to use for the decomposed arguments
     * @param types types to use for the decomposed arguments
     * @return a new signature with decomposed arguments in place of the trailing array
     */
public Signature spread(String[] names, Class<?>... types) {
    assert names.length == types.length : "names and types must be of the same length";
    String[] newArgNames = new String[argNames.length - 1 + names.length];
    System.arraycopy(names, 0, newArgNames, newArgNames.length - names.length, names.length);
    System.arraycopy(argNames, 0, newArgNames, 0, argNames.length - 1);
    MethodType newMethodType = methodType.dropParameterTypes(methodType.parameterCount() - 1, methodType.parameterCount()).appendParameterTypes(types);
    return new Signature(newMethodType, newArgNames);
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 58 with MethodType

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

the class Signature method appendArg.

/**
     * Append 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 appendArg(String name, Class<?> type) {
    String[] newArgNames = new String[argNames.length + 1];
    System.arraycopy(argNames, 0, newArgNames, 0, argNames.length);
    newArgNames[argNames.length] = name;
    MethodType newMethodType = methodType.appendParameterTypes(type);
    return new Signature(newMethodType, newArgNames);
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 59 with MethodType

use of java.lang.invoke.MethodType in project es6draft by anba.

the class Properties method getStaticMethodHandle.

private static MethodHandle getStaticMethodHandle(Lookup lookup, Method method) throws IllegalAccessException {
    MethodHandle handle = lookup.unreflect(method);
    MethodType type = handle.type();
    Class<?>[] params = type.parameterArray();
    int p = 0, pcount = type.parameterCount();
    boolean callerContext = false;
    // First three parameters are (ExecutionContext, ExecutionContext?, Object=ThisValue)
    if (!(p < pcount && ExecutionContext.class.equals(params[p++]))) {
        throw new IllegalArgumentException(type.toString());
    }
    if (p < pcount && ExecutionContext.class.equals(params[p])) {
        callerContext = true;
        p++;
    }
    if (!(p < pcount && Object.class.equals(params[p++]))) {
        throw new IllegalArgumentException(type.toString());
    }
    // Always required to return Object (for now at least)
    if (!Object.class.equals(type.returnType())) {
        throw new IllegalArgumentException(type.toString());
    }
    // Collect remaining arguments into Object[]
    if (!(p + 1 == pcount && Object[].class.equals(params[p]))) {
        final int fixedArguments = callerContext ? 3 : 2;
        final boolean varargs = handle.isVarargsCollector();
        // Otherwise all trailing arguments need to be of type Object or Object[]
        for (; p < pcount; ++p) {
            if (Object.class.equals(params[p])) {
                continue;
            }
            if (p + 1 == pcount && Object[].class.equals(params[p]) && varargs) {
                continue;
            }
            throw new IllegalArgumentException(type.toString());
        }
        // Trailing Object[] arguments are no longer spread in var-args methods (jdk8u40, jdk9).
        if (varargs) {
            handle = handle.asFixedArity();
        }
        // Convert to (ExecutionContext, Object, ...) -> Object handle
        handle = toCanonical(handle, fixedArguments, varargs, method);
    }
    if (!callerContext) {
        handle = MethodHandles.dropArguments(handle, 1, ExecutionContext.class);
    }
    return handle;
}
Also used : MethodType(java.lang.invoke.MethodType) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) MethodHandle(java.lang.invoke.MethodHandle)

Example 60 with MethodType

use of java.lang.invoke.MethodType in project es6draft by anba.

the class Properties method getComputedValueMethodHandle.

private static MethodHandle getComputedValueMethodHandle(Lookup lookup, Method method) throws IllegalAccessException {
    // check: (ExecutionContext) -> Object
    MethodHandle handle = lookup.unreflect(method);
    MethodType type = handle.type();
    if (type.parameterCount() != 1 || !ExecutionContext.class.equals(type.parameterType(0))) {
        throw new IllegalArgumentException(handle.toString());
    }
    if (!Object.class.equals(type.returnType())) {
        throw new IllegalArgumentException(handle.toString());
    }
    return handle;
}
Also used : MethodType(java.lang.invoke.MethodType) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) MethodHandle(java.lang.invoke.MethodHandle)

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