java.lang.invoke.MethodType#changeParameterType ( )源码实例Demo

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

源代码1 项目: presto   文件: ParametricScalarImplementation.java
private MethodHandle getMethodHandle(Method method)
{
    MethodHandle methodHandle = methodHandle(FUNCTION_IMPLEMENTATION_ERROR, method);
    if (!isStatic(method.getModifiers())) {
        // Change type of "this" argument to Object to make sure callers won't have classloader issues
        methodHandle = methodHandle.asType(methodHandle.type().changeParameterType(0, Object.class));
        // Re-arrange the parameters, so that the "this" parameter is after the meta parameters
        int[] permutedIndices = new int[methodHandle.type().parameterCount()];
        permutedIndices[0] = dependencies.size();
        MethodType newType = methodHandle.type().changeParameterType(dependencies.size(), methodHandle.type().parameterType(0));
        for (int i = 0; i < dependencies.size(); i++) {
            permutedIndices[i + 1] = i;
            newType = newType.changeParameterType(i, methodHandle.type().parameterType(i + 1));
        }
        for (int i = dependencies.size() + 1; i < permutedIndices.length; i++) {
            permutedIndices[i] = i;
        }
        methodHandle = permuteArguments(methodHandle, newType, permutedIndices);
    }
    return methodHandle;
}
 
源代码2 项目: lambda-factory   文件: LambdaFactory.java
private static MethodType createLambdaMethodType(Method method, MethodType instantiatedMethodType) {
	boolean isStatic = Modifier.isStatic(method.getModifiers());
	MethodType signature = isStatic ? instantiatedMethodType : instantiatedMethodType.changeParameterType(0, Object.class);

	Class<?>[] params = method.getParameterTypes();
	for (int i=0; i<params.length; i++){
		if (Object.class.isAssignableFrom(params[i])){
			signature = signature.changeParameterType(isStatic ? i : i+1, Object.class);
		}
	}
	if (Object.class.isAssignableFrom(signature.returnType())){
		signature = signature.changeReturnType(Object.class);
	}
	
	return signature;
}
 
源代码3 项目: AutoLoadCache   文件: LambdaFactory.java
private static MethodType createLambdaMethodType(Method method, MethodType instantiatedMethodType) {
	boolean isStatic = Modifier.isStatic(method.getModifiers());
	MethodType signature = isStatic ? instantiatedMethodType : instantiatedMethodType.changeParameterType(0, Object.class);

	Class<?>[] params = method.getParameterTypes();
	for (int i=0; i<params.length; i++){
		if (Object.class.isAssignableFrom(params[i])){
			signature = signature.changeParameterType(isStatic ? i : i+1, Object.class);
		}
	}
	if (Object.class.isAssignableFrom(signature.returnType())){
		signature = signature.changeReturnType(Object.class);
	}
	
	return signature;
}
 
源代码4 项目: TencentKona-8   文件: ScriptFunctionData.java
private static MethodType makeGenericType(final MethodType type) {
    MethodType newType = type.generic();
    if (isVarArg(type)) {
        newType = newType.changeParameterType(type.parameterCount() - 1, Object[].class);
    }
    if (needsCallee(type)) {
        newType = newType.changeParameterType(0, ScriptFunction.class);
    }
    return newType;
}
 
源代码5 项目: openjdk-jdk8u-backup   文件: ScriptFunctionData.java
private static MethodType makeGenericType(final MethodType type) {
    MethodType newType = type.generic();
    if (isVarArg(type)) {
        newType = newType.changeParameterType(type.parameterCount() - 1, Object[].class);
    }
    if (needsCallee(type)) {
        newType = newType.changeParameterType(0, ScriptFunction.class);
    }
    return newType;
}
 
源代码6 项目: openjdk-jdk9   文件: LoopCombinatorTest.java
static MethodHandle tweak(MethodHandle mh, int argPos, Class<?> type) {
    MethodType mt = mh.type();
    if (argPos == -1)
        mt = mt.changeReturnType(type);
    else
        mt = mt.changeParameterType(argPos, type);
    return MethodHandles.explicitCastArguments(mh, mt);
}
 
源代码7 项目: groovy   文件: Selector.java
/**
 * Helper method to manipulate the given type to replace Wrapper with Object.
 */
private MethodType removeWrapper(MethodType targetType) {
    Class<?>[] types = targetType.parameterArray();
    for (int i = 0; i < types.length; i++) {
        if (types[i] == Wrapper.class) {
            targetType = targetType.changeParameterType(i, Object.class);
        }
    }
    return targetType;
}
 
源代码8 项目: groovy   文件: TypeHelper.java
/**
 * Replaces the types in the callSiteType parameter if more specific types
 * given through the arguments. This is in general the case, unless
 * the argument is null.
 */
protected static MethodType replaceWithMoreSpecificType(Object[] args, MethodType callSiteType) {
    for (int i = 0; i < args.length; i++) {
        // if argument null, take the static type
        if (args[i] == null) continue;
        if (callSiteType.parameterType(i).isPrimitive()) continue;
        Class<?> argClass = args[i].getClass();
        callSiteType = callSiteType.changeParameterType(i, argClass);
    }
    return callSiteType;
}
 
源代码9 项目: TencentKona-8   文件: BoundCallableLinker.java
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final Object objBoundCallable = linkRequest.getReceiver();
    if(!(objBoundCallable instanceof BoundCallable)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    if (descriptor.getNameTokenCount() < 2 || !"dyn".equals(descriptor.getNameToken(CallSiteDescriptor.SCHEME))) {
        return null;
    }
    final String operation = descriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // We need to distinguish "dyn:new" from "dyn:call" because "dyn:call" sites have parameter list of the form
    // "callee, this, args", while "dyn:call" sites have "callee, args" -- they lack the "this" parameter.
    final boolean isCall;
    if ("new".equals(operation)) {
        isCall = false;
    } else if ("call".equals(operation)) {
        isCall = true;
    } else {
        // Only dyn:call and dyn:new are supported.
        return null;
    }
    final BoundCallable boundCallable = (BoundCallable)objBoundCallable;
    final Object callable = boundCallable.getCallable();
    final Object boundThis = boundCallable.getBoundThis();

    // We need to ask the linker services for a delegate invocation on the target callable.

    // Replace arguments (boundCallable[, this], args) => (callable[, boundThis], boundArgs, args) when delegating
    final Object[] args = linkRequest.getArguments();
    final Object[] boundArgs = boundCallable.getBoundArgs();
    final int argsLen = args.length;
    final int boundArgsLen = boundArgs.length;
    final Object[] newArgs = new Object[argsLen + boundArgsLen];
    newArgs[0] = callable;
    final int firstArgIndex;
    if (isCall) {
        newArgs[1] = boundThis;
        firstArgIndex = 2;
    } else {
        firstArgIndex = 1;
    }
    System.arraycopy(boundArgs, 0, newArgs, firstArgIndex, boundArgsLen);
    System.arraycopy(args, firstArgIndex, newArgs, firstArgIndex + boundArgsLen, argsLen - firstArgIndex);

    // Use R(T0, T1, T2, ...) => R(callable.class, boundThis.class, boundArg0.class, ..., boundArgn.class, T2, ...)
    // call site type when delegating to underlying linker (for dyn:new, there's no this).
    final MethodType type = descriptor.getMethodType();
    // Use R(T0, ...) => R(callable.class, ...)
    MethodType newMethodType = descriptor.getMethodType().changeParameterType(0, callable.getClass());
    if (isCall) {
        // R(callable.class, T1, ...) => R(callable.class, boundThis.class, ...)
        newMethodType = newMethodType.changeParameterType(1, boundThis == null? Object.class : boundThis.getClass());
    }
    // R(callable.class[, boundThis.class], T2, ...) => R(callable.class[, boundThis.class], boundArg0.class, ..., boundArgn.class, T2, ...)
    for(int i = boundArgs.length; i-- > 0;) {
        newMethodType = newMethodType.insertParameterTypes(firstArgIndex, boundArgs[i] == null ? Object.class : boundArgs[i].getClass());
    }
    final CallSiteDescriptor newDescriptor = descriptor.changeMethodType(newMethodType);

    // Delegate to target's linker
    final GuardedInvocation inv = linkerServices.getGuardedInvocation(linkRequest.replaceArguments(newDescriptor, newArgs));
    if(inv == null) {
        return null;
    }

    // Bind (callable[, boundThis], boundArgs) to the delegate handle
    final MethodHandle boundHandle = MethodHandles.insertArguments(inv.getInvocation(), 0,
            Arrays.copyOf(newArgs, firstArgIndex + boundArgs.length));
    final Class<?> p0Type = type.parameterType(0);
    final MethodHandle droppingHandle;
    if (isCall) {
        // Ignore incoming boundCallable and this
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type, type.parameterType(1));
    } else {
        // Ignore incoming boundCallable
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type);
    }
    // Identity guard on boundCallable object
    final MethodHandle newGuard = Guards.getIdentityGuard(boundCallable);
    return inv.replaceMethods(droppingHandle, newGuard.asType(newGuard.type().changeParameterType(0, p0Type)));
}
 
源代码10 项目: TencentKona-8   文件: WithObject.java
private static GuardedInvocation fixReceiverType(final GuardedInvocation link, final MethodHandle filter) {
    // The receiver may be an Object or a ScriptObject.
    final MethodType invType = link.getInvocation().type();
    final MethodType newInvType = invType.changeParameterType(0, filter.type().returnType());
    return link.asType(newInvType);
}
 
源代码11 项目: jdk8u60   文件: BoundCallableLinker.java
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final Object objBoundCallable = linkRequest.getReceiver();
    if(!(objBoundCallable instanceof BoundCallable)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    if (descriptor.getNameTokenCount() < 2 || !"dyn".equals(descriptor.getNameToken(CallSiteDescriptor.SCHEME))) {
        return null;
    }
    final String operation = descriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // We need to distinguish "dyn:new" from "dyn:call" because "dyn:call" sites have parameter list of the form
    // "callee, this, args", while "dyn:call" sites have "callee, args" -- they lack the "this" parameter.
    final boolean isCall;
    if ("new".equals(operation)) {
        isCall = false;
    } else if ("call".equals(operation)) {
        isCall = true;
    } else {
        // Only dyn:call and dyn:new are supported.
        return null;
    }
    final BoundCallable boundCallable = (BoundCallable)objBoundCallable;
    final Object callable = boundCallable.getCallable();
    final Object boundThis = boundCallable.getBoundThis();

    // We need to ask the linker services for a delegate invocation on the target callable.

    // Replace arguments (boundCallable[, this], args) => (callable[, boundThis], boundArgs, args) when delegating
    final Object[] args = linkRequest.getArguments();
    final Object[] boundArgs = boundCallable.getBoundArgs();
    final int argsLen = args.length;
    final int boundArgsLen = boundArgs.length;
    final Object[] newArgs = new Object[argsLen + boundArgsLen];
    newArgs[0] = callable;
    final int firstArgIndex;
    if (isCall) {
        newArgs[1] = boundThis;
        firstArgIndex = 2;
    } else {
        firstArgIndex = 1;
    }
    System.arraycopy(boundArgs, 0, newArgs, firstArgIndex, boundArgsLen);
    System.arraycopy(args, firstArgIndex, newArgs, firstArgIndex + boundArgsLen, argsLen - firstArgIndex);

    // Use R(T0, T1, T2, ...) => R(callable.class, boundThis.class, boundArg0.class, ..., boundArgn.class, T2, ...)
    // call site type when delegating to underlying linker (for dyn:new, there's no this).
    final MethodType type = descriptor.getMethodType();
    // Use R(T0, ...) => R(callable.class, ...)
    MethodType newMethodType = descriptor.getMethodType().changeParameterType(0, callable.getClass());
    if (isCall) {
        // R(callable.class, T1, ...) => R(callable.class, boundThis.class, ...)
        newMethodType = newMethodType.changeParameterType(1, boundThis == null? Object.class : boundThis.getClass());
    }
    // R(callable.class[, boundThis.class], T2, ...) => R(callable.class[, boundThis.class], boundArg0.class, ..., boundArgn.class, T2, ...)
    for(int i = boundArgs.length; i-- > 0;) {
        newMethodType = newMethodType.insertParameterTypes(firstArgIndex, boundArgs[i] == null ? Object.class : boundArgs[i].getClass());
    }
    final CallSiteDescriptor newDescriptor = descriptor.changeMethodType(newMethodType);

    // Delegate to target's linker
    final GuardedInvocation inv = linkerServices.getGuardedInvocation(linkRequest.replaceArguments(newDescriptor, newArgs));
    if(inv == null) {
        return null;
    }

    // Bind (callable[, boundThis], boundArgs) to the delegate handle
    final MethodHandle boundHandle = MethodHandles.insertArguments(inv.getInvocation(), 0,
            Arrays.copyOf(newArgs, firstArgIndex + boundArgs.length));
    final Class<?> p0Type = type.parameterType(0);
    final MethodHandle droppingHandle;
    if (isCall) {
        // Ignore incoming boundCallable and this
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type, type.parameterType(1));
    } else {
        // Ignore incoming boundCallable
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type);
    }
    // Identity guard on boundCallable object
    final MethodHandle newGuard = Guards.getIdentityGuard(boundCallable);
    return inv.replaceMethods(droppingHandle, newGuard.asType(newGuard.type().changeParameterType(0, p0Type)));
}
 
源代码12 项目: jdk8u60   文件: WithObject.java
private static GuardedInvocation fixReceiverType(final GuardedInvocation link, final MethodHandle filter) {
    // The receiver may be an Object or a ScriptObject.
    final MethodType invType = link.getInvocation().type();
    final MethodType newInvType = invType.changeParameterType(0, filter.type().returnType());
    return link.asType(newInvType);
}
 
源代码13 项目: jdk8u_nashorn   文件: BoundCallableLinker.java
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final Object objBoundCallable = linkRequest.getReceiver();
    if(!(objBoundCallable instanceof BoundCallable)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    if (descriptor.getNameTokenCount() < 2 || !"dyn".equals(descriptor.getNameToken(CallSiteDescriptor.SCHEME))) {
        return null;
    }
    final String operation = descriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // We need to distinguish "dyn:new" from "dyn:call" because "dyn:call" sites have parameter list of the form
    // "callee, this, args", while "dyn:call" sites have "callee, args" -- they lack the "this" parameter.
    final boolean isCall;
    if ("new".equals(operation)) {
        isCall = false;
    } else if ("call".equals(operation)) {
        isCall = true;
    } else {
        // Only dyn:call and dyn:new are supported.
        return null;
    }
    final BoundCallable boundCallable = (BoundCallable)objBoundCallable;
    final Object callable = boundCallable.getCallable();
    final Object boundThis = boundCallable.getBoundThis();

    // We need to ask the linker services for a delegate invocation on the target callable.

    // Replace arguments (boundCallable[, this], args) => (callable[, boundThis], boundArgs, args) when delegating
    final Object[] args = linkRequest.getArguments();
    final Object[] boundArgs = boundCallable.getBoundArgs();
    final int argsLen = args.length;
    final int boundArgsLen = boundArgs.length;
    final Object[] newArgs = new Object[argsLen + boundArgsLen];
    newArgs[0] = callable;
    final int firstArgIndex;
    if (isCall) {
        newArgs[1] = boundThis;
        firstArgIndex = 2;
    } else {
        firstArgIndex = 1;
    }
    System.arraycopy(boundArgs, 0, newArgs, firstArgIndex, boundArgsLen);
    System.arraycopy(args, firstArgIndex, newArgs, firstArgIndex + boundArgsLen, argsLen - firstArgIndex);

    // Use R(T0, T1, T2, ...) => R(callable.class, boundThis.class, boundArg0.class, ..., boundArgn.class, T2, ...)
    // call site type when delegating to underlying linker (for dyn:new, there's no this).
    final MethodType type = descriptor.getMethodType();
    // Use R(T0, ...) => R(callable.class, ...)
    MethodType newMethodType = descriptor.getMethodType().changeParameterType(0, callable.getClass());
    if (isCall) {
        // R(callable.class, T1, ...) => R(callable.class, boundThis.class, ...)
        newMethodType = newMethodType.changeParameterType(1, boundThis == null? Object.class : boundThis.getClass());
    }
    // R(callable.class[, boundThis.class], T2, ...) => R(callable.class[, boundThis.class], boundArg0.class, ..., boundArgn.class, T2, ...)
    for(int i = boundArgs.length; i-- > 0;) {
        newMethodType = newMethodType.insertParameterTypes(firstArgIndex, boundArgs[i] == null ? Object.class : boundArgs[i].getClass());
    }
    final CallSiteDescriptor newDescriptor = descriptor.changeMethodType(newMethodType);

    // Delegate to target's linker
    final GuardedInvocation inv = linkerServices.getGuardedInvocation(linkRequest.replaceArguments(newDescriptor, newArgs));
    if(inv == null) {
        return null;
    }

    // Bind (callable[, boundThis], boundArgs) to the delegate handle
    final MethodHandle boundHandle = MethodHandles.insertArguments(inv.getInvocation(), 0,
            Arrays.copyOf(newArgs, firstArgIndex + boundArgs.length));
    final Class<?> p0Type = type.parameterType(0);
    final MethodHandle droppingHandle;
    if (isCall) {
        // Ignore incoming boundCallable and this
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type, type.parameterType(1));
    } else {
        // Ignore incoming boundCallable
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type);
    }
    // Identity guard on boundCallable object
    final MethodHandle newGuard = Guards.getIdentityGuard(boundCallable);
    return inv.replaceMethods(droppingHandle, newGuard.asType(newGuard.type().changeParameterType(0, p0Type)));
}
 
源代码14 项目: openjdk-8   文件: WithObject.java
private static GuardedInvocation fixReceiverType(final GuardedInvocation link, final MethodHandle filter) {
    // The receiver may be an Object or a ScriptObject.
    final MethodType invType = link.getInvocation().type();
    final MethodType newInvType = invType.changeParameterType(0, filter.type().returnType());
    return link.asType(newInvType);
}
 
源代码15 项目: openjdk-jdk8u   文件: WithObject.java
private static GuardedInvocation fixReceiverType(final GuardedInvocation link, final MethodHandle filter) {
    // The receiver may be an Object or a ScriptObject.
    final MethodType invType = link.getInvocation().type();
    final MethodType newInvType = invType.changeParameterType(0, filter.type().returnType());
    return link.asType(newInvType);
}
 
源代码16 项目: hottub   文件: WithObject.java
private static GuardedInvocation fixReceiverType(final GuardedInvocation link, final MethodHandle filter) {
    // The receiver may be an Object or a ScriptObject.
    final MethodType invType = link.getInvocation().type();
    final MethodType newInvType = invType.changeParameterType(0, filter.type().returnType());
    return link.asType(newInvType);
}
 
源代码17 项目: openjdk-jdk9   文件: WithObject.java
private static GuardedInvocation fixReceiverType(final GuardedInvocation link, final MethodHandle filter) {
    // The receiver may be an Object or a ScriptObject.
    final MethodType invType = link.getInvocation().type();
    final MethodType newInvType = invType.changeParameterType(0, filter.type().returnType());
    return link.asType(newInvType);
}
 
源代码18 项目: hottub   文件: BoundCallableLinker.java
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final Object objBoundCallable = linkRequest.getReceiver();
    if(!(objBoundCallable instanceof BoundCallable)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    if (descriptor.getNameTokenCount() < 2 || !"dyn".equals(descriptor.getNameToken(CallSiteDescriptor.SCHEME))) {
        return null;
    }
    final String operation = descriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // We need to distinguish "dyn:new" from "dyn:call" because "dyn:call" sites have parameter list of the form
    // "callee, this, args", while "dyn:call" sites have "callee, args" -- they lack the "this" parameter.
    final boolean isCall;
    if ("new".equals(operation)) {
        isCall = false;
    } else if ("call".equals(operation)) {
        isCall = true;
    } else {
        // Only dyn:call and dyn:new are supported.
        return null;
    }
    final BoundCallable boundCallable = (BoundCallable)objBoundCallable;
    final Object callable = boundCallable.getCallable();
    final Object boundThis = boundCallable.getBoundThis();

    // We need to ask the linker services for a delegate invocation on the target callable.

    // Replace arguments (boundCallable[, this], args) => (callable[, boundThis], boundArgs, args) when delegating
    final Object[] args = linkRequest.getArguments();
    final Object[] boundArgs = boundCallable.getBoundArgs();
    final int argsLen = args.length;
    final int boundArgsLen = boundArgs.length;
    final Object[] newArgs = new Object[argsLen + boundArgsLen];
    newArgs[0] = callable;
    final int firstArgIndex;
    if (isCall) {
        newArgs[1] = boundThis;
        firstArgIndex = 2;
    } else {
        firstArgIndex = 1;
    }
    System.arraycopy(boundArgs, 0, newArgs, firstArgIndex, boundArgsLen);
    System.arraycopy(args, firstArgIndex, newArgs, firstArgIndex + boundArgsLen, argsLen - firstArgIndex);

    // Use R(T0, T1, T2, ...) => R(callable.class, boundThis.class, boundArg0.class, ..., boundArgn.class, T2, ...)
    // call site type when delegating to underlying linker (for dyn:new, there's no this).
    final MethodType type = descriptor.getMethodType();
    // Use R(T0, ...) => R(callable.class, ...)
    MethodType newMethodType = descriptor.getMethodType().changeParameterType(0, callable.getClass());
    if (isCall) {
        // R(callable.class, T1, ...) => R(callable.class, boundThis.class, ...)
        newMethodType = newMethodType.changeParameterType(1, boundThis == null? Object.class : boundThis.getClass());
    }
    // R(callable.class[, boundThis.class], T2, ...) => R(callable.class[, boundThis.class], boundArg0.class, ..., boundArgn.class, T2, ...)
    for(int i = boundArgs.length; i-- > 0;) {
        newMethodType = newMethodType.insertParameterTypes(firstArgIndex, boundArgs[i] == null ? Object.class : boundArgs[i].getClass());
    }
    final CallSiteDescriptor newDescriptor = descriptor.changeMethodType(newMethodType);

    // Delegate to target's linker
    final GuardedInvocation inv = linkerServices.getGuardedInvocation(linkRequest.replaceArguments(newDescriptor, newArgs));
    if(inv == null) {
        return null;
    }

    // Bind (callable[, boundThis], boundArgs) to the delegate handle
    final MethodHandle boundHandle = MethodHandles.insertArguments(inv.getInvocation(), 0,
            Arrays.copyOf(newArgs, firstArgIndex + boundArgs.length));
    final Class<?> p0Type = type.parameterType(0);
    final MethodHandle droppingHandle;
    if (isCall) {
        // Ignore incoming boundCallable and this
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type, type.parameterType(1));
    } else {
        // Ignore incoming boundCallable
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type);
    }
    // Identity guard on boundCallable object
    final MethodHandle newGuard = Guards.getIdentityGuard(boundCallable);
    return inv.replaceMethods(droppingHandle, newGuard.asType(newGuard.type().changeParameterType(0, p0Type)));
}
 
源代码19 项目: openjdk-8   文件: CompiledFunctions.java
/**
 * Takes a method handle, and returns a potentially different method handle that can be used in
 * {@code ScriptFunction#invoke(Object, Object...)} or {code ScriptFunction#construct(Object, Object...)}.
 * The returned method handle will be sure to return {@code Object}, and will have all its parameters turned into
 * {@code Object} as well, except for the following ones:
 * <ul>
 *   <li>a last parameter of type {@code Object[]} which is used for vararg functions,</li>
 *   <li>the first argument, which is forced to be {@link ScriptFunction}, in case the function receives itself
 *   (callee) as an argument.</li>
 * </ul>
 *
 * @param mh the original method handle
 *
 * @return the new handle, conforming to the rules above.
 */
private static MethodHandle composeGenericMethod(final MethodHandle mh) {
    final MethodType type = mh.type();
    final boolean isVarArg = ScriptFunctionData.isVarArg(mh);
    final int paramCount = isVarArg ? type.parameterCount() - 1 : type.parameterCount();

    MethodType newType = MethodType.genericMethodType(paramCount, isVarArg);

    if (ScriptFunctionData.needsCallee(mh)) {
        newType = newType.changeParameterType(0, ScriptFunction.class);
    }
    return type.equals(newType) ? mh : mh.asType(newType);
}
 
源代码20 项目: openjdk-8-source   文件: CompiledFunctions.java
/**
 * Takes a method handle, and returns a potentially different method handle that can be used in
 * {@code ScriptFunction#invoke(Object, Object...)} or {code ScriptFunction#construct(Object, Object...)}.
 * The returned method handle will be sure to return {@code Object}, and will have all its parameters turned into
 * {@code Object} as well, except for the following ones:
 * <ul>
 *   <li>a last parameter of type {@code Object[]} which is used for vararg functions,</li>
 *   <li>the first argument, which is forced to be {@link ScriptFunction}, in case the function receives itself
 *   (callee) as an argument.</li>
 * </ul>
 *
 * @param mh the original method handle
 *
 * @return the new handle, conforming to the rules above.
 */
private static MethodHandle composeGenericMethod(final MethodHandle mh) {
    final MethodType type = mh.type();
    final boolean isVarArg = ScriptFunctionData.isVarArg(mh);
    final int paramCount = isVarArg ? type.parameterCount() - 1 : type.parameterCount();

    MethodType newType = MethodType.genericMethodType(paramCount, isVarArg);

    if (ScriptFunctionData.needsCallee(mh)) {
        newType = newType.changeParameterType(0, ScriptFunction.class);
    }
    return type.equals(newType) ? mh : mh.asType(newType);
}