use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.
the class TestPrivateMemberPackageSibling method test.
public void test() throws Throwable {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType mt = MethodType.methodType(void.class);
try {
Class<?> checkInittedHolder = TestPrivateMemberPackageSibling.class;
// Original model: checkInittedHolder = Class.class;
// Not using Class.checkInitted because it could change without notice.
MethodHandle mh = lookup.findStatic(checkInittedHolder, "checkInitted", mt);
throw new RuntimeException("IllegalAccessException not thrown");
} catch (IllegalAccessException e) {
// okay
System.out.println("Expected exception: " + e.getMessage());
}
}
use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.
the class LambdaReturn method main.
public static void main(String[] args) throws Throwable {
l = MethodHandles.lookup();
MethodHandle hV = l.findStatic(LambdaReturn.class, "hereV", mt(void.class));
MethodHandle hS = l.findStatic(LambdaReturn.class, "hereS", mt(String.class));
List<String> errs = new ArrayList<>();
MethodType V = mt(void.class);
MethodType S = mt(String.class);
MethodType O = mt(Object.class);
MethodType I = mt(int.class);
amf(errs, hS, S, S, O, true);
amf(errs, hS, S, S, V, false);
amf(errs, hS, S, S, I, false);
amf(errs, hS, O, S, S, true);
amf(errs, hS, V, S, S, false);
amf(errs, hS, I, S, S, false);
amf(errs, hS, O, O, S, false);
amf(errs, hS, S, O, O, false);
amf(errs, hV, V, V, O, false);
amf(errs, hV, V, V, I, false);
amf(errs, hV, V, V, S, false);
amf(errs, hV, O, V, V, false);
amf(errs, hV, I, V, V, false);
amf(errs, hV, S, V, V, false);
if (errs.size() > 0) {
for (String err : errs) {
System.err.println(err);
}
throw new AssertionError("Errors: " + errs.size());
}
}
use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.
the class ValueConversions method convertPrimitive.
public static MethodHandle convertPrimitive(Wrapper wsrc, Wrapper wdst) {
WrapperCache cache = CONVERT_PRIMITIVE_FUNCTIONS[wsrc.ordinal()];
MethodHandle mh = cache.get(wdst);
if (mh != null) {
return mh;
}
// slow path
Class<?> src = wsrc.primitiveType();
Class<?> dst = wdst.primitiveType();
MethodType type = MethodType.methodType(dst, src);
if (wsrc == wdst) {
mh = MethodHandles.identity(src);
} else {
assert (src.isPrimitive() && dst.isPrimitive());
try {
mh = IMPL_LOOKUP.findStatic(THIS_CLASS, src.getSimpleName() + "To" + capitalize(dst.getSimpleName()), type);
} catch (ReflectiveOperationException ex) {
mh = null;
}
}
if (mh != null) {
assert (mh.type() == type) : mh;
return cache.put(wdst, mh);
}
throw new IllegalArgumentException("cannot find primitive conversion function for " + src.getSimpleName() + " -> " + dst.getSimpleName());
}
use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.
the class ValueConversions method unbox.
private static MethodHandle unbox(Wrapper wrap, int kind) {
// kind 0 -> strongly typed with NPE
// kind 1 -> strongly typed but zero for null,
// kind 2 -> asType rules: accept multiple box types but only widening conversions with NPE
// kind 3 -> explicitCastArguments rules: allow narrowing conversions, zero for null
WrapperCache cache = UNBOX_CONVERSIONS[kind];
MethodHandle mh = cache.get(wrap);
if (mh != null) {
return mh;
}
// slow path
switch(wrap) {
case OBJECT:
case VOID:
throw new IllegalArgumentException("unbox " + wrap);
}
// look up the method
String name = "unbox" + wrap.wrapperSimpleName();
MethodType type = unboxType(wrap, kind);
try {
mh = IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
} catch (ReflectiveOperationException ex) {
mh = null;
}
if (mh != null) {
if (kind > 0) {
boolean cast = (kind != 2);
mh = MethodHandles.insertArguments(mh, 1, cast);
}
if (kind == 1) {
// casting but exact (null -> zero)
mh = mh.asType(unboxType(wrap, 0));
}
return cache.put(wrap, mh);
}
throw new IllegalArgumentException("cannot find unbox adapter for " + wrap + (kind <= 1 ? " (exact)" : kind == 3 ? " (cast)" : ""));
}
use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.
the class AccessControlTest_sibling method testOneAccess.
private void testOneAccess(LookupCase sourceCase, Method method, String kind) {
Class<?> targetClass = method.getDeclaringClass();
String methodName = method.getName();
MethodType methodType = methodType(method.getReturnType(), method.getParameterTypes());
boolean willAccess = sourceCase.willAccess(method);
boolean didAccess = false;
ReflectiveOperationException accessError = null;
try {
switch(kind) {
case "find":
if ((method.getModifiers() & Modifier.STATIC) != 0)
sourceCase.lookup().findStatic(targetClass, methodName, methodType);
else
sourceCase.lookup().findVirtual(targetClass, methodName, methodType);
break;
case "unreflect":
sourceCase.lookup().unreflect(method);
break;
default:
throw new AssertionError(kind);
}
didAccess = true;
} catch (ReflectiveOperationException ex) {
accessError = ex;
}
if (willAccess != didAccess) {
System.out.println(sourceCase + " => " + targetClass.getSimpleName() + "." + methodName + methodType);
System.out.println("fail on " + method + " ex=" + accessError);
assertEquals(willAccess, didAccess);
}
testCount++;
if (!didAccess)
testCountFails++;
}
Aggregations