本文整理匯總了Java中java.lang.invoke.MethodHandles.zero方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodHandles.zero方法的具體用法?Java MethodHandles.zero怎麽用?Java MethodHandles.zero使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.invoke.MethodHandles
的用法示例。
在下文中一共展示了MethodHandles.zero方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testCountedLoopVoidInit
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Test
public static void testCountedLoopVoidInit() throws Throwable {
MethodHandle fit5 = MethodHandles.constant(int.class, 5);
for (int i = 0; i < 8; i++) {
MethodHandle zero = MethodHandles.zero(void.class);
MethodHandle init = fit5;
MethodHandle body = Counted.MH_printHello;
boolean useNull = (i & 1) != 0, addInitArg = (i & 2) != 0, addBodyArg = (i & 4) != 0;
if (useNull) zero = null;
if (addInitArg) init = MethodHandles.dropArguments(init, 0, int.class);
if (addBodyArg) body = MethodHandles.dropArguments(body, 1, int.class);
System.out.println("testCountedLoopVoidInit i="+i+" : "+Arrays.asList(init, zero, body));
MethodHandle loop = MethodHandles.countedLoop(init, zero, body);
MethodType expectedType = Counted.MT_countedPrinting;
if (addInitArg || addBodyArg)
expectedType = expectedType.insertParameterTypes(0, int.class);
assertEquals(expectedType, loop.type());
if (addInitArg || addBodyArg)
loop.invoke(99);
else
loop.invoke();
}
}
示例2: countedLoopNegativeData
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@DataProvider
static Object[][] countedLoopNegativeData() {
MethodHandle dummy = MethodHandles.zero(void.class);
MethodHandle one = MethodHandles.constant(int.class, 1);
MethodHandle oneString = MethodHandles.dropArguments(one, 0, String.class);
MethodHandle oneDouble = MethodHandles.dropArguments(one, 0, double.class);
return new Object[][]{
{dummy, one, dummy, dummy, String.format("start/end must return int %s, %s", dummy, one)},
{one, dummy, dummy, dummy, String.format("start/end must return int %s, %s", one, dummy)},
{oneString, oneDouble, dummy, dummy,
String.format("start and end parameter types must match: %s != %s", oneString.type(),
oneDouble.type())},
{oneString, oneString, dummy, dummy,
String.format("start/end and init parameter types must match: %s != %s", oneString.type(),
dummy.type())},
{one, one, null, dummy, String.format("actual and expected body signatures must match: %s != %s",
dummy.type(), dummy.type().appendParameterTypes(int.class))}
};
}
示例3: testZeroNPE
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Test
@ExpectedExceptions(NullPointerException.class)
public void testZeroNPE() {
MethodHandle mh = MethodHandles.zero(null);
}