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


Java MethodHandles.exactInvoker方法代码示例

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


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

示例1: 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

示例2: getUnnamedPropertySetter

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
private GuardedInvocationComponent getUnnamedPropertySetter(final ComponentLinkRequest req) throws Exception {
    final CallSiteDescriptor callSiteDescriptor = req.getDescriptor();
    // 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);
    final LinkerServices linkerServices = req.linkerServices;

    // 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 = getNextComponent(req);

    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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:60,代码来源:AbstractJavaLinker.java


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