Search in sources :

Example 6 with MethodType

use of java.lang.invoke.MethodType in project quasar by puniverse.

the class DynamicMethodHandleRecord method fixMethodHandleType.

private static MethodHandle fixMethodHandleType(Field field, MethodHandle mh) throws IllegalAccessException {
    if (mh == null)
        return null;
    final MethodType origType = mh.type();
    final Class<?>[] params = origType.parameterArray();
    params[0] = Object.class;
    for (int i = 1; i < params.length; i++) {
        if (!params[i].isPrimitive())
            params[i] = Object.class;
    }
    Class<?> rtype = origType.returnType();
    if (field instanceof Field.ArrayField && rtype.isArray()) {
        if (!rtype.getComponentType().isPrimitive())
            rtype = Object[].class;
    } else if (!rtype.isPrimitive())
        rtype = Object.class;
    final MethodType mt = MethodType.methodType(rtype, params);
    return mh.asType(mt);
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 7 with MethodType

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

the class NativeCalls method getNativeMethodHandle.

private static MethodHandle getNativeMethodHandle(String name) {
    if (!name.startsWith("native:")) {
        throw new IllegalArgumentException();
    }
    String methodName = name.substring("native:".length());
    for (Class<?> lookupClass : lookupClasses) {
        MethodLookup lookup = new MethodLookup(MethodHandles.publicLookup().in(lookupClass));
        Method m = findMethod(lookup, methodName);
        if (m == null) {
            continue;
        }
        MethodHandle mh;
        try {
            mh = lookup.getLookup().unreflect(m);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException();
        }
        // Allow to omit execution context argument.
        MethodType type = mh.type();
        if (type.parameterCount() == 0 || !type.parameterType(0).equals(ExecutionContext.class)) {
            mh = MethodHandles.dropArguments(mh, 0, ExecutionContext.class);
        }
        // Allow void return type.
        if (type.returnType() == void.class) {
            mh = MethodHandles.filterReturnValue(mh, MethodHandles.constant(Object.class, UNDEFINED));
        }
        return mh;
    }
    throw new IllegalArgumentException();
}
Also used : MethodType(java.lang.invoke.MethodType) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) Method(java.lang.reflect.Method) MethodHandle(java.lang.invoke.MethodHandle)

Example 8 with MethodType

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

the class Properties method toCanonical.

private static MethodHandle toCanonical(MethodHandle handle, int fixedArguments, boolean varargs, Method method) {
    assert !handle.isVarargsCollector();
    MethodType type = handle.type();
    int actual = type.parameterCount() - fixedArguments - (varargs ? 1 : 0);
    Object[] defaults = method != null ? methodDefaults(method, fixedArguments, actual) : null;
    MethodHandle filter = Parameters.filter(actual, varargs, defaults);
    MethodHandle spreader = MethodHandles.spreadInvoker(type, fixedArguments);
    spreader = MethodHandles.insertArguments(spreader, 0, handle);
    spreader = MethodHandles.filterArguments(spreader, fixedArguments, filter);
    return spreader;
}
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)

Example 9 with MethodType

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

the class IndyArrayAccess method buildGetter.

private static MethodHandle buildGetter(Class arrayClass) {
    MethodHandle get = MethodHandles.arrayElementGetter(arrayClass);
    MethodHandle fallback = MethodHandles.explicitCastArguments(get, get.type().changeParameterType(0, Object.class));
    fallback = MethodHandles.dropArguments(fallback, 2, int.class);
    MethodType reorderType = fallback.type().insertParameterTypes(0, int.class).dropParameterTypes(2, 3);
    fallback = MethodHandles.permuteArguments(fallback, reorderType, 1, 0, 0);
    fallback = MethodHandles.foldArguments(fallback, normalizeIndex);
    fallback = MethodHandles.explicitCastArguments(fallback, get.type());
    MethodHandle guard = MethodHandles.dropArguments(notNegative, 0, arrayClass);
    MethodHandle handle = MethodHandles.guardWithTest(guard, get, fallback);
    return handle;
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 10 with MethodType

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

the class IndyArrayAccess method buildSetter.

private static MethodHandle buildSetter(Class arrayClass) {
    MethodHandle set = MethodHandles.arrayElementSetter(arrayClass);
    MethodHandle fallback = MethodHandles.explicitCastArguments(set, set.type().changeParameterType(0, Object.class));
    fallback = MethodHandles.dropArguments(fallback, 3, int.class);
    MethodType reorderType = fallback.type().insertParameterTypes(0, int.class).dropParameterTypes(4, 5);
    fallback = MethodHandles.permuteArguments(fallback, reorderType, 1, 0, 3, 0);
    fallback = MethodHandles.foldArguments(fallback, normalizeIndex);
    fallback = MethodHandles.explicitCastArguments(fallback, set.type());
    MethodHandle guard = MethodHandles.dropArguments(notNegative, 0, arrayClass);
    MethodHandle handle = MethodHandles.guardWithTest(guard, set, fallback);
    return handle;
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

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