Package org.dynalang.dynalink.linker

Examples of org.dynalang.dynalink.linker.GuardedInvocation


                runtimeContextArgCount == 0 ? new LinkRequestImpl(callSiteDescriptor, callSiteUnstable, arguments)
                        : new RuntimeContextLinkRequestImpl(callSiteDescriptor, callSiteUnstable, arguments,
                                runtimeContextArgCount);

        // Find a suitable method handle with a guard
        GuardedInvocation guardedInvocation = linkerServices.getGuardedInvocation(linkRequest);

        // None found - throw an exception
        if(guardedInvocation == null) {
            throw new NoSuchDynamicMethodException(callSiteDescriptor.toString());
        }

        // If our call sites have a runtime context, and the linker produced a context-stripped invocation, adapt the
        // produced invocation into contextual invocation (by dropping the context...)
        if(runtimeContextArgCount > 0) {
            final MethodType origType = callSiteDescriptor.getMethodType();
            final MethodHandle invocation = guardedInvocation.getInvocation();
            if(invocation.type().parameterCount() == origType.parameterCount() - runtimeContextArgCount) {
                final List<Class<?>> prefix = origType.parameterList().subList(1, runtimeContextArgCount + 1);
                final MethodHandle guard = guardedInvocation.getGuard();
                guardedInvocation = guardedInvocation.dropArguments(1, prefix);
            }
        }

        int newRelinkCount = relinkCount;
        // Note that the short-circuited "&&" evaluation below ensures we'll increment the relinkCount until
        // threshold + 1 but not beyond that. Threshold + 1 is treated as a special value to signal that resetAndRelink
        // has already executed once for the unstable call site; we only want the call site to throw away its current
        // linkage once, when it transitions to unstable.
        if(unstableDetectionEnabled && newRelinkCount <= unstableRelinkThreshold && newRelinkCount++ == unstableRelinkThreshold) {
            callSite.resetAndRelink(guardedInvocation, createRelinkAndInvokeMethod(callSite, newRelinkCount));
        } else {
            callSite.relink(guardedInvocation, createRelinkAndInvokeMethod(callSite, newRelinkCount));
        }
        if(syncOnRelink) {
            MutableCallSite.syncAll(new MutableCallSite[] { (MutableCallSite)callSite });
        }
        return guardedInvocation.getInvocation();
    }
View Full Code Here


     * Tests setting a guardless target (it has to be linked directly).
     */
    public static void testSetGuardless() {
        final MethodHandle mh = MethodHandles.identity(Object.class);
        final MonomorphicCallSite mcs = createCallSite(mh.type());
        mcs.relink(new GuardedInvocation(mh, null), null);
        assertSame(mh, mcs.getTarget());
    }
View Full Code Here

            }
        } else {
            typedFixedKey = fixedKey;
        }

        final GuardedInvocation gi = gic.getGuardedInvocation();
        final Binder binder = new Binder(linkerServices, callSiteType, typedFixedKey);
        final MethodHandle invocation = gi.getInvocation();

        if(nextComponent == null) {
            return gic.replaceInvocation(binder.bind(invocation));
        }

        final MethodHandle checkGuard;
        if(invocation == GET_LIST_ELEMENT) {
            checkGuard = convertArgToInt(RANGE_CHECK_LIST, linkerServices, callSiteDescriptor);
        } else if(invocation == GET_MAP_ELEMENT) {
            // TODO: A more complex solution could be devised for maps, one where we do a get() first, and fold it
            // into a GWT that tests if it returned null, and if it did, do another GWT with containsKey()
            // that returns constant null (on true), or falls back to next component (on false)
            checkGuard = CONTAINS_MAP;
        } else {
            checkGuard = convertArgToInt(RANGE_CHECK_ARRAY, linkerServices, callSiteDescriptor);
        }
        return nextComponent.compose(MethodHandles.guardWithTest(binder.bindTest(checkGuard),
                binder.bind(invocation), nextComponent.getGuardedInvocation().getInvocation()), gi.getGuard(),
                gic.getValidatorClass(), gic.getValidationType());
    }
View Full Code Here

    /*private*/ MethodHandle createConverter(Class<?> sourceType, Class<?> targetType) throws Exception {
        final MethodType type = MethodType.methodType(targetType, sourceType);
        final MethodHandle identity = IDENTITY_CONVERSION.asType(type);
        MethodHandle last = identity;
        for(int i = factories.length; i-- > 0;) {
            final GuardedInvocation next = factories[i].convertToType(sourceType, targetType);
            if(next != null) {
                next.assertType(type);
                last = next.compose(last);
            }
        }
        return last == identity ? IDENTITY_CONVERSION : last;
    }
View Full Code Here

        final Object obj = linkRequest.getReceiver();
        if(obj == null) {
            return null;
        }
        for(TypeBasedGuardingDynamicLinker linker: classToLinker.get(obj.getClass())) {
            final GuardedInvocation invocation = linker.getGuardedInvocation(linkRequest, linkerServices);
            if(invocation != null) {
                return invocation;
            }
        }
        return null;
View Full Code Here

    }

    private GuardedInvocation createGuardedDynamicMethodInvocation(CallSiteDescriptor callSiteDescriptor,
            LinkerServices linkerServices, String methodName, Map<String, DynamicMethod> methodMap){
        final MethodHandle inv = getDynamicMethodInvocation(callSiteDescriptor, linkerServices, methodName, methodMap);
        return inv == null ? null : new GuardedInvocation(inv, getClassGuard(callSiteDescriptor.getMethodType()));
    }
View Full Code Here

                return nextComponent.compose(compositeSetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
            }
            case 3: {
                // Must have two arguments: target object and property value
                assertParameterCount(callSiteDescriptor, 2);
                final GuardedInvocation gi = createGuardedDynamicMethodInvocation(callSiteDescriptor, linkerServices,
                        callSiteDescriptor.getNameToken(CallSiteDescriptor.NAME_OPERAND), propertySetters);
                // If we have a property setter with this name, this composite operation will always stop here
                if(gi != null) {
                    return new GuardedInvocationComponent(gi, clazz, ValidationType.EXACT_CLASS);
                }
View Full Code Here

    private static void testEarlyBoundArrayLengthGetter(Class<?> arrayClass, String op, boolean early) throws Throwable {
        final BeansLinker bl = new BeansLinker();
        final CallSiteDescriptor csd =
                createCallSiteDescriptor("dyn:" + op, MethodType.methodType(int.class, arrayClass));
        final Object array = Array.newInstance(arrayClass.getComponentType(), 2);
        final GuardedInvocation inv = getGuardedInvocation(bl, csd, array);
        if(early) {
            // early bound, as call site guarantees we'll pass an array
            assertNull(inv.getGuard());
        }
        final MethodHandle mh = inv.getInvocation();
        assertNotNull(mh);
        assertEquals(csd.getMethodType(), mh.type());
        assertEquals(2, mh.invokeWithArguments(array));
    }
View Full Code Here

    public void testEarlyBoundCollectionLengthGetter() throws Throwable {
        final BeansLinker bl = new BeansLinker();
        final CallSiteDescriptor csd =
                createCallSiteDescriptor("dyn:getLength", MethodType.methodType(int.class, List.class));
        final GuardedInvocation inv = getGuardedInvocation(bl, csd, Collections.EMPTY_LIST);
        assertNull(inv.getGuard());
        final MethodHandle mh = inv.getInvocation();
        assertNotNull(mh);
        assertEquals(csd.getMethodType(), mh.type());
        assertEquals(0, mh.invokeWithArguments(new Object[] { Collections.EMPTY_LIST }));
        assertEquals(2, mh.invokeWithArguments(new Object[] { Arrays.asList(new Object[] { "a", "b" }) }));
    }
View Full Code Here

    public void testEarlyBoundMapLengthGetter() throws Throwable {
        final BeansLinker bl = new BeansLinker();
        final CallSiteDescriptor csd =
                createCallSiteDescriptor("dyn:getLength", MethodType.methodType(int.class, Map.class));
        final GuardedInvocation inv = getGuardedInvocation(bl, csd, Collections.EMPTY_MAP);
        assertNull(inv.getGuard());
        final MethodHandle mh = inv.getInvocation();
        assertNotNull(mh);
        assertEquals(csd.getMethodType(), mh.type());
        assertEquals(0, mh.invokeWithArguments(Collections.EMPTY_MAP));
        assertEquals(1, mh.invokeWithArguments(Collections.singletonMap("a", "b")));
    }
View Full Code Here

TOP

Related Classes of org.dynalang.dynalink.linker.GuardedInvocation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.