当前位置: 首页>>代码示例>>Java>>正文


Java MethodHandles.dropArguments方法代码示例

本文整理汇总了Java中java.lang.invoke.MethodHandles.dropArguments方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandles.dropArguments方法的具体用法?Java MethodHandles.dropArguments怎么用?Java MethodHandles.dropArguments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.lang.invoke.MethodHandles的用法示例。


在下文中一共展示了MethodHandles.dropArguments方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testCountedLoopVoidInit

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testCountedLoopVoidInit() throws Throwable {
    MethodHandle fit5 = MethodHandles.constant(int.class, 5);
    for (int i = 0; i < 8; i++) {
        MethodHandle zero = MethodHandles.zero(void.class);
        MethodHandle init = fit5;
        MethodHandle body = Counted.MH_printHello;
        boolean useNull = (i & 1) != 0, addInitArg = (i & 2) != 0, addBodyArg = (i & 4) != 0;
        if (useNull)    zero = null;
        if (addInitArg) init = MethodHandles.dropArguments(init, 0, int.class);
        if (addBodyArg) body = MethodHandles.dropArguments(body, 1, int.class);
        System.out.println("testCountedLoopVoidInit i="+i+" : "+Arrays.asList(init, zero, body));
        MethodHandle loop = MethodHandles.countedLoop(init, zero, body);
        MethodType expectedType = Counted.MT_countedPrinting;
        if (addInitArg || addBodyArg)
            expectedType = expectedType.insertParameterTypes(0, int.class);
        assertEquals(expectedType, loop.type());
        if (addInitArg || addBodyArg)
            loop.invoke(99);
        else
            loop.invoke();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:LoopCombinatorTest.java

示例2: testDropArguments

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
void testDropArguments(int nargs, int pos, int drop) throws Throwable {
    countTest();
    MethodHandle target = varargsArray(nargs);
    Object[] args = randomArgs(target.type().parameterArray());
    MethodHandle target2 = MethodHandles.dropArguments(target, pos,
            Collections.nCopies(drop, Object.class).toArray(new Class<?>[0]));
    List<Object> resList = Arrays.asList(args);
    List<Object> argsToDrop = new ArrayList<>(resList);
    for (int i = drop; i > 0; i--) {
        argsToDrop.add(pos, "blort#"+i);
    }
    Object res2 = target2.invokeWithArguments(argsToDrop);
    Object res2List = Arrays.asList((Object[])res2);
    //if (!resList.equals(res2List))
    //    System.out.println("*** fail at n/p/d = "+nargs+"/"+pos+"/"+drop+": "+argsToDrop+" => "+res2List);
    assertEquals(resList, res2List);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:MethodHandlesTest.java

示例3: addTrailingArgs

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
public static MethodHandle addTrailingArgs(MethodHandle target, int nargs,
        List<Class<?>> classes) {
    int targetLen = target.type().parameterCount();
    int extra = (nargs - targetLen);
    if (extra <= 0) {
        return target;
    }
    List<Class<?>> fakeArgs = new ArrayList<>(extra);
    for (int i = 0; i < extra; ++i) {
        fakeArgs.add(classes.get(i % classes.size()));
    }
    return MethodHandles.dropArguments(target, targetLen, fakeArgs);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:Helper.java

示例4: getBoundBeanMethodGetter

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
private static MethodHandle getBoundBeanMethodGetter(final Object source, final MethodHandle methodGetter) {
    try {
        // NOTE: we're relying on the fact that "dyn:getMethod:..." return value is constant for any given method
        // name and object linked with BeansLinker. (Actually, an even stronger assumption is true: return value is
        // constant for any given method name and object's class.)
        return MethodHandles.dropArguments(MethodHandles.constant(Object.class,
                Bootstrap.bindCallable(methodGetter.invoke(source), source, null)), 0, Object.class);
    } catch(RuntimeException|Error e) {
        throw e;
    } catch(final Throwable t) {
        throw new RuntimeException(t);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:14,代码来源:NativeObject.java

示例5: getGuardedInvocation

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) {
    final Object receiver = linkRequest.getReceiver();
    if(!(receiver instanceof DynamicMethod)) {
        return null;
    }
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if(desc.getNameTokenCount() != 2 && desc.getNameToken(CallSiteDescriptor.SCHEME) != "dyn") {
        return null;
    }
    final String operator = desc.getNameToken(CallSiteDescriptor.OPERATOR);
    final DynamicMethod dynMethod = (DynamicMethod)receiver;
    final boolean constructor = dynMethod.isConstructor();
    final MethodHandle invocation;

    if (operator == "call" && !constructor) {
        invocation = dynMethod.getInvocation(
                CallSiteDescriptorFactory.dropParameterTypes(desc, 0, 1), linkerServices);
    } else if (operator == "new" && constructor) {
        final MethodHandle ctorInvocation = dynMethod.getInvocation(desc, linkerServices);
        if(ctorInvocation == null) {
            return null;
        }

        // Insert null for StaticClass parameter
        invocation = MethodHandles.insertArguments(ctorInvocation, 0, (Object)null);
    } else {
        return null;
    }

    if (invocation != null) {
        return new GuardedInvocation(MethodHandles.dropArguments(invocation, 0,
            desc.getMethodType().parameterType(0)), Guards.getIdentityGuard(receiver));
    }

    return null;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:38,代码来源:DynamicMethodLinker.java

示例6: makePruneAndInvokeMethod

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
/**
 * Creates a method that rebuilds our call chain, pruning it of any invalidated switchpoints, and then invokes that
 * chain.
 * @param relinkAndInvoke the ultimate fallback for the chain passed from the dynamic linker.
 * @return a method handle for prune-and-invoke
 */
private MethodHandle makePruneAndInvokeMethod(final MethodHandle relinkAndInvoke, final MethodHandle prune) {
    // Bind prune to (this, relink)
    final MethodHandle boundPrune = MethodHandles.insertArguments(prune, 0, this, relinkAndInvoke);
    // Make it ignore all incoming arguments
    final MethodHandle ignoreArgsPrune = MethodHandles.dropArguments(boundPrune, 0, type().parameterList());
    // Invoke prune, then invoke the call site target with original arguments
    return MethodHandles.foldArguments(MethodHandles.exactInvoker(type()), ignoreArgsPrune);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:ChainedCallSite.java

示例7: addTrailingArgs

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
static MethodHandle addTrailingArgs(MethodHandle target, int nargs, Class<?> argClass) {
    int targetLen = target.type().parameterCount();
    int extra = (nargs - targetLen);
    if (extra <= 0)  return target;
    List<Class<?>> fakeArgs = Collections.<Class<?>>nCopies(extra, argClass);
    return MethodHandles.dropArguments(target, targetLen, fakeArgs);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:MethodHandlesTest.java

示例8: omitTrailingArguments

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@DataProvider
static Object[][] omitTrailingArguments() {
    MethodHandle c = TryFinally.MH_voidCleanup;
    return new Object[][]{
            {c},
            {MethodHandles.dropArguments(c, 1, int.class)},
            {MethodHandles.dropArguments(c, 1, int.class, long.class)},
            {MethodHandles.dropArguments(c, 1, int.class, long.class, Object.class, int.class)},
            {MethodHandles.dropArguments(c, 1, int.class, long.class, Object.class, int.class, long.class)}
    };
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:TryFinallyTest.java

示例9: dropReceiver

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
private static MethodHandle dropReceiver(final MethodHandle mh, final Class<?> receiverClass) {
    MethodHandle newHandle = MethodHandles.dropArguments(mh, 0, receiverClass);
    // NOTE: this is a workaround for the fact that dropArguments doesn't preserve vararg collector state.
    if(mh.isVarargsCollector() && !newHandle.isVarargsCollector()) {
        final MethodType type = mh.type();
        newHandle = newHandle.asVarargsCollector(type.parameterType(type.parameterCount() - 1));
    }
    return newHandle;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:StaticClassIntrospector.java

示例10: testLoopWithVirtuals

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testLoopWithVirtuals() throws Throwable {
    // construct a loop (to calculate factorial) that uses a mix of static and virtual methods
    MethodHandle[] counterClause = new MethodHandle[]{null, LoopWithVirtuals.permute(LoopWithVirtuals.MH_inc)};
    MethodHandle[] accumulatorClause = new MethodHandle[]{
            // init function must indicate the loop arguments (there is no other means to determine them)
            MethodHandles.dropArguments(LoopWithVirtuals.MH_one, 0, LoopWithVirtuals.class),
            LoopWithVirtuals.permute(LoopWithVirtuals.MH_mult),
            LoopWithVirtuals.permute(LoopWithVirtuals.MH_pred),
            LoopWithVirtuals.permute(LoopWithVirtuals.MH_fin)
    };
    MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
    assertEquals(LoopWithVirtuals.MT_loop, loop.type());
    assertEquals(120, loop.invoke(new LoopWithVirtuals(), 5));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:LoopCombinatorTest.java

示例11: testCountedRangeLoop

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testCountedRangeLoop() throws Throwable {
    // String s = "Lambdaman!"; for (int i = -5; i < 8; ++i) { s = "na " + s; } return s; => a well known theme
    MethodHandle fitm5 = MethodHandles.dropArguments(Counted.MH_m5, 0, String.class);
    MethodHandle fit8 = MethodHandles.dropArguments(Counted.MH_8, 0, String.class);
    MethodHandle loop = MethodHandles.countedLoop(fitm5, fit8, Counted.MH_start, Counted.MH_step);
    assertEquals(Counted.MT_counted, loop.type());
    assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!"));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:LoopCombinatorTest.java

示例12: testAsSpreaderIllegalMethodType

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test(expectedExceptions = {WrongMethodTypeException.class})
public static void testAsSpreaderIllegalMethodType() {
    MethodHandle h = MethodHandles.dropArguments(MethodHandles.constant(String.class, ""), 0, int.class, int.class);
    MethodHandle s = h.asSpreader(String[].class, 1);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:6,代码来源:SpreadCollectTest.java

示例13: dropArguments

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Override
public MethodHandle dropArguments(final MethodHandle target, final int pos, final List<Class<?>> values) {
    final MethodHandle mh = MethodHandles.dropArguments(target, pos, values);
    return debug(mh, "dropArguments", target, pos, values);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:6,代码来源:MethodHandleFactory.java

示例14: getUnnamedPropertyGetter

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
private GuardedInvocationComponent getUnnamedPropertyGetter(final ComponentLinkRequest req) throws Exception {
    // Since we can't know what kind of a getter we'll get back on different invocations, we'll just
    // conservatively presume Object. Note we can't just coerce to a narrower call site type as the linking
    // runtime might not allow coercing at that call site.
    final CallSiteDescriptor callSiteDescriptor = req.getDescriptor();
    final MethodType type = callSiteDescriptor.getMethodType().changeReturnType(Object.class);
    // Must have exactly two arguments: receiver and name
    assertParameterCount(callSiteDescriptor, 2);

    // What's below is basically:
    //   foldArguments(guardWithTest(isNotNull, invoke(get_handle), null|nextComponent.invocation), get_getter_handle)
    // only with a bunch of method signature adjustments. Basically, retrieve method getter
    // AnnotatedDynamicMethod; if it is non-null, invoke its "handle" field, otherwise either return null,
    // or delegate to next component's invocation.

    final LinkerServices linkerServices = req.linkerServices;
    final MethodHandle typedGetter = linkerServices.asType(getPropertyGetterHandle, type.changeReturnType(
            AnnotatedDynamicMethod.class));
    final MethodHandle callSiteBoundMethodGetter = MethodHandles.insertArguments(
            GET_ANNOTATED_METHOD, 1, callSiteDescriptor, linkerServices);
    final MethodHandle callSiteBoundInvoker = MethodHandles.filterArguments(GETTER_INVOKER, 0,
            callSiteBoundMethodGetter);
    // Object(AnnotatedDynamicMethod, Object)->Object(AnnotatedDynamicMethod, T0)
    final MethodHandle invokeHandleTyped = linkerServices.asType(callSiteBoundInvoker,
            MethodType.methodType(type.returnType(), AnnotatedDynamicMethod.class, type.parameterType(0)));
    // Since it's in the target of a fold, drop the unnecessary second argument
    // Object(AnnotatedDynamicMethod, T0)->Object(AnnotatedDynamicMethod, T0, T1)
    final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandleTyped, 2,
            type.parameterType(1));
    final GuardedInvocationComponent nextComponent = getNextComponent(req);

    final MethodHandle fallbackFolded;
    if(nextComponent == null) {
        // Object(AnnotatedDynamicMethod)->Object(AnnotatedDynamicMethod, T0, T1); returns constant null
        fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_ANNOTATED_METHOD, 1,
                type.parameterList()).asType(type.insertParameterTypes(0, AnnotatedDynamicMethod.class));
    } else {
        // Object(T0, T1)->Object(AnnotatedDynamicMethod, T0, T1); adapts the next component's invocation to
        // drop the extra argument resulting from fold and to change its return type to Object.
        final MethodHandle nextInvocation = nextComponent.getGuardedInvocation().getInvocation();
        final MethodType nextType = nextInvocation.type();
        fallbackFolded = MethodHandles.dropArguments(nextInvocation.asType(
                nextType.changeReturnType(Object.class)), 0, AnnotatedDynamicMethod.class);
    }

    // fold(Object(AnnotatedDynamicMethod, T0, T1), AnnotatedDynamicMethod(T0, T1))
    final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
                IS_ANNOTATED_METHOD_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter);
    if(nextComponent == null) {
        return getClassGuardedInvocationComponent(compositeGetter, type);
    }
    return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:54,代码来源:AbstractJavaLinker.java

示例15: getPropertySetter

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
private GuardedInvocationComponent getPropertySetter(final CallSiteDescriptor callSiteDescriptor,
        final LinkerServices linkerServices, final List<String> operations) throws Exception {
    switch(callSiteDescriptor.getNameTokenCount()) {
        case 2: {
            // Must have three arguments: target object, property name, and property value.
            assertParameterCount(callSiteDescriptor, 3);

            // We want setters that conform to "Object(O, V)". Note, we aren't doing "R(O, V)" as it might not be
            // valid for us to convert return values proactively. Also, since we don't know what setters will be
            // invoked, we'll conservatively presume Object return type. The one exception is void return.
            final MethodType origType = callSiteDescriptor.getMethodType();
            final MethodType type = origType.returnType() == void.class ? origType : origType.changeReturnType(Object.class);

            // What's below is basically:
            //   foldArguments(guardWithTest(isNotNull, invoke, null|nextComponent.invocation),
            //     get_setter_handle(type, linkerServices))
            // only with a bunch of method signature adjustments. Basically, retrieve method setter
            // MethodHandle; if it is non-null, invoke it, otherwise either return null, or delegate to next
            // component's invocation.

            // Call site type is "ret_type(object_type,property_name_type,property_value_type)", which we'll
            // abbreviate to R(O, N, V) going forward, although we don't really use R here (see above about using
            // Object return type).
            final MethodType setterType = type.dropParameterTypes(1, 2);
            // Bind property setter handle to the expected setter type and linker services. Type is
            // MethodHandle(Object, String, Object)
            final MethodHandle boundGetter = MethodHandles.insertArguments(getPropertySetterHandle, 0,
                    callSiteDescriptor.changeMethodType(setterType), linkerServices);

            // Cast getter to MethodHandle(O, N, V)
            final MethodHandle typedGetter = linkerServices.asType(boundGetter, type.changeReturnType(
                    MethodHandle.class));

            // Handle to invoke the setter R(MethodHandle, O, V)
            final MethodHandle invokeHandle = MethodHandles.exactInvoker(setterType);
            // Handle to invoke the setter, dropping unnecessary fold arguments R(MethodHandle, O, N, V)
            final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandle, 2, type.parameterType(
                    1));
            final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
                    linkerServices, operations);

            final MethodHandle fallbackFolded;
            if(nextComponent == null) {
                // Object(MethodHandle)->Object(MethodHandle, O, N, V); returns constant null
                fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_METHOD_HANDLE, 1,
                        type.parameterList()).asType(type.insertParameterTypes(0, MethodHandle.class));
            } else {
                // Object(O, N, V)->Object(MethodHandle, O, N, V); adapts the next component's invocation to drop the
                // extra argument resulting from fold
                fallbackFolded = MethodHandles.dropArguments(nextComponent.getGuardedInvocation().getInvocation(),
                        0, MethodHandle.class);
            }

            // fold(R(MethodHandle, O, N, V), MethodHandle(O, N, V))
            final MethodHandle compositeSetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
                        IS_METHOD_HANDLE_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter);
            if(nextComponent == null) {
                return getClassGuardedInvocationComponent(compositeSetter, type);
            }
            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);
            }
            // If we don't have a property setter with this name, always fall back to the next operation in the
            // composite (if any)
            return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, operations);
        }
        default: {
            // More than two name components; don't know what to do with it.
            return null;
        }
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:81,代码来源:AbstractJavaLinker.java


注:本文中的java.lang.invoke.MethodHandles.dropArguments方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。