use of java.lang.invoke.MethodHandleInfo in project jdk8u_jdk by JetBrains.
the class RevealDirectTest method testWithMember.
void testWithMember(Member mem, // initial lookup of member => MH
Lookup lookup, // reveal MH => info
Lookup revLookup, // reflect info => member
Lookup refLookup, int failureMode) throws Throwable {
// testOnMembersNoLookup
boolean expectEx1 = (failureMode == FAIL_LOOKUP);
// testOnMembersNoReveal
boolean expectEx2 = (failureMode == FAIL_REVEAL);
// testOnMembersNoReflect
boolean expectEx3 = (failureMode == FAIL_REFLECT);
for (int variation = 0; ; variation++) {
UnreflectResult res = unreflectMember(lookup, mem, variation);
failureModeCounts[failureMode] += 1;
if (variation == 0)
assert (res != null);
if (res == null)
break;
if (VERBOSE && variation == 0)
System.out.println("from " + mem.getDeclaringClass().getSimpleName());
MethodHandle mh = res.mh;
Throwable ex1 = res.ex;
if (VERBOSE)
System.out.println(" " + variation + ": " + res + " << " + (mh != null ? mh : ex1));
if (expectEx1 && ex1 != null)
// this is OK; we expected that lookup to fail
continue;
if (expectEx1)
throw new AssertionError("unexpected lookup for negative test");
if (ex1 != null && !expectEx1) {
if (failureMode != NO_FAIL)
throw new AssertionError("unexpected lookup failure for negative test", ex1);
throw ex1;
}
MethodHandleInfo info;
try {
info = revLookup.revealDirect(mh);
if (expectEx2)
throw new AssertionError("unexpected revelation for negative test");
} catch (IllegalArgumentException | SecurityException ex2) {
if (VERBOSE)
System.out.println(" " + variation + ": " + res + " => " + mh.getClass().getName() + " => (EX2)" + ex2);
if (expectEx2)
// this is OK; we expected the reflect to fail
continue;
if (failureMode != NO_FAIL)
throw new AssertionError("unexpected revelation failure for negative test", ex2);
throw ex2;
}
assert (consistent(res, info));
Member mem2;
try {
mem2 = info.reflectAs(Member.class, refLookup);
if (expectEx3)
throw new AssertionError("unexpected reflection for negative test");
assert (!(mem instanceof SignaturePolymorphicMethod));
} catch (IllegalArgumentException ex3) {
if (VERBOSE)
System.out.println(" " + variation + ": " + info + " => (EX3)" + ex3);
if (expectEx3)
// this is OK; we expected the reflect to fail
continue;
if (mem instanceof SignaturePolymorphicMethod)
// this is OK; we cannot reflect MH.invokeExact(a,b,c)
continue;
if (failureMode != NO_FAIL)
throw new AssertionError("unexpected reflection failure for negative test", ex3);
throw ex3;
}
assert (consistent(mem, mem2));
UnreflectResult res2 = unreflectMember(lookup, mem2, variation);
MethodHandle mh2 = res2.mh;
assert (consistent(mh, mh2));
MethodHandleInfo info2 = lookup.revealDirect(mh2);
assert (consistent(info, info2));
assert (consistent(res, info2));
Member mem3;
if (hasSM())
mem3 = info2.reflectAs(Member.class, lookup);
else
mem3 = MethodHandles.reflectAs(Member.class, mh2);
assert (consistent(mem2, mem3));
if (hasSM()) {
try {
MethodHandles.reflectAs(Member.class, mh2);
throw new AssertionError("failed to throw on " + mem3);
} catch (SecurityException ex3) {
// OK...
}
}
}
}
use of java.lang.invoke.MethodHandleInfo in project Taskbar by farmerbb.
the class HiddenApiBypass method getDeclaredMethods.
/**
* get declared methods of given class without hidden api restriction
*
* @param clazz the class to fetch declared methods
* @return list of declared methods of {@code clazz}
*/
public static List<Executable> getDeclaredMethods(Class<?> clazz) {
ArrayList<Executable> list = new ArrayList<>();
if (clazz.isPrimitive() || clazz.isArray())
return list;
MethodHandle mh;
try {
mh = MethodHandles.lookup().unreflect(Helper.NeverCall.class.getDeclaredMethod("a"));
} catch (NoSuchMethodException | IllegalAccessException e) {
return list;
}
long methods = unsafe.getLong(clazz, methodsOffset);
int numMethods = unsafe.getInt(methods);
if (BuildConfig.DEBUG)
Log.d(TAG, clazz + " has " + numMethods + " methods");
for (int i = 0; i < numMethods; i++) {
long method = methods + i * size + bias;
unsafe.putLong(mh, artOffset, method);
unsafe.putObject(mh, infoOffset, null);
try {
MethodHandles.lookup().revealDirect(mh);
} catch (Throwable ignored) {
}
MethodHandleInfo info = (MethodHandleInfo) unsafe.getObject(mh, infoOffset);
Executable member = (Executable) unsafe.getObject(info, memberOffset);
if (BuildConfig.DEBUG)
Log.v(TAG, "got " + clazz.getTypeName() + "." + member + "(" + Arrays.stream(member.getTypeParameters()).map(Type::getTypeName).collect(Collectors.joining()) + ")");
list.add(member);
}
return list;
}
use of java.lang.invoke.MethodHandleInfo in project openj9 by eclipse.
the class TestMethodHandleInfo method testConstructor.
/**
* Test a simple constructor
*/
@Test
public void testConstructor() {
try {
MethodType methodType = MethodType.methodType(void.class);
MethodHandle methodHandle = MethodHandles.lookup().findConstructor(Object.class, methodType);
MethodHandleInfo info = MethodHandles.lookup().revealDirect(methodHandle);
checkMethodHandleInfo(info, Object.class, MethodType.methodType(void.class), Modifier.PUBLIC, "<init>", MethodHandleInfo.REF_newInvokeSpecial);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getLocalizedMessage());
}
}
use of java.lang.invoke.MethodHandleInfo in project openj9 by eclipse.
the class TestMethodHandleInfo method testStaticFieldGetter.
/**
* Test a static field getter handle
*/
@Test
public void testStaticFieldGetter() {
try {
Class declaringClass = this.getClass();
String fieldName = "staticFieldGetterTestInt";
MethodHandle methodHandle = MethodHandles.lookup().findStaticGetter(declaringClass, fieldName, int.class);
MethodHandleInfo info = MethodHandles.lookup().revealDirect(methodHandle);
checkMethodHandleInfo(info, declaringClass, MethodType.methodType(int.class), Modifier.PUBLIC | Modifier.VOLATILE | Modifier.STATIC, fieldName, MethodHandleInfo.REF_getStatic);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getLocalizedMessage());
}
}
use of java.lang.invoke.MethodHandleInfo in project openj9 by eclipse.
the class TestMethodHandleInfo method testVarargs.
/**
* Test a varargs handle
*/
@Test
public void testVarargs() {
try {
Class<?> declaringClass = String.class;
/* public static java.lang.String.format(String format, Object... args) */
String methodName = "format";
MethodType methodType = MethodType.methodType(String.class, String.class, Object[].class);
MethodHandle methodHandle = MethodHandles.lookup().findStatic(declaringClass, methodName, methodType);
AssertJUnit.assertTrue("methodHandle is not an instance of VarargsCollectorHandle", methodHandle.isVarargsCollector());
MethodHandleInfo info = MethodHandles.lookup().revealDirect(methodHandle);
checkMethodHandleInfo(info, declaringClass, methodType, Modifier.PUBLIC | Modifier.STATIC | Modifier.TRANSIENT, /* Note: MethodHandles.lookup().VARARGS == Modifiers.TRANSIENT, the same bit is reused */
methodName, MethodHandleInfo.REF_invokeStatic);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getLocalizedMessage());
}
}
Aggregations