MethodHandlesTest.java revision 4250
0N/A * @compile -source 7 -target 7 MethodHandlesTest.java
955N/Apublic class MethodHandlesTest {
0N/A static boolean CAN_SKIP_WORKING = false;
0N/A static boolean DO_MORE_CALLS = true;
// AMH.<init>: IllegalArgumentException: bad adapter (conversion=0xfffab300): adapter pushes too many parameters
// ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object
// ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object
// ToGeneric.<init>: UnsupportedOperationException: NYI: primitive parameters must follow references; entryType = (int,java.lang.Object)java.lang.Object
// ValueConversions.varargsArray: UnsupportedOperationException: NYI: cannot form a varargs array of length 13
public MethodHandlesTest() {
public void printCounts() {
else ++negTests;
return entry;
return (int)(value);
return (long)(value);
return (char)(value);
return (short)(value);
return (float)(value);
return (double)(value);
return (byte)(value);
return null;
static long nextArgVal;
if (moreBits)
static int nextArg() {
return (int) nextArg(false);
return nextArg(true);
return (long) nextArg();
return wrap;
{ param = c; break; }
return args;
return args;
static <T> T[] cat(T[] a, T... b) {
return res;
if (x == null) return x;
if (x instanceof List) {
public static interface IntExample {
public void v0();
{ { false, PUBLIC }, { false, PACKAGE }, { true, PRIVATE }, { true, EXAMPLE } }, //[1]: only PRIVATE
if (pubc)
return cases;
@Test
if (CAN_SKIP_WORKING) return;
void testFindStatic(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
void testFindStatic(Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
void testFindStatic(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
static final boolean DEBUG_METHOD_HANDLE_NAMES = Boolean.getBoolean("java.lang.invoke.MethodHandle.DEBUG_NAMES");
if (!DEBUG_METHOD_HANDLE_NAMES) {
assertEquals(s, x);
@Test
if (CAN_SKIP_WORKING) return;
void testFindVirtual(Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
void testFindVirtual(Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
void testFindVirtual(Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
void testFindVirtual(boolean positive, Lookup lookup, Class<?> rcvc, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
@Test
if (CAN_SKIP_WORKING) return;
System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
@Test
if (CAN_SKIP_WORKING) return;
void testBind(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
@Test
if (CAN_SKIP_WORKING) return;
void testUnreflect(Class<?> defc, boolean isStatic, Class<?> ret, String name, Class<?>... params) throws Throwable {
testUnreflectMaybeSpecial(null, (Boolean)ac[0], (Lookup)ac[1], defc, (isStatic ? null : defc), ret, name, params);
void testUnreflect(Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
if (isSpecial)
assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
if (!isStatic) {
if (isStatic) {
if (isSpecial)
void testUnreflectSpecial(Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
testUnreflectMaybeSpecial(specialCaller, (Boolean)ac[0], (Lookup)ac[1], defc, rcvc, ret, name, params);
@Test
if (CAN_SKIP_WORKING) return;
testUnreflectMaybeSpecial(Example.class, false, PRIVATE, Example.class, Example.class, void.class, "s0");
public static class HasFields {
boolean fZ = false;
static boolean sZ = true;
if (type == float.class) {
if (isStatic) v++;
cases.add(new Object[]{ new Object[]{ false, HasFields.class, "bogus_fD", double.class }, Error.class });
cases.add(new Object[]{ new Object[]{ true, HasFields.class, "bogus_sL", Object.class }, Error.class });
switch (testMode) {
@Test
@Test
@Test
boolean isStatic;
if (f != null) {
f = null;
if (isGetter)
switch (testMode0) {
case TEST_SETTER|
case TEST_SETTER|
case TEST_SETTER|
System.out.println("find"+(isStatic?"Static":"")+(isGetter?"Getter":"Setter")+" "+fclass.getName()+"."+fname+"/"+ftype
assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, mh != null);
if (isGetter) {
.changeReturnType(void.class)
if (isGetter) {
if (isStatic) {
if (ftype == int.class)
if (ftype == int.class)
if (isStatic) {
if (ftype == int.class)
if (ftype == int.class)
@Test
@Test
@Test
@Test
testArrayElementGetterSetter(false);
@Test
testArrayElementGetterSetter(true);
countTest(true);
if (verbosity >= 2) System.out.println("array type = "+array.getClass().getComponentType().getName()+"["+Array.getLength(array)+"]");
if (testSetter) {
if (elemType == int.class)
else if (elemType == boolean.class)
if (!testSetter) {
if (elemType == int.class)
else if (elemType == boolean.class)
return model;
static class Callee {
@Test
if (CAN_SKIP_WORKING) return;
void testConvert(MethodHandle id, Class<?> rtype, String name, Class<?>... params) throws Throwable {
assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
@Test
@Test
if (CAN_SKIP_WORKING) return;
public void testPermuteArguments(int max, Class<?> type1, int t2c, Class<?> type2, int dilution) throws Throwable {
casStep++;
if (i + d >= inargs) continue;
c /= inargs;
return reorder;
countTest();
for (int j : reorder) {
System.out.println("*** failed permuteArguments "+Arrays.toString(reorder)+" types="+Arrays.asList(types));
@Test
if (CAN_SKIP_WORKING) return;
countTest();
switch (nargs) {
Object args2 = ValueConversions.changeArrayType(arrayType, Arrays.copyOfRange(args, pos, args.length));
args1[pos] = ValueConversions.changeArrayType(arrayType, Arrays.copyOfRange(args, pos, args.length));
} else if (argType == int.class) {
} else if (argType == long.class) {
@Test
if (CAN_SKIP_WORKING) return;
countTest();
@Test
if (CAN_SKIP_WORKING) return;
countTest();
@Test
if (CAN_SKIP_WORKING) return;
List.class,
CharSequence.class,
String.class }) {
countTest();
System.out.println("*** fail at n/rt = "+nargs+"/"+rtype.getSimpleName()+": "+Arrays.asList(argsToPass)+" => "+result+" != "+expected);
@Test
if (CAN_SKIP_WORKING) return;
countTest();
System.out.println("*** fail at n/p = "+nargs+"/"+pos+": "+Arrays.asList(argsToPass)+" => "+result+" != "+expected);
@Test
if (CAN_SKIP_WORKING) return;
countTest();
System.out.println("*** fail at n/p/f = "+nargs+"/"+pos+"/"+fold+": "+argsToPass+" => "+result+" != "+expected);
@Test
if (CAN_SKIP_WORKING) return;
countTest();
// System.out.println("*** fail at n/p/d = "+nargs+"/"+pos+"/"+drop+": "+argsToDrop+" => "+res2List);
@Test
if (CAN_SKIP_WORKING) return;
else if (argType == void.class)
countTest();
countTest();
switch (nargs) {
countTest();
countTest();
assertEquals(x, y);
assertEquals(x, y);
@Test
if (CAN_SKIP_WORKING) return;
countTest();
MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.methodType(boolean.class, Object.class));
MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.genericMethodType(nargs));
MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.genericMethodType(nargs));
boolean equals;
switch (nargs) {
@Test
if (CAN_SKIP_WORKING) return;
private static <T extends Throwable>
return normal;
void testCatchException(Class<?> returnType, Throwable thrown, boolean throwIt, int nargs) throws Throwable {
countTest();
if (!throwIt) {
@Test
if (CAN_SKIP_WORKING) return;
countTest();
@Test
if (CAN_SKIP_WORKING) return;
static class Surprise {
countTest(false);
MethodHandle identity = Surprise.REF_IDENTITY, surprise0 = boo.asMethodHandle(), surprise = surprise0;
assertEquals(x, y);
assertEquals(x, z);
assertEquals(x, y);
assertTrue(false);
return null;
@Test
if (CAN_SKIP_WORKING) return;
static void runForRunnable() {
private interface Fooable {
private interface WillThrow {
@Test
if (CAN_SKIP_WORKING) return;
new MyCheckedException()
assertTrue(false);
String.class,
CharSequence.class,
Example.class }) {
assertTrue(false);
class ValueConversions {
MethodType vaType = MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType));
throw err;
class PackageSibling {