本文整理汇总了Java中java.lang.invoke.MethodHandle.asSpreader方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandle.asSpreader方法的具体用法?Java MethodHandle.asSpreader怎么用?Java MethodHandle.asSpreader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodHandle
的用法示例。
在下文中一共展示了MethodHandle.asSpreader方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
}
示例2: testAsSpreaderExample
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@Test
public static void testAsSpreaderExample() throws Throwable {
// test the JavaDoc asSpreader-with-pos example
MethodHandle compare = LOOKUP.findStatic(Objects.class, "compare", methodType(int.class, Object.class, Object.class, Comparator.class));
MethodHandle compare2FromArray = compare.asSpreader(0, Object[].class, 2);
Object[] ints = new Object[]{3, 9, 7, 7};
Comparator<Integer> cmp = (a, b) -> a - b;
assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 0, 2), cmp) < 0);
assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 1, 3), cmp) > 0);
assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 2, 4), cmp) == 0);
}
示例3: 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);
}
}
示例4: asSpreader
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
private static MethodHandle asSpreader(MethodHandle mh) {
int paramCount = mh.type().parameterCount();
if (mh.isVarargsCollector()) {
if (paramCount == 1) {
return mh;
}
return mh.asSpreader(Object[].class, paramCount - 1);
}
return mh.asSpreader(Object[].class, paramCount);
}
示例5: asSpreader
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@Override
public MethodHandle asSpreader(final MethodHandle handle, final Class<?> arrayType, final int arrayLength) {
final MethodHandle mh = handle.asSpreader(arrayType, arrayLength);
return debug(mh, "asSpreader", handle, arrayType, arrayLength);
}
示例6: testSpreads
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@Test
public void testSpreads() throws Throwable {
System.out.println("testing asSpreader on arity=3");
Object[] args = testArgs(3);
int r0 = Objects.hash(args);
MethodHandle mh = MH_hashArguments(3);
Object r;
r = mh.invokeExact(args[0], args[1], args[2]);
assertEquals(r0, r);
r = mh.invoke(args[0], args[1], args[2]);
assertEquals(r0, r);
r = mh.invoke((Comparable) args[0], (Integer) args[1], (Number) args[2]);
assertEquals(r0, r);
r = mh.invokeWithArguments(args);
assertEquals(r0, r);
for (Class<?> cls0 : new Class<?>[] {
Object[].class, Number[].class, Integer[].class, Comparable[].class
}) {
@SuppressWarnings("unchecked")
Class<? extends Object[]> cls = (Class<? extends Object[]>) cls0;
//Class<? extends Object[]> cls = Object[].class.asSubclass(cls0);
int nargs = args.length, skip;
MethodHandle smh = mh.asSpreader(cls, nargs - (skip = 0));
Object[] tail = Arrays.copyOfRange(args, skip, nargs, cls);
if (cls == Object[].class)
r = smh.invokeExact(tail);
else if (cls == Integer[].class)
r = smh.invokeExact((Integer[]) tail); //warning OK, see 8019340
else
r = smh.invoke(tail);
assertEquals(r0, r);
smh = mh.asSpreader(cls, nargs - (skip = 1));
tail = Arrays.copyOfRange(args, skip, nargs, cls);
if (cls == Object[].class)
r = smh.invokeExact(args[0], tail);
else if (cls == Integer[].class)
r = smh.invokeExact(args[0], (Integer[]) tail);
else
r = smh.invoke(args[0], tail);
assertEquals(r0, r);
smh = mh.asSpreader(cls, nargs - (skip = 2));
tail = Arrays.copyOfRange(args, skip, nargs, cls);
if (cls == Object[].class)
r = smh.invokeExact(args[0], args[1], tail);
else if (cls == Integer[].class)
r = smh.invokeExact(args[0], args[1], (Integer[]) tail);
else
r = smh.invoke(args[0], args[1], tail);
assertEquals(r0, r);
smh = mh.asSpreader(cls, nargs - (skip = 3));
tail = Arrays.copyOfRange(args, skip, nargs, cls);
if (cls == Object[].class)
r = smh.invokeExact(args[0], args[1], args[2], tail);
else if (cls == Integer[].class)
r = smh.invokeExact(args[0], args[1], args[2], (Integer[]) tail);
else
r = smh.invoke(args[0], args[1], args[2], tail);
assertEquals(r0, r);
// Try null array in addition to zero-length array:
tail = null;
if (cls == Object[].class)
r = smh.invokeExact(args[0], args[1], args[2], tail);
else if (cls == Integer[].class)
r = smh.invokeExact(args[0], args[1], args[2], (Integer[]) tail);
else
r = smh.invoke(args[0], args[1], args[2], tail);
assertEquals(r0, r);
}
}
示例7: testAsSpreaderIllegalMethodType
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@Test(expectedExceptions = {WrongMethodTypeException.class})
public static void testAsSpreaderIllegalMethodType() {
MethodHandle h = MethodHandles.dropArguments(MethodHandles.constant(String.class, ""), 0, int.class, int.class);
MethodHandle s = h.asSpreader(String[].class, 1);
}