下面列出了怎么用java.lang.invoke.LambdaForm.BasicType的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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);
}
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);
}
/**
* 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;
}
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);
}
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);
}
/**
* 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;
}
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);
}
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);
}
/**
* 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;
}
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);
}
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);
}
/**
* 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;
}
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);
}
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);
}
/**
* 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;
}
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);
}
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);
}