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

The following examples show how to use java.lang.invoke.MethodType#insertParameterTypes() . 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: CallerSensitiveDynamicMethod.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static MethodType getMethodType(final AccessibleObject ao) {
    final boolean isMethod = ao instanceof Method;
    final Class<?> rtype = isMethod ? ((Method)ao).getReturnType() : ((Constructor<?>)ao).getDeclaringClass();
    final Class<?>[] ptypes = isMethod ? ((Method)ao).getParameterTypes() : ((Constructor<?>)ao).getParameterTypes();
    final MethodType type = MethodType.methodType(rtype, ptypes);
    final Member m = (Member)ao;
    return type.insertParameterTypes(0,
            isMethod ?
                    Modifier.isStatic(m.getModifiers()) ?
                            Object.class :
                            m.getDeclaringClass() :
                    StaticClass.class);
}
 
Example 2
Source File: CallerSensitiveDynamicMethod.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static MethodType getMethodType(final AccessibleObject ao) {
    final boolean isMethod = ao instanceof Method;
    final Class<?> rtype = isMethod ? ((Method)ao).getReturnType() : ((Constructor<?>)ao).getDeclaringClass();
    final Class<?>[] ptypes = isMethod ? ((Method)ao).getParameterTypes() : ((Constructor<?>)ao).getParameterTypes();
    final MethodType type = MethodType.methodType(rtype, ptypes);
    final Member m = (Member)ao;
    return type.insertParameterTypes(0,
            isMethod ?
                    Modifier.isStatic(m.getModifiers()) ?
                            Object.class :
                            m.getDeclaringClass() :
                    StaticClass.class);
}
 
Example 3
Source File: CallerSensitiveDynamicMethod.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static MethodType getMethodType(AccessibleObject ao) {
    final boolean isMethod = ao instanceof Method;
    final Class<?> rtype = isMethod ? ((Method)ao).getReturnType() : ((Constructor<?>)ao).getDeclaringClass();
    final Class<?>[] ptypes = isMethod ? ((Method)ao).getParameterTypes() : ((Constructor<?>)ao).getParameterTypes();
    final MethodType type = MethodType.methodType(rtype, ptypes);
    final Member m = (Member)ao;
    return type.insertParameterTypes(0,
            isMethod ?
                    Modifier.isStatic(m.getModifiers()) ?
                            Object.class :
                            m.getDeclaringClass() :
                    StaticClass.class);
}
 
Example 4
Source File: CallerSensitiveDynamicMethod.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static MethodType getMethodType(final AccessibleObject ao) {
    final boolean isMethod = ao instanceof Method;
    final Class<?> rtype = isMethod ? ((Method)ao).getReturnType() : ((Constructor<?>)ao).getDeclaringClass();
    final Class<?>[] ptypes = isMethod ? ((Method)ao).getParameterTypes() : ((Constructor<?>)ao).getParameterTypes();
    final MethodType type = MethodType.methodType(rtype, ptypes);
    final Member m = (Member)ao;
    return type.insertParameterTypes(0,
            isMethod ?
                    Modifier.isStatic(m.getModifiers()) ?
                            Object.class :
                            m.getDeclaringClass() :
                    StaticClass.class);
}
 
Example 5
Source File: CallerSensitiveDynamicMethod.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static MethodType getMethodType(final AccessibleObject ao) {
    final boolean isMethod = ao instanceof Method;
    final Class<?> rtype = isMethod ? ((Method)ao).getReturnType() : ((Constructor<?>)ao).getDeclaringClass();
    final Class<?>[] ptypes = isMethod ? ((Method)ao).getParameterTypes() : ((Constructor<?>)ao).getParameterTypes();
    final MethodType type = MethodType.methodType(rtype, ptypes);
    final Member m = (Member)ao;
    return type.insertParameterTypes(0,
            isMethod ?
                    Modifier.isStatic(m.getModifiers()) ?
                            Object.class :
                            m.getDeclaringClass() :
                    StaticClass.class);
}
 
Example 6
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "countedLoopBodyParameters")
public static void testCountedLoopBodyParameters(MethodType countType, MethodType initType, MethodType bodyType) throws Throwable {
    MethodHandle loop = MethodHandles.countedLoop(
            MethodHandles.empty(countType),
            initType == null ? null : MethodHandles.empty(initType),
            MethodHandles.empty(bodyType));
    // The rule:  If body takes the minimum number of parameters, then take what countType offers.
    // The initType has to just roll with whatever the other two agree on.
    int innerParams = (bodyType.returnType() == void.class ? 1 : 2);
    MethodType expectType = bodyType.dropParameterTypes(0, innerParams);
    if (expectType.parameterCount() == 0)
        expectType = expectType.insertParameterTypes(0, countType.parameterList());
    assertEquals(expectType, loop.type());
}
 
Example 7
Source File: CallerSensitiveDynamicMethod.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static MethodType getMethodType(AccessibleObject ao) {
    final boolean isMethod = ao instanceof Method;
    final Class<?> rtype = isMethod ? ((Method)ao).getReturnType() : ((Constructor<?>)ao).getDeclaringClass();
    final Class<?>[] ptypes = isMethod ? ((Method)ao).getParameterTypes() : ((Constructor<?>)ao).getParameterTypes();
    final MethodType type = MethodType.methodType(rtype, ptypes);
    final Member m = (Member)ao;
    return type.insertParameterTypes(0,
            isMethod ?
                    Modifier.isStatic(m.getModifiers()) ?
                            Object.class :
                            m.getDeclaringClass() :
                    StaticClass.class);
}
 
Example 8
Source File: CallerSensitiveDynamicMethod.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static MethodType getMethodType(final AccessibleObject ao) {
    final boolean isMethod = ao instanceof Method;
    final Class<?> rtype = isMethod ? ((Method)ao).getReturnType() : ((Constructor<?>)ao).getDeclaringClass();
    final Class<?>[] ptypes = isMethod ? ((Method)ao).getParameterTypes() : ((Constructor<?>)ao).getParameterTypes();
    final MethodType type = MethodType.methodType(rtype, ptypes);
    final Member m = (Member)ao;
    return type.insertParameterTypes(0,
            isMethod ?
                    Modifier.isStatic(m.getModifiers()) ?
                            Object.class :
                            m.getDeclaringClass() :
                    StaticClass.class);
}
 
Example 9
Source File: BeanLinker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
Binder(final LinkerServices linkerServices, final MethodType methodType, final Object fixedKey) {
    this.linkerServices = linkerServices;
    this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass());
    this.fixedKey = fixedKey;
}
 
Example 10
Source File: BeanLinker.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
Binder(final LinkerServices linkerServices, final MethodType methodType, final Object fixedKey) {
    this.linkerServices = linkerServices;
    this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass());
    this.fixedKey = fixedKey;
}
 
Example 11
Source File: BeanLinker.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
Binder(final LinkerServices linkerServices, final MethodType methodType, final Object fixedKey) {
    this.linkerServices = linkerServices;
    this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass());
    this.fixedKey = fixedKey;
}
 
Example 12
Source File: BoundCallableLinker.java    From openjdk-jdk9 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();
    final Operation operation = NamedOperation.getBaseOperation(descriptor.getOperation());
    // We need to distinguish NEW from CALL because CALL sites have parameter list of the form
    // "callee, this, args", while NEW sites have "callee, args" -- they lack the "this" parameter.
    final boolean isCall;
    if (operation == StandardOperation.NEW) {
        isCall = false;
    } else if (operation == StandardOperation.CALL) {
        isCall = true;
    } else {
        // Only CALL and 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 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 13
Source File: BeanLinker.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
Binder(LinkerServices linkerServices, MethodType methodType, Object fixedKey) {
    this.linkerServices = linkerServices;
    this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass());
    this.fixedKey = fixedKey;
}
 
Example 14
Source File: BoundCallableLinker.java    From openjdk-jdk8u-backup 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 15
Source File: BeanLinker.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
Binder(final LinkerServices linkerServices, final MethodType methodType, final Object fixedKey) {
    this.linkerServices = linkerServices;
    this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass());
    this.fixedKey = fixedKey;
}
 
Example 16
Source File: BeanLinker.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
Binder(LinkerServices linkerServices, MethodType methodType, Object fixedKey) {
    this.linkerServices = linkerServices;
    this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass());
    this.fixedKey = fixedKey;
}
 
Example 17
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 18
Source File: BeanLinker.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
Binder(LinkerServices linkerServices, MethodType methodType, Object fixedKey) {
    this.linkerServices = linkerServices;
    this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass());
    this.fixedKey = fixedKey;
}
 
Example 19
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 20
Source File: BeanLinker.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
Binder(final LinkerServices linkerServices, final MethodType methodType, final Object fixedKey) {
    this.linkerServices = linkerServices;
    this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass());
    this.fixedKey = fixedKey;
}