java.lang.invoke.LambdaForm.BasicType Java Examples

The following examples show how to use java.lang.invoke.LambdaForm.BasicType. 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: MethodHandles.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
    filterArgumentChecks(target, pos, filter);
    MethodType targetType = target.type();
    MethodType filterType = filter.type();
    BoundMethodHandle result = target.rebind();
    Class<?> newParamType = filterType.parameterType(0);
    LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
    MethodType newType = targetType.changeParameterType(pos, newParamType);
    result = result.copyWithExtendL(newType, lform, filter);
    return result;
}
 
Example #2
Source File: MethodHandles.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
    filterArgumentChecks(target, pos, filter);
    MethodType targetType = target.type();
    MethodType filterType = filter.type();
    BoundMethodHandle result = target.rebind();
    Class<?> newParamType = filterType.parameterType(0);
    LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
    MethodType newType = targetType.changeParameterType(pos, newParamType);
    result = result.copyWithExtendL(newType, lform, filter);
    return result;
}
 
Example #3
Source File: MethodHandles.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
static
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
    filterArgumentChecks(target, pos, filter);
    MethodType targetType = target.type();
    MethodType filterType = filter.type();
    BoundMethodHandle result = target.rebind();
    Class<?> newParamType = filterType.parameterType(0);
    LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
    MethodType newType = targetType.changeParameterType(pos, newParamType);
    result = result.copyWithExtendL(newType, lform, filter);
    return result;
}
 
Example #4
Source File: MethodHandles.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
    filterArgumentChecks(target, pos, filter);
    MethodType targetType = target.type();
    MethodType filterType = filter.type();
    BoundMethodHandle result = target.rebind();
    Class<?> newParamType = filterType.parameterType(0);
    LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
    MethodType newType = targetType.changeParameterType(pos, newParamType);
    result = result.copyWithExtendL(newType, lform, filter);
    return result;
}
 
Example #5
Source File: MethodHandles.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
    filterArgumentChecks(target, pos, filter);
    MethodType targetType = target.type();
    MethodType filterType = filter.type();
    BoundMethodHandle result = target.rebind();
    Class<?> newParamType = filterType.parameterType(0);
    LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
    MethodType newType = targetType.changeParameterType(pos, newParamType);
    result = result.copyWithExtendL(newType, lform, filter);
    return result;
}
 
Example #6
Source File: MethodHandles.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
    filterArgumentChecks(target, pos, filter);
    MethodType targetType = target.type();
    MethodType filterType = filter.type();
    BoundMethodHandle result = target.rebind();
    Class<?> newParamType = filterType.parameterType(0);
    LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
    MethodType newType = targetType.changeParameterType(pos, newParamType);
    result = result.copyWithExtendL(newType, lform, filter);
    return result;
}
 
Example #7
Source File: MethodHandles.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
    filterArgumentChecks(target, pos, filter);
    MethodType targetType = target.type();
    MethodType filterType = filter.type();
    BoundMethodHandle result = target.rebind();
    Class<?> newParamType = filterType.parameterType(0);
    LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
    MethodType newType = targetType.changeParameterType(pos, newParamType);
    result = result.copyWithExtendL(newType, lform, filter);
    return result;
}
 
Example #8
Source File: MethodHandles.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
    filterArgumentChecks(target, pos, filter);
    MethodType targetType = target.type();
    MethodType filterType = filter.type();
    BoundMethodHandle result = target.rebind();
    Class<?> newParamType = filterType.parameterType(0);
    LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
    MethodType newType = targetType.changeParameterType(pos, newParamType);
    result = result.copyWithExtendL(newType, lform, filter);
    return result;
}
 
Example #9
Source File: MethodHandles.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
    filterArgumentChecks(target, pos, filter);
    MethodType targetType = target.type();
    MethodType filterType = filter.type();
    BoundMethodHandle result = target.rebind();
    Class<?> newParamType = filterType.parameterType(0);
    LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
    MethodType newType = targetType.changeParameterType(pos, newParamType);
    result = result.copyWithExtendL(newType, lform, filter);
    return result;
}
 
Example #10
Source File: MethodHandles.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
static
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
    filterArgumentChecks(target, pos, filter);
    MethodType targetType = target.type();
    MethodType filterType = filter.type();
    BoundMethodHandle result = target.rebind();
    Class<?> newParamType = filterType.parameterType(0);
    LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
    MethodType newType = targetType.changeParameterType(pos, newParamType);
    result = result.copyWithExtendL(newType, lform, filter);
    return result;
}
 
Example #11
Source File: MethodHandles.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
static
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
    filterArgumentChecks(target, pos, filter);
    MethodType targetType = target.type();
    MethodType filterType = filter.type();
    BoundMethodHandle result = target.rebind();
    Class<?> newParamType = filterType.parameterType(0);
    LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
    MethodType newType = targetType.changeParameterType(pos, newParamType);
    result = result.copyWithExtendL(newType, lform, filter);
    return result;
}
 
Example #12
Source File: MethodHandles.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
    filterArgumentChecks(target, pos, filter);
    MethodType targetType = target.type();
    MethodType filterType = filter.type();
    BoundMethodHandle result = target.rebind();
    Class<?> newParamType = filterType.parameterType(0);
    LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
    MethodType newType = targetType.changeParameterType(pos, newParamType);
    result = result.copyWithExtendL(newType, lform, filter);
    return result;
}
 
Example #13
Source File: MethodHandles.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
    filterArgumentChecks(target, pos, filter);
    MethodType targetType = target.type();
    MethodType filterType = filter.type();
    BoundMethodHandle result = target.rebind();
    Class<?> newParamType = filterType.parameterType(0);
    LambdaForm lform = result.editor().filterArgumentForm(1 + pos, BasicType.basicType(newParamType));
    MethodType newType = targetType.changeParameterType(pos, newParamType);
    result = result.copyWithExtendL(newType, lform, filter);
    return result;
}
 
Example #14
Source File: MethodHandles.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle makeZero(Class<?> rtype) {
    MethodType mtype = MethodType.methodType(rtype);
    LambdaForm lform = LambdaForm.zeroForm(BasicType.basicType(rtype));
    return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.ZERO);
}
 
Example #15
Source File: MethodHandles.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
     * Produces a method handle which will discard some dummy arguments
     * before calling some other specified <i>target</i> method handle.
     * The type of the new method handle will be the same as the target's type,
     * except it will also include the dummy argument types,
     * at some given position.
     * <p>
     * The {@code pos} argument may range between zero and <i>N</i>,
     * where <i>N</i> is the arity of the target.
     * If {@code pos} is zero, the dummy arguments will precede
     * the target's real arguments; if {@code pos} is <i>N</i>
     * they will come after.
     * <p>
     * <b>Example:</b>
     * <blockquote><pre>{@code
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
assertEquals("xy", (String) cat.invokeExact("x", "y"));
MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
assertEquals(bigType, d0.type());
assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
     * }</pre></blockquote>
     * <p>
     * This method is also equivalent to the following code:
     * <blockquote><pre>
     * {@link #dropArguments(MethodHandle,int,Class...) dropArguments}{@code (target, pos, valueTypes.toArray(new Class[0]))}
     * </pre></blockquote>
     * @param target the method handle to invoke after the arguments are dropped
     * @param valueTypes the type(s) of the argument(s) to drop
     * @param pos position of first argument to drop (zero for the leftmost)
     * @return a method handle which drops arguments of the given types,
     *         before calling the original method handle
     * @throws NullPointerException if the target is null,
     *                              or if the {@code valueTypes} list or any of its elements is null
     * @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class},
     *                  or if {@code pos} is negative or greater than the arity of the target,
     *                  or if the new method handle's type would have too many parameters
     */
    public static
    MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
        MethodType oldType = target.type();  // get NPE
        int dropped = dropArgumentChecks(oldType, pos, valueTypes);
        MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
        if (dropped == 0)  return target;
        BoundMethodHandle result = target.rebind();
        LambdaForm lform = result.form;
        int insertFormArg = 1 + pos;
        for (Class<?> ptype : valueTypes) {
            lform = lform.editor().addArgumentForm(insertFormArg++, BasicType.basicType(ptype));
        }
        result = result.copyWith(newType, lform);
        return result;
    }
 
Example #16
Source File: MethodHandles.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle makeZero(Class<?> rtype) {
    MethodType mtype = MethodType.methodType(rtype);
    LambdaForm lform = LambdaForm.zeroForm(BasicType.basicType(rtype));
    return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.ZERO);
}
 
Example #17
Source File: MethodHandles.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
     * Produces a method handle which will discard some dummy arguments
     * before calling some other specified <i>target</i> method handle.
     * The type of the new method handle will be the same as the target's type,
     * except it will also include the dummy argument types,
     * at some given position.
     * <p>
     * The {@code pos} argument may range between zero and <i>N</i>,
     * where <i>N</i> is the arity of the target.
     * If {@code pos} is zero, the dummy arguments will precede
     * the target's real arguments; if {@code pos} is <i>N</i>
     * they will come after.
     * <p>
     * <b>Example:</b>
     * <blockquote><pre>{@code
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
assertEquals("xy", (String) cat.invokeExact("x", "y"));
MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
assertEquals(bigType, d0.type());
assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
     * }</pre></blockquote>
     * <p>
     * This method is also equivalent to the following code:
     * <blockquote><pre>
     * {@link #dropArguments(MethodHandle,int,Class...) dropArguments}{@code (target, pos, valueTypes.toArray(new Class[0]))}
     * </pre></blockquote>
     * @param target the method handle to invoke after the arguments are dropped
     * @param valueTypes the type(s) of the argument(s) to drop
     * @param pos position of first argument to drop (zero for the leftmost)
     * @return a method handle which drops arguments of the given types,
     *         before calling the original method handle
     * @throws NullPointerException if the target is null,
     *                              or if the {@code valueTypes} list or any of its elements is null
     * @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class},
     *                  or if {@code pos} is negative or greater than the arity of the target,
     *                  or if the new method handle's type would have too many parameters
     */
    public static
    MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
        MethodType oldType = target.type();  // get NPE
        int dropped = dropArgumentChecks(oldType, pos, valueTypes);
        MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
        if (dropped == 0)  return target;
        BoundMethodHandle result = target.rebind();
        LambdaForm lform = result.form;
        int insertFormArg = 1 + pos;
        for (Class<?> ptype : valueTypes) {
            lform = lform.editor().addArgumentForm(insertFormArg++, BasicType.basicType(ptype));
        }
        result = result.copyWith(newType, lform);
        return result;
    }
 
Example #18
Source File: MethodHandles.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
     * Produces a method handle which will discard some dummy arguments
     * before calling some other specified <i>target</i> method handle.
     * The type of the new method handle will be the same as the target's type,
     * except it will also include the dummy argument types,
     * at some given position.
     * <p>
     * The {@code pos} argument may range between zero and <i>N</i>,
     * where <i>N</i> is the arity of the target.
     * If {@code pos} is zero, the dummy arguments will precede
     * the target's real arguments; if {@code pos} is <i>N</i>
     * they will come after.
     * <p>
     * <b>Example:</b>
     * <blockquote><pre>{@code
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
assertEquals("xy", (String) cat.invokeExact("x", "y"));
MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
assertEquals(bigType, d0.type());
assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
     * }</pre></blockquote>
     * <p>
     * This method is also equivalent to the following code:
     * <blockquote><pre>
     * {@link #dropArguments(MethodHandle,int,Class...) dropArguments}{@code (target, pos, valueTypes.toArray(new Class[0]))}
     * </pre></blockquote>
     * @param target the method handle to invoke after the arguments are dropped
     * @param valueTypes the type(s) of the argument(s) to drop
     * @param pos position of first argument to drop (zero for the leftmost)
     * @return a method handle which drops arguments of the given types,
     *         before calling the original method handle
     * @throws NullPointerException if the target is null,
     *                              or if the {@code valueTypes} list or any of its elements is null
     * @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class},
     *                  or if {@code pos} is negative or greater than the arity of the target,
     *                  or if the new method handle's type would have too many parameters
     */
    public static
    MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
        valueTypes = copyTypes(valueTypes);
        MethodType oldType = target.type();  // get NPE
        int dropped = dropArgumentChecks(oldType, pos, valueTypes);
        MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
        if (dropped == 0)  return target;
        BoundMethodHandle result = target.rebind();
        LambdaForm lform = result.form;
        int insertFormArg = 1 + pos;
        for (Class<?> ptype : valueTypes) {
            lform = lform.editor().addArgumentForm(insertFormArg++, BasicType.basicType(ptype));
        }
        result = result.copyWith(newType, lform);
        return result;
    }
 
Example #19
Source File: MethodHandles.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle makeZero(Class<?> rtype) {
    MethodType mtype = MethodType.methodType(rtype);
    LambdaForm lform = LambdaForm.zeroForm(BasicType.basicType(rtype));
    return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.ZERO);
}
 
Example #20
Source File: MethodHandles.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle makeIdentity(Class<?> ptype) {
    MethodType mtype = MethodType.methodType(ptype, ptype);
    LambdaForm lform = LambdaForm.identityForm(BasicType.basicType(ptype));
    return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.IDENTITY);
}
 
Example #21
Source File: MethodHandles.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle makeIdentity(Class<?> ptype) {
    MethodType mtype = MethodType.methodType(ptype, ptype);
    LambdaForm lform = LambdaForm.identityForm(BasicType.basicType(ptype));
    return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.IDENTITY);
}
 
Example #22
Source File: MethodHandles.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
     * Produces a method handle which will discard some dummy arguments
     * before calling some other specified <i>target</i> method handle.
     * The type of the new method handle will be the same as the target's type,
     * except it will also include the dummy argument types,
     * at some given position.
     * <p>
     * The {@code pos} argument may range between zero and <i>N</i>,
     * where <i>N</i> is the arity of the target.
     * If {@code pos} is zero, the dummy arguments will precede
     * the target's real arguments; if {@code pos} is <i>N</i>
     * they will come after.
     * <p>
     * <b>Example:</b>
     * <blockquote><pre>{@code
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
assertEquals("xy", (String) cat.invokeExact("x", "y"));
MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
assertEquals(bigType, d0.type());
assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
     * }</pre></blockquote>
     * <p>
     * This method is also equivalent to the following code:
     * <blockquote><pre>
     * {@link #dropArguments(MethodHandle,int,Class...) dropArguments}{@code (target, pos, valueTypes.toArray(new Class[0]))}
     * </pre></blockquote>
     * @param target the method handle to invoke after the arguments are dropped
     * @param valueTypes the type(s) of the argument(s) to drop
     * @param pos position of first argument to drop (zero for the leftmost)
     * @return a method handle which drops arguments of the given types,
     *         before calling the original method handle
     * @throws NullPointerException if the target is null,
     *                              or if the {@code valueTypes} list or any of its elements is null
     * @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class},
     *                  or if {@code pos} is negative or greater than the arity of the target,
     *                  or if the new method handle's type would have too many parameters
     */
    public static
    MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
        MethodType oldType = target.type();  // get NPE
        int dropped = dropArgumentChecks(oldType, pos, valueTypes);
        MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
        if (dropped == 0)  return target;
        BoundMethodHandle result = target.rebind();
        LambdaForm lform = result.form;
        int insertFormArg = 1 + pos;
        for (Class<?> ptype : valueTypes) {
            lform = lform.editor().addArgumentForm(insertFormArg++, BasicType.basicType(ptype));
        }
        result = result.copyWith(newType, lform);
        return result;
    }
 
Example #23
Source File: MethodHandles.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle makeZero(Class<?> rtype) {
    MethodType mtype = MethodType.methodType(rtype);
    LambdaForm lform = LambdaForm.zeroForm(BasicType.basicType(rtype));
    return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.ZERO);
}
 
Example #24
Source File: MethodHandles.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
private static MethodHandle makeZero(Class<?> rtype) {
    MethodType mtype = MethodType.methodType(rtype);
    LambdaForm lform = LambdaForm.zeroForm(BasicType.basicType(rtype));
    return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.ZERO);
}
 
Example #25
Source File: MethodHandles.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
private static MethodHandle makeIdentity(Class<?> ptype) {
    MethodType mtype = MethodType.methodType(ptype, ptype);
    LambdaForm lform = LambdaForm.identityForm(BasicType.basicType(ptype));
    return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.IDENTITY);
}
 
Example #26
Source File: MethodHandles.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
     * Produces a method handle which will discard some dummy arguments
     * before calling some other specified <i>target</i> method handle.
     * The type of the new method handle will be the same as the target's type,
     * except it will also include the dummy argument types,
     * at some given position.
     * <p>
     * The {@code pos} argument may range between zero and <i>N</i>,
     * where <i>N</i> is the arity of the target.
     * If {@code pos} is zero, the dummy arguments will precede
     * the target's real arguments; if {@code pos} is <i>N</i>
     * they will come after.
     * <p>
     * <b>Example:</b>
     * <blockquote><pre>{@code
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
assertEquals("xy", (String) cat.invokeExact("x", "y"));
MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
assertEquals(bigType, d0.type());
assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
     * }</pre></blockquote>
     * <p>
     * This method is also equivalent to the following code:
     * <blockquote><pre>
     * {@link #dropArguments(MethodHandle,int,Class...) dropArguments}{@code (target, pos, valueTypes.toArray(new Class[0]))}
     * </pre></blockquote>
     * @param target the method handle to invoke after the arguments are dropped
     * @param valueTypes the type(s) of the argument(s) to drop
     * @param pos position of first argument to drop (zero for the leftmost)
     * @return a method handle which drops arguments of the given types,
     *         before calling the original method handle
     * @throws NullPointerException if the target is null,
     *                              or if the {@code valueTypes} list or any of its elements is null
     * @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class},
     *                  or if {@code pos} is negative or greater than the arity of the target,
     *                  or if the new method handle's type would have too many parameters
     */
    public static
    MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
        valueTypes = copyTypes(valueTypes);
        MethodType oldType = target.type();  // get NPE
        int dropped = dropArgumentChecks(oldType, pos, valueTypes);
        MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
        if (dropped == 0)  return target;
        BoundMethodHandle result = target.rebind();
        LambdaForm lform = result.form;
        int insertFormArg = 1 + pos;
        for (Class<?> ptype : valueTypes) {
            lform = lform.editor().addArgumentForm(insertFormArg++, BasicType.basicType(ptype));
        }
        result = result.copyWith(newType, lform);
        return result;
    }
 
Example #27
Source File: MethodHandles.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
     * Produces a method handle which will discard some dummy arguments
     * before calling some other specified <i>target</i> method handle.
     * The type of the new method handle will be the same as the target's type,
     * except it will also include the dummy argument types,
     * at some given position.
     * <p>
     * The {@code pos} argument may range between zero and <i>N</i>,
     * where <i>N</i> is the arity of the target.
     * If {@code pos} is zero, the dummy arguments will precede
     * the target's real arguments; if {@code pos} is <i>N</i>
     * they will come after.
     * <p>
     * <b>Example:</b>
     * <blockquote><pre>{@code
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
assertEquals("xy", (String) cat.invokeExact("x", "y"));
MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
assertEquals(bigType, d0.type());
assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
     * }</pre></blockquote>
     * <p>
     * This method is also equivalent to the following code:
     * <blockquote><pre>
     * {@link #dropArguments(MethodHandle,int,Class...) dropArguments}{@code (target, pos, valueTypes.toArray(new Class[0]))}
     * </pre></blockquote>
     * @param target the method handle to invoke after the arguments are dropped
     * @param valueTypes the type(s) of the argument(s) to drop
     * @param pos position of first argument to drop (zero for the leftmost)
     * @return a method handle which drops arguments of the given types,
     *         before calling the original method handle
     * @throws NullPointerException if the target is null,
     *                              or if the {@code valueTypes} list or any of its elements is null
     * @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class},
     *                  or if {@code pos} is negative or greater than the arity of the target,
     *                  or if the new method handle's type would have too many parameters
     */
    public static
    MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
        MethodType oldType = target.type();  // get NPE
        int dropped = dropArgumentChecks(oldType, pos, valueTypes);
        MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
        if (dropped == 0)  return target;
        BoundMethodHandle result = target.rebind();
        LambdaForm lform = result.form;
        int insertFormArg = 1 + pos;
        for (Class<?> ptype : valueTypes) {
            lform = lform.editor().addArgumentForm(insertFormArg++, BasicType.basicType(ptype));
        }
        result = result.copyWith(newType, lform);
        return result;
    }
 
Example #28
Source File: MethodHandles.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle makeZero(Class<?> rtype) {
    MethodType mtype = MethodType.methodType(rtype);
    LambdaForm lform = LambdaForm.zeroForm(BasicType.basicType(rtype));
    return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.ZERO);
}
 
Example #29
Source File: MethodHandles.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle makeIdentity(Class<?> ptype) {
    MethodType mtype = MethodType.methodType(ptype, ptype);
    LambdaForm lform = LambdaForm.identityForm(BasicType.basicType(ptype));
    return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.IDENTITY);
}
 
Example #30
Source File: MethodHandles.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle makeIdentity(Class<?> ptype) {
    MethodType mtype = MethodType.methodType(ptype, ptype);
    LambdaForm lform = LambdaForm.identityForm(BasicType.basicType(ptype));
    return MethodHandleImpl.makeIntrinsic(mtype, lform, Intrinsic.IDENTITY);
}