Java Code Examples for java.lang.invoke.MethodType#changeParameterType()

The following examples show how to use java.lang.invoke.MethodType#changeParameterType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParametricScalarImplementation.java    From presto with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: LambdaFactory.java    From lambda-factory with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: LambdaFactory.java    From AutoLoadCache with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: TypeHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: Selector.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 7
Source File: ScriptFunctionData.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: ScriptFunctionData.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: BoundCallableLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@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)));
}
 
Example 10
Source File: WithObject.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 11
Source File: WithObject.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 12
Source File: WithObject.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 13
Source File: BoundCallableLinker.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@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)));
}
 
Example 14
Source File: WithObject.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 15
Source File: WithObject.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 16
Source File: WithObject.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 17
Source File: BoundCallableLinker.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@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)));
}
 
Example 18
Source File: BoundCallableLinker.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@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)));
}
 
Example 19
Source File: CompiledFunctions.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: CompiledFunctions.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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);
}