use of java.lang.invoke.MethodHandles.Lookup in project jdk8u_jdk by JetBrains.
the class PackageSibling method testFindSpecial.
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
countTest(positive);
// foo/bar => foo
String methodName = name.substring(1 + name.indexOf('/'));
MethodType type = MethodType.methodType(ret, params);
Lookup specialLookup = maybeMoveIn(lookup, specialCaller);
boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller && (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
MethodHandle target = null;
Exception noAccess = null;
try {
if (verbosity >= 4)
System.out.println("lookup via " + lookup + " of " + defc + " " + name + type);
if (verbosity >= 5)
System.out.println(" lookup => " + specialLookup);
target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
} catch (ReflectiveOperationException ex) {
noAccess = ex;
assertExceptionClass(// this check should happen first
(!specialAccessOK) ? IllegalAccessException.class : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>")) ? NoSuchMethodException.class : IllegalAccessException.class, noAccess);
if (verbosity >= 5)
ex.printStackTrace(System.out);
}
if (verbosity >= 3)
System.out.println("findSpecial from " + specialCaller.getName() + " to " + defc.getName() + "." + name + "/" + type + " => " + target + (target == null ? "" : target.type()) + (noAccess == null ? "" : " !! " + noAccess));
if (positive && noAccess != null)
throw noAccess;
assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
// negative test failed as expected
if (!positive)
return;
assertEquals(specialCaller, target.type().parameterType(0));
assertEquals(type, target.type().dropParameterTypes(0, 1));
Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class) specialCaller), params);
MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
assertNameStringContains(target, methodName);
Object[] args = randomArgs(paramsWithSelf);
printCalled(target, name, args);
target.invokeWithArguments(args);
assertCalled(name, args);
}
use of java.lang.invoke.MethodHandles.Lookup in project jdk8u_jdk by JetBrains.
the class PackageSibling method testFindSpecial0.
public void testFindSpecial0() throws Throwable {
if (CAN_SKIP_WORKING)
return;
startTest("findSpecial");
testFindSpecial(SubExample.class, Example.class, void.class, "v0");
testFindSpecial(SubExample.class, Example.class, void.class, "pkg_v0");
testFindSpecial(RemoteExample.class, PubExample.class, void.class, "Pub/pro_v0");
// Do some negative testing:
for (Lookup lookup : new Lookup[] { PRIVATE, EXAMPLE, PACKAGE, PUBLIC }) {
testFindSpecial(false, lookup, Object.class, Example.class, void.class, "v0");
testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "bogus");
testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "<init>", int.class);
testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "<init>", Void.class);
testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "s0");
}
}
use of java.lang.invoke.MethodHandles.Lookup in project jdk8u_jdk by JetBrains.
the class PackageSibling method makeLists.
static MethodHandle[] makeLists() {
ArrayList<MethodHandle> lists = new ArrayList<>();
MethodHandles.Lookup lookup = IMPL_LOOKUP;
for (; ; ) {
int nargs = lists.size();
MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class);
String name = "list";
MethodHandle list = null;
try {
list = lookup.findStatic(ValueConversions.class, name, type);
} catch (ReflectiveOperationException ex) {
// break from loop!
}
if (list == null)
break;
lists.add(list);
}
// current number of methods
assertTrue(lists.size() == 11);
return lists.toArray(new MethodHandle[0]);
}
use of java.lang.invoke.MethodHandles.Lookup in project jdk8u_jdk by JetBrains.
the class PackageSibling method makeArrays.
static MethodHandle[] makeArrays() {
ArrayList<MethodHandle> arrays = new ArrayList<>();
MethodHandles.Lookup lookup = IMPL_LOOKUP;
for (; ; ) {
int nargs = arrays.size();
MethodType type = MethodType.genericMethodType(nargs).changeReturnType(Object[].class);
String name = "array";
MethodHandle array = null;
try {
array = lookup.findStatic(ValueConversions.class, name, type);
} catch (ReflectiveOperationException ex) {
// break from loop!
}
if (array == null)
break;
arrays.add(array);
}
// current number of methods
assertTrue(arrays.size() == 11);
return arrays.toArray(new MethodHandle[0]);
}
use of java.lang.invoke.MethodHandles.Lookup in project jdk8u_jdk by JetBrains.
the class PackageSibling method testGetter.
public void testGetter(int testMode) throws Throwable {
// FIXME: test more lookups than this one
Lookup lookup = PRIVATE;
for (Object[] c : HasFields.CASES) {
boolean positive = (c[1] != Error.class);
testGetter(positive, lookup, c[0], c[1], testMode);
if (positive)
testGetter(positive, lookup, c[0], c[1], testMode | TEST_NPE);
}
testGetter(true, lookup, new Object[] { true, System.class, "out", java.io.PrintStream.class }, System.out, testMode);
for (int isStaticN = 0; isStaticN <= 1; isStaticN++) {
testGetter(false, lookup, new Object[] { (isStaticN != 0), System.class, "bogus", char.class }, null, testMode);
}
}
Aggregations