java.lang.invoke.MethodHandles#guardWithTest ( )源码实例Demo

下面列出了java.lang.invoke.MethodHandles#guardWithTest ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: groovy   文件: IndyArrayAccess.java
private static MethodHandle buildSetter(Class<?> arrayClass) {
    MethodHandle set = MethodHandles.arrayElementSetter(arrayClass);
    MethodHandle fallback = MethodHandles.explicitCastArguments(set, set.type().changeParameterType(0, Object.class));

    fallback = MethodHandles.dropArguments(fallback, 3, int.class);
    MethodType reorderType = fallback.type().
            insertParameterTypes(0, int.class).
            dropParameterTypes(4, 5);
    fallback = MethodHandles.permuteArguments(fallback, reorderType, 1, 0, 3, 0);

    fallback = MethodHandles.foldArguments(fallback, normalizeIndex);
    fallback = MethodHandles.explicitCastArguments(fallback, set.type());

    MethodHandle guard = MethodHandles.dropArguments(notNegative, 0, arrayClass);
    MethodHandle handle = MethodHandles.guardWithTest(guard, set, fallback);
    return handle;
}
 
源代码2 项目: es6draft   文件: Bootstrap.java
private static MethodHandle setCallSiteTarget(MutableCallSite callsite, MethodHandle target, MethodHandle test,
        MethodHandle generic) {
    MethodHandle callSiteTarget;
    if (target != null) {
        target = target.asType(callsite.type());
        if (test != null) {
            MethodHandle fallback = createFallback(callsite, generic);
            callSiteTarget = MethodHandles.guardWithTest(test, target, fallback);
        } else {
            callSiteTarget = target;
        }
    } else {
        callSiteTarget = target = generic;
    }
    callsite.setTarget(callSiteTarget);
    return target;
}
 
源代码3 项目: TencentKona-8   文件: GuardedInvocation.java
/**
 * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back.
 * @param switchpointFallback the fallback method handle in case switchpoint is invalidated.
 * @param guardFallback the fallback method handle in case guard returns false.
 * @param catchFallback the fallback method in case the exception handler triggers
 * @return a composite method handle.
 */
public MethodHandle compose(final MethodHandle guardFallback, final MethodHandle switchpointFallback, final MethodHandle catchFallback) {
    final MethodHandle guarded =
            guard == null ?
                    invocation :
                    MethodHandles.guardWithTest(
                            guard,
                            invocation,
                            guardFallback);

    final MethodHandle catchGuarded =
            exception == null ?
                    guarded :
                    MH.catchException(
                            guarded,
                            exception,
                            MethodHandles.dropArguments(
                                catchFallback,
                                0,
                                exception));

    if (switchPoints == null) {
        return catchGuarded;
    }

    MethodHandle spGuarded = catchGuarded;
    for (final SwitchPoint sp : switchPoints) {
        spGuarded = sp.guardWithTest(spGuarded, switchpointFallback);
    }

    return spGuarded;
}
 
源代码4 项目: jdk8u60   文件: GuardedInvocation.java
/**
 * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back.
 * @param switchpointFallback the fallback method handle in case switchpoint is invalidated.
 * @param guardFallback the fallback method handle in case guard returns false.
 * @param catchFallback the fallback method in case the exception handler triggers
 * @return a composite method handle.
 */
public MethodHandle compose(final MethodHandle guardFallback, final MethodHandle switchpointFallback, final MethodHandle catchFallback) {
    final MethodHandle guarded =
            guard == null ?
                    invocation :
                    MethodHandles.guardWithTest(
                            guard,
                            invocation,
                            guardFallback);

    final MethodHandle catchGuarded =
            exception == null ?
                    guarded :
                    MH.catchException(
                            guarded,
                            exception,
                            MethodHandles.dropArguments(
                                catchFallback,
                                0,
                                exception));

    if (switchPoints == null) {
        return catchGuarded;
    }

    MethodHandle spGuarded = catchGuarded;
    for (final SwitchPoint sp : switchPoints) {
        spGuarded = sp.guardWithTest(spGuarded, switchpointFallback);
    }

    return spGuarded;
}
 
源代码5 项目: gravel   文件: PolymorphicCallSite.java
private MethodHandle getGuardedMethod(CacheEntry entry,
		MethodHandle fallback) {
	MethodHandle test = entry.receiverClass == null ? NIL_TEST_METHOD_HANDLE
			 : MethodHandles.insertArguments(
			TYPE_TEST_METHOD_HANDLE, 1, entry.receiverClass);
	Class[] tail = ArrayExtensions.tail(type.parameterArray());
	test = MethodHandles.dropArguments(test, 1, tail);
	test = test.asType(MethodType.methodType(Boolean.TYPE,
			type.parameterArray()));
	MethodHandle guard1 = MethodHandles.guardWithTest(test,
			entry.methodHandle, fallback);
	return guard1;
}
 
源代码6 项目: openjdk-jdk9   文件: GuardedInvocation.java
/**
 * Composes the invocation, guard, switch points, and the exception into a
 * composite method handle that knows how to fall back when the guard fails
 * or the invocation is invalidated.
 * @param switchpointFallback the fallback method handle in case a switch
 * point is invalidated.
 * @param guardFallback the fallback method handle in case guard returns
 * false.
 * @param catchFallback the fallback method in case the exception handler
 * triggers.
 * @return a composite method handle.
 */
public MethodHandle compose(final MethodHandle guardFallback, final MethodHandle switchpointFallback, final MethodHandle catchFallback) {
    final MethodHandle guarded =
            guard == null ?
                    invocation :
                    MethodHandles.guardWithTest(
                            guard,
                            invocation,
                            guardFallback);

    final MethodHandle catchGuarded =
            exception == null ?
                    guarded :
                    MethodHandles.catchException(
                            guarded,
                            exception,
                            MethodHandles.dropArguments(
                                catchFallback,
                                0,
                                exception));

    if (switchPoints == null) {
        return catchGuarded;
    }

    MethodHandle spGuarded = catchGuarded;
    for (final SwitchPoint sp : switchPoints) {
        spGuarded = sp.guardWithTest(spGuarded, switchpointFallback);
    }

    return spGuarded;
}
 
源代码7 项目: TencentKona-8   文件: MethodHandleFactory.java
@Override
public MethodHandle guardWithTest(final MethodHandle test, final MethodHandle target, final MethodHandle fallback) {
    final MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback);
    return debug(mh, "guardWithTest", test, target, fallback);
}
 
源代码8 项目: TencentKona-8   文件: AbstractJavaLinker.java
MethodHandle guardWithTest(final MethodHandle test) {
    return MethodHandles.guardWithTest(test, method1, method2);
}
 
源代码9 项目: jdk8u60   文件: MethodHandleFactory.java
@Override
public MethodHandle guardWithTest(final MethodHandle test, final MethodHandle target, final MethodHandle fallback) {
    final MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback);
    return debug(mh, "guardWithTest", test, target, fallback);
}
 
源代码10 项目: jdk8u60   文件: AbstractJavaLinker.java
MethodHandle guardWithTest(final MethodHandle test) {
    return MethodHandles.guardWithTest(test, method1, method2);
}
 
源代码11 项目: openjdk-jdk8u   文件: MethodHandleFactory.java
@Override
public MethodHandle guardWithTest(final MethodHandle test, final MethodHandle target, final MethodHandle fallback) {
    final MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback);
    return debug(mh, "guardWithTest", test, target, fallback);
}
 
源代码12 项目: jdk8u_nashorn   文件: AbstractJavaLinker.java
MethodHandle guardWithTest(final MethodHandle test) {
    return MethodHandles.guardWithTest(test, method1, method2);
}
 
源代码13 项目: nashorn   文件: SingleDynamicMethod.java
/**
 * Given a method handle and a call site type, adapts the method handle to the call site type. Performs type
 * conversions as needed using the specified linker services, and in case that the method handle is a vararg
 * collector, matches it to the arity of the call site.
 * @param target the method handle to adapt
 * @param callSiteType the type of the call site
 * @param linkerServices the linker services used for type conversions
 * @return the adapted method handle.
 */
static MethodHandle getInvocation(MethodHandle target, MethodType callSiteType, LinkerServices linkerServices) {
    final MethodType methodType = target.type();
    final int paramsLen = methodType.parameterCount();
    final boolean varArgs = target.isVarargsCollector();
    final MethodHandle fixTarget = varArgs ? target.asFixedArity() : target;
    final int fixParamsLen = varArgs ? paramsLen - 1 : paramsLen;
    final int argsLen = callSiteType.parameterCount();
    if(argsLen < fixParamsLen) {
        // Less actual arguments than number of fixed declared arguments; can't invoke.
        return null;
    }
    // Method handle has the same number of fixed arguments as the call site type
    if(argsLen == fixParamsLen) {
        // Method handle that matches the number of actual arguments as the number of fixed arguments
        final MethodHandle matchedMethod;
        if(varArgs) {
            // If vararg, add a zero-length array of the expected type as the last argument to signify no variable
            // arguments.
            matchedMethod = MethodHandles.insertArguments(fixTarget, fixParamsLen, Array.newInstance(
                    methodType.parameterType(fixParamsLen).getComponentType(), 0));
        } else {
            // Otherwise, just use the method
            matchedMethod = fixTarget;
        }
        return createConvertingInvocation(matchedMethod, linkerServices, callSiteType);
    }

    // What's below only works for varargs
    if(!varArgs) {
        return null;
    }

    final Class<?> varArgType = methodType.parameterType(fixParamsLen);
    // Handle a somewhat sinister corner case: caller passes exactly one argument in the vararg position, and we
    // must handle both a prepacked vararg array as well as a genuine 1-long vararg sequence.
    if(argsLen == paramsLen) {
        final Class<?> callSiteLastArgType = callSiteType.parameterType(fixParamsLen);
        if(varArgType.isAssignableFrom(callSiteLastArgType)) {
            // Call site signature guarantees we'll always be passed a single compatible array; just link directly
            // to the method, introducing necessary conversions. Also, preserve it being a variable arity method.
            return createConvertingInvocation(target, linkerServices, callSiteType).asVarargsCollector(
                    callSiteLastArgType);
        }
        if(!linkerServices.canConvert(callSiteLastArgType, varArgType)) {
            // Call site signature guarantees the argument can definitely not be an array (i.e. it is primitive);
            // link immediately to a vararg-packing method handle.
            return createConvertingInvocation(collectArguments(fixTarget, argsLen), linkerServices, callSiteType);
        }
        // Call site signature makes no guarantees that the single argument in the vararg position will be
        // compatible across all invocations. Need to insert an appropriate guard and fall back to generic vararg
        // method when it is not.
        return MethodHandles.guardWithTest(Guards.isInstance(varArgType, fixParamsLen, callSiteType),
                createConvertingInvocation(fixTarget, linkerServices, callSiteType),
                createConvertingInvocation(collectArguments(fixTarget, argsLen), linkerServices, callSiteType));
    }

    // Remaining case: more than one vararg.
    return createConvertingInvocation(collectArguments(fixTarget, argsLen), linkerServices, callSiteType);
}
 
源代码14 项目: openjdk-8   文件: MethodHandleFactory.java
@Override
public MethodHandle guardWithTest(final MethodHandle test, final MethodHandle target, final MethodHandle fallback) {
    return MethodHandles.guardWithTest(test, target, fallback);
}
 
源代码15 项目: openjdk-jdk9   文件: MethodHandleFactory.java
@Override
public MethodHandle guardWithTest(final MethodHandle test, final MethodHandle target, final MethodHandle fallback) {
    final MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback);
    return debug(mh, "guardWithTest", test, target, fallback);
}
 
源代码16 项目: nashorn   文件: MethodHandleFactory.java
@Override
public MethodHandle guardWithTest(final MethodHandle test, final MethodHandle target, final MethodHandle fallback) {
    return MethodHandles.guardWithTest(test, target, fallback);
}
 
源代码17 项目: hottub   文件: AbstractJavaLinker.java
MethodHandle guardWithTest(final MethodHandle test) {
    return MethodHandles.guardWithTest(test, method1, method2);
}
 
源代码18 项目: openjdk-8-source   文件: MethodHandleFactory.java
@Override
public MethodHandle guardWithTest(final MethodHandle test, final MethodHandle target, final MethodHandle fallback) {
    return MethodHandles.guardWithTest(test, target, fallback);
}
 
源代码19 项目: openjdk-8   文件: GuardedInvocation.java
/**
 * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back.
 * @param switchpointFallback the fallback method handle in case switchpoint is invalidated.
 * @param guardFallback the fallback method handle in case guard returns false.
 * @return a composite method handle.
 */
public MethodHandle compose(MethodHandle switchpointFallback, MethodHandle guardFallback) {
    final MethodHandle guarded =
            guard == null ? invocation : MethodHandles.guardWithTest(guard, invocation, guardFallback);
    return switchPoint == null ? guarded : switchPoint.guardWithTest(guarded, switchpointFallback);
}
 
源代码20 项目: nashorn   文件: GuardedInvocation.java
/**
 * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back.
 * @param switchpointFallback the fallback method handle in case switchpoint is invalidated.
 * @param guardFallback the fallback method handle in case guard returns false.
 * @return a composite method handle.
 */
public MethodHandle compose(MethodHandle switchpointFallback, MethodHandle guardFallback) {
    final MethodHandle guarded =
            guard == null ? invocation : MethodHandles.guardWithTest(guard, invocation, guardFallback);
    return switchPoint == null ? guarded : switchPoint.guardWithTest(guarded, switchpointFallback);
}