本文整理汇总了Java中java.lang.invoke.MethodHandle.asCollector方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandle.asCollector方法的具体用法?Java MethodHandle.asCollector怎么用?Java MethodHandle.asCollector使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodHandle
的用法示例。
在下文中一共展示了MethodHandle.asCollector方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createRelinkAndInvokeMethod
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
private MethodHandle createRelinkAndInvokeMethod(final RelinkableCallSite callSite, final int relinkCount) {
// Make a bound MH of invoke() for this linker and call site
final MethodHandle boundRelinker = MethodHandles.insertArguments(RELINK, 0, this, callSite, Integer.valueOf(
relinkCount));
// Make a MH that gathers all arguments to the invocation into an Object[]
final MethodType type = callSite.getDescriptor().getMethodType();
final MethodHandle collectingRelinker = boundRelinker.asCollector(Object[].class, type.parameterCount());
return MethodHandles.foldArguments(MethodHandles.exactInvoker(type), collectingRelinker.asType(
type.changeReturnType(MethodHandle.class)));
}
示例2: testArities
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
assert(mh_VA.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
}
}
示例3: testAsCollectorExample
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@Test
public static void testAsCollectorExample() throws Throwable {
// test the JavaDoc asCollector-with-pos example
StringWriter swr = new StringWriter();
MethodHandle swWrite = LOOKUP.
findVirtual(StringWriter.class, "write", methodType(void.class, char[].class, int.class, int.class)).
bindTo(swr);
MethodHandle swWrite4 = swWrite.asCollector(0, char[].class, 4);
swWrite4.invoke('A', 'B', 'C', 'D', 1, 2);
assertEquals("BC", swr.toString());
swWrite4.invoke('P', 'Q', 'R', 'S', 0, 4);
assertEquals("BCPQRS", swr.toString());
swWrite4.invoke('W', 'X', 'Y', 'Z', 3, 1);
assertEquals("BCPQRSZ", swr.toString());
}
示例4: testArities
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
MethodHandle mh_VA_h = mh.asSpreader(0, cls, arity-1);
assert(mh_VA.type().parameterType(0) == cls);
assert(mh_VA_h.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA, mh_VA_h);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
MethodHandle mh_VA2_h = mh_CA.asSpreader(0, cls, arity-1);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2, mh_VA2_h);
}
}
示例5: asCollector
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@Override
public MethodHandle asCollector(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
final MethodHandle mh = handle.asCollector(arrayType, arrayLength);
return debug(mh, "asCollector", handle, arrayType, arrayLength);
}
示例6: collectArguments
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
/**
* Creates a method handle out of the original target that will collect the varargs for the exact component type of
* the varArg array. Note that this will nicely trigger language-specific type converters for exactly those varargs
* for which it is necessary when later passed to linkerServices.convertArguments().
*
* @param target the original method handle
* @param parameterCount the total number of arguments in the new method handle
* @return a collecting method handle
*/
static MethodHandle collectArguments(final MethodHandle target, final int parameterCount) {
final MethodType methodType = target.type();
final int fixParamsLen = methodType.parameterCount() - 1;
final Class<?> arrayType = methodType.parameterType(fixParamsLen);
return target.asCollector(arrayType, parameterCount - fixParamsLen);
}