Search in sources :

Example 66 with MethodType

use of java.lang.invoke.MethodType in project seph by seph-lang.

the class SephCallSite method setSpecialized.

public void setSpecialized(TypeProfile allProfiles) {
    MethodType type = this.type();
    MethodHandle elsePath = slowPath;
    elsePath = filterArguments(elsePath, 0, computeProfiler(true));
    TypeProfile[] cases = allProfiles.cases();
    int casesToCode = cases.length;
    for (int i = casesToCode - 1; i >= 0; i--) {
        MethodHandle[] guardAndFastPath = computeGuardAndFastPath(cases[i]);
        if (guardAndFastPath == null) {
            continue;
        }
        MethodHandle guard = guardAndFastPath[0];
        MethodHandle fastPath = guardAndFastPath[1];
        elsePath = guardWithTest(guard, fastPath, elsePath);
    }
    setTarget(elsePath);
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 67 with MethodType

use of java.lang.invoke.MethodType in project CorfuDB by CorfuDB.

the class ClientMsgHandler method generateHandlers.

/** Generate handlers for a particular client.
     *
     * @param caller    The context that is being used. Call MethodHandles.lookup() to obtain.
     * @param o         The object that implements the client.
     * @return
     */
public ClientMsgHandler generateHandlers(final MethodHandles.Lookup caller, final Object o) {
    Arrays.stream(o.getClass().getDeclaredMethods()).filter(x -> x.isAnnotationPresent(ClientHandler.class)).forEach(x -> {
        ClientHandler a = x.getAnnotation(ClientHandler.class);
        if (!x.getParameterTypes()[0].isAssignableFrom(a.type().messageType.getRawType())) {
            throw new RuntimeException("Incorrect message type, expected " + a.type().messageType.getRawType() + " but provided " + x.getParameterTypes()[0]);
        }
        if (handlerMap.containsKey(a.type())) {
            throw new RuntimeException("Handler for " + a.type() + " already registered!");
        }
        try {
            if (Modifier.isStatic(x.getModifiers())) {
                MethodHandle mh = caller.unreflect(x);
                MethodType mt = mh.type().changeParameterType(0, CorfuMsg.class);
                handlerMap.put(a.type(), (ClientMsgHandler.Handler) LambdaMetafactory.metafactory(caller, "handle", MethodType.methodType(ClientMsgHandler.Handler.class), mt, mh, mh.type()).getTarget().invokeExact());
            } else {
                MethodType mt = MethodType.methodType(x.getReturnType(), x.getParameterTypes());
                MethodHandle mh = caller.findVirtual(o.getClass(), x.getName(), mt);
                MethodType mtGeneric = mh.type().changeParameterType(1, CorfuMsg.class).changeReturnType(Object.class);
                handlerMap.put(a.type(), (ClientMsgHandler.Handler) LambdaMetafactory.metafactory(caller, "handle", MethodType.methodType(ClientMsgHandler.Handler.class, o.getClass()), mtGeneric.dropParameterTypes(0, 1), mh, mh.type().dropParameterTypes(0, 1)).getTarget().bindTo(o).invoke());
            }
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    });
    return this;
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LambdaMetafactory(java.lang.invoke.LambdaMetafactory) MethodHandle(java.lang.invoke.MethodHandle) Arrays(java.util.Arrays) MethodType(java.lang.invoke.MethodType) CorfuMsg(org.corfudb.protocols.wireprotocol.CorfuMsg) MethodHandles(java.lang.invoke.MethodHandles) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CorfuMsgType(org.corfudb.protocols.wireprotocol.CorfuMsgType) MethodType(java.lang.invoke.MethodType) CorfuMsg(org.corfudb.protocols.wireprotocol.CorfuMsg) MethodHandle(java.lang.invoke.MethodHandle)

Example 68 with MethodType

use of java.lang.invoke.MethodType in project robolectric by robolectric.

the class InvokeDynamicSupport method bindWithFallback.

private static MethodHandle bindWithFallback(MethodHandle mh, RoboCallSite site, MethodHandle fallback) {
    SwitchPoint switchPoint = getInvalidator(site.getCaller());
    MethodType type = site.type();
    MethodHandle boundFallback = foldArguments(exactInvoker(type), fallback.bindTo(site));
    mh = switchPoint.guardWithTest(mh.asType(type), boundFallback);
    site.setTarget(mh);
    return mh;
}
Also used : MethodType(java.lang.invoke.MethodType) SwitchPoint(java.lang.invoke.SwitchPoint) MethodHandle(java.lang.invoke.MethodHandle)

Example 69 with MethodType

use of java.lang.invoke.MethodType in project robolectric by robolectric.

the class ShadowWrangler method findShadowMethod.

@Override
public MethodHandle findShadowMethod(Class<?> caller, String name, MethodType type, boolean isStatic) throws IllegalAccessException {
    ShadowConfig shadowConfig = shadowConfigs.get(caller);
    if (shadowConfig == null)
        return CALL_REAL_CODE;
    ClassLoader classLoader = caller.getClassLoader();
    MethodType actualType = isStatic ? type : type.dropParameterTypes(0, 1);
    Method method = findShadowMethod(classLoader, shadowConfig, name, actualType.parameterArray());
    if (method == null) {
        return shadowConfig.callThroughByDefault ? CALL_REAL_CODE : DO_NOTHING;
    }
    Class<?> declaredShadowedClass = getShadowedClass(method);
    if (declaredShadowedClass.equals(Object.class)) {
        // e.g. for equals(), hashCode(), toString()
        return CALL_REAL_CODE;
    }
    boolean shadowClassMismatch = !declaredShadowedClass.equals(caller);
    if (shadowClassMismatch && !shadowConfig.inheritImplementationMethods) {
        return CALL_REAL_CODE;
    } else {
        MethodHandle mh = LOOKUP.unreflect(method);
        // in MessageQueue.nativeInit() which used to be void non-static in 4.2.
        if (!isStatic && Modifier.isStatic(method.getModifiers())) {
            return dropArguments(mh, 0, Object.class);
        } else {
            return mh;
        }
    }
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 70 with MethodType

use of java.lang.invoke.MethodType in project robolectric by robolectric.

the class ShadowWrangler method getShadowCreator.

@Override
public MethodHandle getShadowCreator(Class<?> caller) {
    String shadowClassName = getShadowClassNameInvoke(caller);
    if (shadowClassName == null)
        return dropArguments(NO_SHADOW_HANDLE, 0, caller);
    try {
        Class<?> shadowClass = Class.forName(shadowClassName, false, caller.getClassLoader());
        MethodHandle constructor = LOOKUP.findConstructor(shadowClass, methodType(void.class));
        MetaShadow metaShadow = getMetaShadow(shadowClass);
        // (instance)
        MethodHandle mh = identity(shadowClass);
        // (instance)
        mh = dropArguments(mh, 1, caller);
        for (Field field : metaShadow.realObjectFields) {
            MethodHandle setter = LOOKUP.unreflectSetter(field);
            MethodType setterType = mh.type().changeReturnType(void.class);
            mh = foldArguments(mh, setter.asType(setterType));
        }
        // (shadow, instance)
        mh = foldArguments(mh, constructor);
        // (instance)
        return mh;
    } catch (NoSuchMethodException | IllegalAccessException e) {
        throw new RuntimeException("Could not instantiate shadow, missing public empty constructor.", e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Could not instantiate shadow", e);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) 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