本文整理汇总了Java中java.lang.invoke.MethodHandles.constant方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandles.constant方法的具体用法?Java MethodHandles.constant怎么用?Java MethodHandles.constant使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodHandles
的用法示例。
在下文中一共展示了MethodHandles.constant方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: negativeTestData
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@DataProvider
static Object[][] negativeTestData() {
MethodHandle intid = MethodHandles.identity(int.class);
MethodHandle intco = MethodHandles.constant(int.class, 0);
MethodHandle errTarget = MethodHandles.dropArguments(intco, 0, int.class, double.class, String.class, int.class);
MethodHandle errCleanup = MethodHandles.dropArguments(MethodHandles.constant(int.class, 0), 0, Throwable.class,
int.class, double.class, Object.class);
MethodHandle voidTarget = TryFinally.MH_voidTarget;
MethodHandle voidICleanup = MethodHandles.dropArguments(TryFinally.MH_voidCleanup, 1, int.class);
return new Object[][]{
{intid, MethodHandles.identity(double.class),
"target and return types must match: double != int"},
{intid, MethodHandles.dropArguments(intid, 0, String.class),
"cleanup first argument and Throwable must match: (String,int)int != class java.lang.Throwable"},
{intid, MethodHandles.dropArguments(intid, 0, Throwable.class, double.class),
"cleanup second argument and target return type must match: (Throwable,double,int)int != int"},
{errTarget, errCleanup,
"cleanup parameters after (Throwable,result) and target parameter list prefix must match: " +
errCleanup.type() + " != " + errTarget.type()},
{voidTarget, voidICleanup,
"cleanup parameters after (Throwable,result) and target parameter list prefix must match: " +
voidICleanup.type() + " != " + voidTarget.type()}
};
}
示例2: 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();
}
}
示例3: 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))}
};
}
示例4: testMultipleArgs
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
/**
* Tests that MHs.eCA method works correctly with MHs with multiple arguments.
* @throws Throwable
*/
public static void testMultipleArgs() throws Throwable {
int arity = 1 + RNG.nextInt(Helper.MAX_ARITY / 2 - 2);
int arityMinus = RNG.nextInt(arity);
int arityPlus = arity + RNG.nextInt(Helper.MAX_ARITY / 2 - arity) + 1;
MethodType mType = Helper.randomMethodTypeGenerator(arity);
MethodType mTypeNew = Helper.randomMethodTypeGenerator(arity);
MethodType mTypeNewMinus = Helper.randomMethodTypeGenerator(arityMinus);
MethodType mTypeNewPlus = Helper.randomMethodTypeGenerator(arityPlus);
Class<?> rType = mType.returnType();
MethodHandle original;
if (rType.equals(void.class)) {
MethodType mt = MethodType.methodType(void.class);
original = MethodHandles.publicLookup()
.findStatic(THIS_CLASS, "retVoid", mt);
} else {
Object rValue = Helper.castToWrapper(1, rType);
original = MethodHandles.constant(rType, rValue);
}
original = Helper.addTrailingArgs(original, arity, mType.parameterList());
MethodHandle target = MethodHandles
.explicitCastArguments(original, mTypeNew);
Object[] parList = Helper.randomArgs(mTypeNew.parameterList());
for (int i = 0; i < parList.length; i++) {
if (parList[i] instanceof String) {
parList[i] = null; //getting rid of Stings produced by randomArgs
}
}
target.invokeWithArguments(parList);
checkForWrongMethodTypeException(original, mTypeNewMinus);
checkForWrongMethodTypeException(original, mTypeNewPlus);
}
示例5: testCountedPrintingLoop
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testCountedPrintingLoop() throws Throwable {
MethodHandle fit5 = MethodHandles.constant(int.class, 5);
MethodHandle loop = MethodHandles.countedLoop(fit5, null, Counted.MH_printHello);
assertEquals(Counted.MT_countedPrinting, loop.type());
loop.invoke();
}
示例6: testCountedLoopNullBody
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test(expectedExceptions = NullPointerException.class)
public static void testCountedLoopNullBody() throws Throwable {
MethodHandle h5 = MethodHandles.constant(int.class, 5);
MethodHandle h13 = MethodHandles.constant(int.class, 13);
MethodHandle loop = MethodHandles.countedLoop(h5, h13, null);
assertEquals(methodType(int.class), loop.type());
assertEquals(13, loop.invoke());
}
示例7: testCountedLoopCounterInit
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testCountedLoopCounterInit() throws Throwable {
// int x = 0; for (int i = 0; i < 5; ++i) { x += i; } return x; => 10
// (only if counter's first value in body is 0)
MethodHandle iter = MethodHandles.constant(int.class, 5);
MethodHandle init = MethodHandles.constant(int.class, 0);
MethodHandle body = Counted.MH_addCounter;
MethodHandle loop = MethodHandles.countedLoop(iter, init, body);
assertEquals(Counted.MT_counterInit, loop.type());
assertEquals(10, loop.invoke());
}
示例8: testShortZeroConversion
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public void testShortZeroConversion() throws Throwable {
MethodHandle h1 = MethodHandles.constant(Short.class, (short)42);
MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 42
MethodHandle h3 = h2.asType(MethodType.methodType(short.class)); // add 0
MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box
Object x = h4.invokeExact();
assertEquals(x, (short)0);
assertTrue(x == Short.valueOf((short)0));
assertTrue(x == Wrapper.SHORT.zero());
}
示例9: testIntZeroConversion
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public void testIntZeroConversion() throws Throwable {
MethodHandle h1 = MethodHandles.constant(Integer.class, 42);
MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 42
MethodHandle h3 = h2.asType(MethodType.methodType(int.class)); // add 0
MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box
Object x = h4.invokeExact();
assertEquals(x, 0);
assertTrue(x == Integer.valueOf(0));
assertTrue(x == Wrapper.INT.zero());
}
示例10: testLongZeroConversion
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public void testLongZeroConversion() throws Throwable {
MethodHandle h1 = MethodHandles.constant(Long.class, 42L);
MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 42
MethodHandle h3 = h2.asType(MethodType.methodType(long.class)); // add 0
MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box
Object x = h4.invokeExact();
assertEquals(x, 0L);
assertTrue(x == Long.valueOf(0));
assertTrue(x == Wrapper.LONG.zero());
}
示例11: testByteZeroConversion
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public void testByteZeroConversion() throws Throwable {
MethodHandle h1 = MethodHandles.constant(Byte.class, (byte)42);
MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 42
MethodHandle h3 = h2.asType(MethodType.methodType(byte.class)); // add 0
MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box
Object x = h4.invokeExact();
assertEquals(x, (byte)0);
assertTrue(x == Byte.valueOf((byte)0));
assertTrue(x == Wrapper.BYTE.zero());
}
示例12: testCharacterZeroConversion
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public void testCharacterZeroConversion() throws Throwable {
MethodHandle h1 = MethodHandles.constant(Character.class, (char)42);
MethodHandle h2 = h1.asType(MethodType.methodType(void.class)); // drop 42
MethodHandle h3 = h2.asType(MethodType.methodType(char.class)); // add 0
MethodHandle h4 = h3.asType(MethodType.methodType(Object.class)); // box
Object x = h4.invokeExact();
assertEquals(x, (char)0);
assertTrue(x == Character.valueOf((char)0));
assertTrue(x == Wrapper.CHAR.zero());
}
示例13: constant
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Override
public MethodHandle constant(final Class<?> type, final Object value) {
final MethodHandle mh = MethodHandles.constant(type, value);
return debug(mh, "constant", type, value);
}
示例14: negativeTestData
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@DataProvider
static Object[][] negativeTestData() {
MethodHandle i0 = MethodHandles.constant(int.class, 0);
MethodHandle ii = MethodHandles.dropArguments(i0, 0, int.class, int.class);
MethodHandle id = MethodHandles.dropArguments(i0, 0, int.class, double.class);
MethodHandle i3 = MethodHandles.dropArguments(i0, 0, int.class, int.class, int.class);
List<MethodHandle> inits = Arrays.asList(ii, id, i3);
List<Class<?>> ints3 = Arrays.asList(int.class, int.class, int.class);
List<Class<?>> ints4 = Arrays.asList(int.class, int.class, int.class, int.class);
List<MethodHandle> finis = Arrays.asList(Fac.MH_fin, Fac.MH_inc, Counted.MH_step);
List<MethodHandle> preds1 = Arrays.asList(null, null, null);
List<MethodHandle> preds2 = Arrays.asList(null, Fac.MH_fin, null);
MethodHandle eek = MethodHandles.dropArguments(i0, 0, int.class, int.class, double.class);
List<MethodHandle> nesteps = Arrays.asList(Fac.MH_inc, eek, Fac.MH_dot);
List<MethodHandle> nepreds = Arrays.asList(null, Fac.MH_pred, null);
List<MethodHandle> nefinis = Arrays.asList(null, Fac.MH_fin, null);
List<MethodHandle> lvsteps = Arrays.asList(LoopWithVirtuals.MH_inc, LoopWithVirtuals.MH_mult);
List<MethodHandle> lvpreds = Arrays.asList(null, LoopWithVirtuals.MH_pred);
List<MethodHandle> lvfinis = Arrays.asList(null, LoopWithVirtuals.MH_fin);
return new Object[][] {
{null, "null or no clauses passed"},
{new MethodHandle[][]{}, "null or no clauses passed"},
{new MethodHandle[][]{{null, Fac.MH_inc}, {Fac.MH_one, null, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin}},
"All loop clauses must be represented as MethodHandle arrays with at most 4 elements."},
{new MethodHandle[][]{{null, Fac.MH_inc}, null}, "null clauses are not allowed"},
{new MethodHandle[][]{{Fac.MH_zero, Fac.MH_dot}},
"clause 0: init and step return types must match: int != void"},
{new MethodHandle[][]{{ii}, {id}, {i3}},
"found non-effectively identical init parameter type lists: " + inits +
" (common suffix: " + ints3 + ")"},
{new MethodHandle[][]{{null, Fac.MH_inc, null, Fac.MH_fin}, {null, Fac.MH_inc, null, Fac.MH_inc},
{null, Counted.MH_start, null, Counted.MH_step}},
"found non-identical finalizer return types: " + finis + " (return type: int)"},
{new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, null, Fac.MH_fin},
{null, Fac.MH_dot}}, "no predicate found: " + preds1},
{new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, Fac.MH_fin, Fac.MH_fin},
{null, Fac.MH_dot}}, "predicates must have boolean return type: " + preds2},
{new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, eek, Fac.MH_pred, Fac.MH_fin},
{null, Fac.MH_dot}},
"found non-effectively identical parameter type lists:\nstep: " + nesteps +
"\npred: " + nepreds + "\nfini: " + nefinis + " (common parameter sequence: " + ints3 + ")"},
{new MethodHandle[][]{{null, LoopWithVirtuals.MH_inc},
{LoopWithVirtuals.MH_one, LoopWithVirtuals.MH_mult, LoopWithVirtuals.MH_pred, LoopWithVirtuals.MH_fin}},
"found non-effectively identical parameter type lists:\nstep: " + lvsteps +
"\npred: " + lvpreds + "\nfini: " + lvfinis + " (common parameter sequence: " + ints4 + ")"}
};
}
示例15: nullArgs
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@DataProvider
static Object[][] nullArgs() {
MethodHandle c = MethodHandles.constant(int.class, 1);
return new Object[][]{{null, c}, {c, null}};
}