Search in sources :

Example 96 with MethodHandle

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

the class SuperCallSite method lookupMethod.

public MethodHandle lookupMethod(Class receiverClass, String selector) {
    Reference reference = Reference.factory.value_(lookupStart);
    AbstractMethodMapping mapping = ImageBootstrapper.systemMapping.superMethodMappingFor_methodName_(reference, selector);
    if (mapping == null) {
        if (selector.equals("doesNotUnderstand_"))
            throw new RuntimeException("Can't find DNU method");
        return wrapDNUHandle(lookupMethod(receiverClass, "doesNotUnderstand_"));
    }
    MethodHandle methodHandle;
    try {
        Method method = MethodTools.searchForMethod(mapping.definingClass(), selector, type.parameterArray(), true);
        if (method != null) {
            return mapping.methodHandle().asType(type);
        }
        methodHandle = lookup.findSpecial(mapping.definingClass(), selector, type.dropParameterTypes(0, 1), lookup.lookupClass());
    } catch (NoSuchMethodException | IllegalAccessException r) {
        try {
            methodHandle = lookup.findStatic(mapping.definingClass(), selector, type);
        } catch (NoSuchMethodException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    return methodHandle.asType(type);
}
Also used : AbstractMethodMapping(st.gravel.support.compiler.ast.AbstractMethodMapping) Reference(st.gravel.support.compiler.ast.Reference) Method(java.lang.reflect.Method) MethodHandle(java.lang.invoke.MethodHandle)

Example 97 with MethodHandle

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

the class SuperCallSite method addTargetToCache.

@Override
protected void addTargetToCache(Object receiver) {
    Class receiverClass = receiver.getClass();
    MethodHandle target = lookupMethod(receiverClass);
    setTarget(target);
}
Also used : MethodHandle(java.lang.invoke.MethodHandle)

Example 98 with MethodHandle

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

the class TryFinally method up.

public MethodHandle up(MethodHandle target) {
    MethodHandle exceptionHandler = Binder.from(target.type().insertParameterTypes(0, Throwable.class).changeReturnType(void.class)).drop(0).invoke(post);
    MethodHandle rethrow = Binder.from(target.type().insertParameterTypes(0, Throwable.class)).fold(exceptionHandler).drop(1, target.type().parameterCount()).throwException();
    target = MethodHandles.catchException(target, Throwable.class, rethrow);
    // if target returns a value, we must return it regardless of post
    MethodHandle realPost = post;
    if (target.type().returnType() != void.class) {
        // modify post to ignore return value
        MethodHandle newPost = Binder.from(target.type().insertParameterTypes(0, target.type().returnType()).changeReturnType(void.class)).drop(0).invoke(post);
        // fold post into an identity chain that only returns the value
        realPost = Binder.from(target.type().insertParameterTypes(0, target.type().returnType())).fold(newPost).drop(1, target.type().parameterCount()).identity();
    }
    return MethodHandles.foldArguments(realPost, target);
}
Also used : MethodHandle(java.lang.invoke.MethodHandle)

Example 99 with MethodHandle

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

the class BinderTest method testTryFinallyReturn3.

@Test
public void testTryFinallyReturn3() throws Throwable {
    MethodHandle post = Binder.from(void.class, String[].class).invokeStatic(LOOKUP, BinderTest.class, "finallyLogic");
    MethodHandle ignoreException = Binder.from(int.class, BlahException.class, String[].class).drop(0, 2).constant(1);
    MethodHandle handle = Binder.from(int.class, String[].class).tryFinally(post).catchException(BlahException.class, ignoreException).invokeStatic(LOOKUP, BinderTest.class, "setZeroToFooReturnIntAndRaise");
    assertEquals(MethodType.methodType(int.class, String[].class), handle.type());
    String[] stringAry = new String[1];
    try {
        assertEquals(1, (int) handle.invokeExact(stringAry));
    } catch (BlahException be) {
        assertTrue("should not have reached here", false);
    }
    assertEquals("foofinally", stringAry[0]);
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

Example 100 with MethodHandle

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

the class BinderTest method testConstant.

@Test
public void testConstant() throws Throwable {
    MethodHandle handle = Binder.from(String.class).constant("hello");
    assertEquals(MethodType.methodType(String.class), handle.type());
    assertEquals("hello", (String) handle.invokeExact());
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

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