下面列出了java.lang.invoke.MethodHandles#constant ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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))}
};
}
private void handleBoolean() {
if (handle != null) return;
// boolean->boolean, Boolean->boolean, boolean->Boolean
// is handled by compiler
// that leaves (T)Z and (T)Boolean, where T is the static type
// but runtime type of T might be Boolean
boolean primitive = staticTargetType == boolean.class;
if (!primitive && staticTargetType != Boolean.class) return;
if (args[0] == null) {
if (primitive) {
handle = MethodHandles.constant(boolean.class, false);
handle = MethodHandles.dropArguments(handle, 0, staticSourceType);
} else {
handle = BOOLEAN_IDENTITY;
}
} else if (args[0] instanceof Boolean) {
// give value through or unbox
handle = BOOLEAN_IDENTITY;
} else {
//call asBoolean
name = "asBoolean";
super.setCallSiteTarget();
}
}
/**
* 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);
}
@Test
public static void testOverriddenDefaultMethods() throws Throwable {
MethodHandle target = MethodHandles.constant(String.class, "F");
Override proxy = MethodHandleProxies.asInterfaceInstance(Override.class, target);
assertEquals(proxy.a(), "OA");
assertEquals(proxy.b(), "OB");
assertEquals(proxy.c(), "OC");
}
/**
* 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);
}
@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());
}
@Test
public static void testOverriddenDefaultMethods() throws Throwable {
MethodHandle target = MethodHandles.constant(String.class, "F");
Override proxy = MethodHandleProxies.asInterfaceInstance(Override.class, target);
assertEquals(proxy.a(), "OA");
assertEquals(proxy.b(), "OB");
assertEquals(proxy.c(), "OC");
}
@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());
}
@Test
public static void testOverriddenDefaultMethods() throws Throwable {
MethodHandle target = MethodHandles.constant(String.class, "F");
Override proxy = MethodHandleProxies.asInterfaceInstance(Override.class, target);
assertEquals(proxy.a(), "OA");
assertEquals(proxy.b(), "OB");
assertEquals(proxy.c(), "OC");
}
@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());
}
@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());
}
/**
* 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);
}
@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());
}
/**
* 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);
}
@Override
public MethodHandle constant(final Class<?> type, final Object value) {
return MethodHandles.constant(type, value);
}
@Override
public MethodHandle constant(final Class<?> type, final Object value) {
final MethodHandle mh = MethodHandles.constant(type, value);
return debug(mh, "constant", type, value);
}
@Override
public MethodHandle constant(final Class<?> type, final Object value) {
final MethodHandle mh = MethodHandles.constant(type, value);
return debug(mh, "constant", type, value);
}
@Override
public MethodHandle constant(final Class<?> type, final Object value) {
final MethodHandle mh = MethodHandles.constant(type, value);
return debug(mh, "constant", type, value);
}
@Override
public MethodHandle constant(final Class<?> type, final Object value) {
return MethodHandles.constant(type, value);
}
@DataProvider
static Object[][] nullArgs() {
MethodHandle c = MethodHandles.constant(int.class, 1);
return new Object[][]{{null, c}, {c, null}};
}