Search in sources :

Example 1 with MethodHandle

use of java.lang.invoke.MethodHandle in project groovy by apache.

the class TypeTransformers method createSAMTransform.

/**
     * creates a method handle able to transform the given Closure into a SAM type
     * if the given parameter is a SAM type 
     */
private static MethodHandle createSAMTransform(Object arg, Class parameter) {
    Method method = CachedSAMClass.getSAMMethod(parameter);
    if (method == null)
        return null;
    // TODO: have to think about how to optimize this!
    if (parameter.isInterface()) {
        if (Traits.isTrait(parameter)) {
            // the following code will basically do this:
            // Map<String,Closure> impl = Collections.singletonMap(method.getName(),arg);
            // return ProxyGenerator.INSTANCE.instantiateAggregate(impl,Collections.singletonList(clazz));
            // TO_SAMTRAIT_PROXY is a handle (Object,Object,ProxyGenerator,Class)GroovyObject
            // where the second object is the input closure, everything else
            // needs to be provide and is in remaining order: method name,
            // ProxyGenerator.INSTANCE and singletonList(parameter)
            MethodHandle ret = TO_SAMTRAIT_PROXY;
            ret = MethodHandles.insertArguments(ret, 2, ProxyGenerator.INSTANCE, Collections.singletonList(parameter));
            ret = MethodHandles.insertArguments(ret, 0, method.getName());
            return ret;
        }
        // the following code will basically do this:
        // return Proxy.newProxyInstance(
        //        arg.getClass().getClassLoader(),
        //        new Class[]{parameter},
        //        new ConvertedClosure((Closure) arg));
        // TO_REFLECTIVE_PROXY will do that for us, though
        // input is the closure, the method name, the class loader and the 
        // class[]. All of that but the closure must be provided here  
        MethodHandle ret = TO_REFLECTIVE_PROXY;
        ret = MethodHandles.insertArguments(ret, 1, method.getName(), arg.getClass().getClassLoader(), new Class[] { parameter });
        return ret;
    } else {
        // the following code will basically do this:
        //Map<String, Object> m = Collections.singletonMap(method.getName(), arg);
        //return ProxyGenerator.INSTANCE.
        //            instantiateAggregateFromBaseClass(m, parameter);
        // TO_GENERATED_PROXY is a handle (Object,Object,ProxyGenerator,Class)GroovyObject
        // where the second object is the input closure, everything else
        // needs to be provide and is in remaining order: method name, 
        // ProxyGenerator.INSTANCE and parameter
        MethodHandle ret = TO_GENERATED_PROXY;
        ret = MethodHandles.insertArguments(ret, 2, ProxyGenerator.INSTANCE, parameter);
        ret = MethodHandles.insertArguments(ret, 0, method.getName());
        return ret;
    }
}
Also used : CachedSAMClass(org.codehaus.groovy.reflection.stdclasses.CachedSAMClass) Method(java.lang.reflect.Method) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with MethodHandle

use of java.lang.invoke.MethodHandle in project groovy by apache.

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 3 with MethodHandle

use of java.lang.invoke.MethodHandle in project groovy by apache.

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)

Example 4 with MethodHandle

use of java.lang.invoke.MethodHandle in project groovy by apache.

the class IndyArrayAccess method arraySet.

public static MethodHandle arraySet(MethodType type) {
    Class key = type.parameterType(0);
    MethodHandle res = setterMap.get(key);
    if (res != null)
        return res;
    res = buildSetter(key);
    res = MethodHandles.explicitCastArguments(res, type);
    return res;
}
Also used : MethodHandle(java.lang.invoke.MethodHandle)

Example 5 with MethodHandle

use of java.lang.invoke.MethodHandle in project groovy by apache.

the class ReevaluatingReference method replacePayLoad.

private T replacePayLoad() {
    T payload = valueSupplier.get();
    MethodHandle ref = MethodHandles.constant(clazzRef.get(), payload);
    SwitchPoint sp = validationSupplier.apply(payload);
    returnRef = sp.guardWithTest(ref, FALLBACK_HANDLE);
    return payload;
}
Also used : SwitchPoint(java.lang.invoke.SwitchPoint) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)300 Test (org.junit.Test)101 MethodType (java.lang.invoke.MethodType)43 Type (com.facebook.presto.spi.type.Type)37 Method (java.lang.reflect.Method)18 OperatorType (com.facebook.presto.spi.function.OperatorType)14 MethodHandles (java.lang.invoke.MethodHandles)12 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)11 Signature (com.facebook.presto.metadata.Signature)10 CallSite (java.lang.invoke.CallSite)10 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)9 ImmutableList (com.google.common.collect.ImmutableList)9 List (java.util.List)8 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)7 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)7 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)6 Parameter (com.facebook.presto.bytecode.Parameter)6 PrestoException (com.facebook.presto.spi.PrestoException)6 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)6 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)6