Search in sources :

Example 11 with MethodHandleInfo

use of java.lang.invoke.MethodHandleInfo in project openj9 by eclipse.

the class TestMethodHandleInfo method testNonPublicJSR292Method.

/**
 * Test a non-public JSR 292 handle.
 * These should be filtered out by MethodHandles.lookup().revealDirect()
 * and we should not get a MethodHandleInfo for them.
 */
@Test
public void testNonPublicJSR292Method() throws Throwable {
    try {
        Method method = MethodHandleProxies.class.getDeclaredMethod("isObjectMethod", Method.class);
        method.setAccessible(true);
        MethodHandle methodHandle = MethodHandles.lookup().unreflect(method);
        MethodHandleInfo info = MethodHandles.lookup().revealDirect(methodHandle);
        Assert.fail("MethodHandles.lookup().revealDirect() should have returned null because MethodHandleProxies.isObjectMethod() is a non-public method in the java.lang.invoke package.");
    } catch (IllegalArgumentException e) {
    // Success!
    }
}
Also used : Method(java.lang.reflect.Method) MethodHandleInfo(java.lang.invoke.MethodHandleInfo) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 12 with MethodHandleInfo

use of java.lang.invoke.MethodHandleInfo in project openj9 by eclipse.

the class TestMethodHandleInfo method testPublicStaticMethod.

/**
 * Test a public static method reference
 */
@Test
public void testPublicStaticMethod() {
    try {
        Class declaringClass = System.class;
        MethodType methodType = MethodType.methodType(String.class, String.class);
        String methodName = "getenv";
        MethodHandle methodHandle = MethodHandles.lookup().findStatic(declaringClass, methodName, methodType);
        MethodHandleInfo info = MethodHandles.lookup().revealDirect(methodHandle);
        checkMethodHandleInfo(info, declaringClass, methodType, Modifier.PUBLIC | Modifier.STATIC, methodName, MethodHandleInfo.REF_invokeStatic);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getLocalizedMessage());
    }
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandleInfo(java.lang.invoke.MethodHandleInfo) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 13 with MethodHandleInfo

use of java.lang.invoke.MethodHandleInfo in project openj9 by eclipse.

the class TestMethodHandleInfo method testStaticFieldSetter.

/**
 * Test a static field setter handle
 */
@Test
public void testStaticFieldSetter() {
    try {
        Class<?> declaringClass = this.getClass();
        String fieldName = "staticFieldSetterTestString";
        MethodHandle methodHandle = MethodHandles.lookup().findStaticSetter(declaringClass, fieldName, String.class);
        MethodHandleInfo info = MethodHandles.lookup().revealDirect(methodHandle);
        checkMethodHandleInfo(info, declaringClass, MethodType.methodType(void.class, String.class), Modifier.PUBLIC | Modifier.STATIC, fieldName, MethodHandleInfo.REF_putStatic);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getLocalizedMessage());
    }
}
Also used : MethodHandleInfo(java.lang.invoke.MethodHandleInfo) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 14 with MethodHandleInfo

use of java.lang.invoke.MethodHandleInfo in project openj9 by eclipse.

the class TestMethodHandleInfo method testFieldGetter.

/**
 * Test a field getter handle
 */
@Test
public void testFieldGetter() {
    try {
        Class<?> declaringClass = this.getClass();
        String fieldName = "fieldGetterTestInt";
        MethodHandle methodHandle = MethodHandles.lookup().findGetter(declaringClass, fieldName, int.class);
        MethodHandleInfo info = MethodHandles.lookup().revealDirect(methodHandle);
        checkMethodHandleInfo(info, declaringClass, MethodType.methodType(int.class), Modifier.PUBLIC | Modifier.VOLATILE, fieldName, MethodHandleInfo.REF_getField);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getLocalizedMessage());
    }
}
Also used : MethodHandleInfo(java.lang.invoke.MethodHandleInfo) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 15 with MethodHandleInfo

use of java.lang.invoke.MethodHandleInfo in project openj9 by eclipse.

the class Test_MethodHandle method testMethodDescribeConstableGeneralSub.

private void testMethodDescribeConstableGeneralSub(String testName, MethodHandle handle, MethodHandles.Lookup resolveLookup) throws Throwable {
    MethodHandleDesc handleDesc = handle.describeConstable().orElseThrow();
    /* verify that descriptor can be resolved. If not ReflectiveOperationException will be thrown. */
    MethodHandle resolvedHandle = (MethodHandle) handleDesc.resolveConstantDesc(resolveLookup);
    /* verify that resolved MethodType is the same as original */
    MethodHandleInfo oInfo = MethodHandles.lookup().revealDirect(handle);
    MethodHandleInfo rInfo = MethodHandles.lookup().revealDirect(resolvedHandle);
    logger.debug(testName + ": Descriptor of original MethodHandle " + oInfo.getMethodType().descriptorString() + " descriptor of MethodHandleDesc is: " + rInfo.getMethodType().descriptorString());
    Assert.assertTrue(oInfo.getMethodType().equals(rInfo.getMethodType()));
    Assert.assertTrue(oInfo.getDeclaringClass().equals(rInfo.getDeclaringClass()));
    Assert.assertTrue(oInfo.getName().equals(rInfo.getName()));
    Assert.assertEquals(oInfo.getModifiers(), rInfo.getModifiers());
    Assert.assertEquals(oInfo.getReferenceKind(), rInfo.getReferenceKind());
}
Also used : MethodHandleDesc(java.lang.constant.MethodHandleDesc) MethodHandleInfo(java.lang.invoke.MethodHandleInfo) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

MethodHandleInfo (java.lang.invoke.MethodHandleInfo)15 MethodHandle (java.lang.invoke.MethodHandle)13 Test (org.testng.annotations.Test)11 MethodType (java.lang.invoke.MethodType)5 MethodHandleDesc (java.lang.constant.MethodHandleDesc)1 Executable (java.lang.reflect.Executable)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1