Search in sources :

Example 1 with ClassDesc

use of java.lang.constant.ClassDesc in project openj9 by eclipse.

the class MethodType method describeConstable.

/**
 * Returns the nominal descriptor of this MethodType instance, or an empty Optional
 * if construction is not possible.
 *
 * @return Optional with a nominal descriptor of MethodType instance
 */
public Optional<MethodTypeDesc> describeConstable() {
    try {
        ClassDesc returnDesc = rtype.describeConstable().orElseThrow();
        /* convert parameter classes to ClassDesc */
        final int argumentsLength = ptypes.length;
        ClassDesc[] paramDescs = new ClassDesc[argumentsLength];
        for (int i = 0; i < argumentsLength; i++) {
            paramDescs[i] = ptypes[i].describeConstable().orElseThrow();
        }
        /* create MethodTypeDesc */
        MethodTypeDesc typeDesc = MethodTypeDesc.of(returnDesc, paramDescs);
        return Optional.of(typeDesc);
    } catch (NoSuchElementException e) {
        return Optional.empty();
    }
}
Also used : MethodTypeDesc(java.lang.constant.MethodTypeDesc) NoSuchElementException(java.util.NoSuchElementException) ClassDesc(java.lang.constant.ClassDesc)

Example 2 with ClassDesc

use of java.lang.constant.ClassDesc in project openj9 by eclipse.

the class Test_Class method describeConstableTestGeneral.

private void describeConstableTestGeneral(String testName, Class<?> testClass) throws Throwable {
    Optional<ClassDesc> optionalDesc = testClass.describeConstable();
    ClassDesc desc = optionalDesc.orElseThrow();
    /*
		 * verify that descriptor can be resolved. Otherwise exception will be thrown.
		 */
    Class<?> resolvedClass = (Class<?>) desc.resolveConstantDesc(MethodHandles.lookup());
    String originalDescriptor = testClass.descriptorString();
    String newDescriptor = resolvedClass.descriptorString();
    logger.debug(testName + ": Descriptor of original class is: " + originalDescriptor + " descriptor of ClassDesc is: " + newDescriptor);
    Assert.assertTrue(testClass.equals(resolvedClass));
}
Also used : ClassDesc(java.lang.constant.ClassDesc)

Example 3 with ClassDesc

use of java.lang.constant.ClassDesc in project openj9 by eclipse.

the class Test_DynamicCallSiteDesc method testDynamicCallSiteDescResolveCallSiteDesc.

/*
	 * Test Java 12 API DynamicCallSiteDesc.resolveCallSiteDesc()
	 */
@Test(groups = { "level.sanity" })
public void testDynamicCallSiteDescResolveCallSiteDesc() throws Throwable {
    /* setup */
    ClassDesc bsmOwnerDesc = Test_DynamicCallSiteDesc.class.describeConstable().orElseThrow();
    ClassDesc bsmRetTypeDesc = ConstantCallSite.class.describeConstable().orElseThrow();
    MethodTypeDesc handleType = MethodType.methodType(void.class).describeConstable().orElseThrow();
    /* describe and resolve callsite */
    DirectMethodHandleDesc bsm = ConstantDescs.ofCallsiteBootstrap(bsmOwnerDesc, "bsm", bsmRetTypeDesc);
    DynamicCallSiteDesc desc = DynamicCallSiteDesc.of(bsm, fieldName, handleType);
    CallSite resolvedSite = desc.resolveCallSiteDesc(MethodHandles.lookup());
    /* verify that the callsite contains the expected MethodHandle */
    logger.debug("testDynamicCallSiteDescResolveCallSiteDesc: resolved CallSite type is: " + resolvedSite.type().toMethodDescriptorString());
    Assert.assertTrue(fieldName.equals((String) resolvedSite.getTarget().invokeExact()));
}
Also used : DirectMethodHandleDesc(java.lang.constant.DirectMethodHandleDesc) ConstantCallSite(java.lang.invoke.ConstantCallSite) MethodTypeDesc(java.lang.constant.MethodTypeDesc) CallSite(java.lang.invoke.CallSite) ConstantCallSite(java.lang.invoke.ConstantCallSite) ClassDesc(java.lang.constant.ClassDesc) DynamicCallSiteDesc(java.lang.constant.DynamicCallSiteDesc) Test(org.testng.annotations.Test)

Example 4 with ClassDesc

use of java.lang.constant.ClassDesc in project openj9 by eclipse.

the class Test_DynamicConstantDesc method testDynamicConstantDescResolveConstantDescVarHandleArray.

@Test(groups = { "level.sanity" })
public void testDynamicConstantDescResolveConstantDescVarHandleArray() throws Throwable {
    ClassDesc arrayClassDesc = int[].class.describeConstable().orElseThrow();
    /* describe and resolve constant */
    DynamicConstantDesc<VarHandle> desc = DynamicConstantDesc.ofNamed(ConstantDescs.BSM_VARHANDLE_ARRAY, "af", ConstantDescs.CD_VarHandle, new ConstantDesc[] { arrayClassDesc });
    VarHandle resolvedClass = (VarHandle) desc.resolveConstantDesc(MethodHandles.lookup());
    DynamicConstantDesc<VarHandle> resDesc = resolvedClass.describeConstable().orElseThrow();
    /* test if descriptors are equals */
    logger.debug("testDynamicConstantDescResolveConstantDescVarHandleArray" + ": original is: " + desc.toString() + " resolved desc is: " + resDesc.toString());
    /* test equivalence manually, descriptor names will not be equal */
    Assert.assertTrue(Arrays.equals(desc.bootstrapArgs(), resDesc.bootstrapArgs()));
    Assert.assertTrue(desc.bootstrapMethod().equals(resDesc.bootstrapMethod()));
    Assert.assertEquals(resDesc.constantName(), "_");
    Assert.assertEquals(desc.constantType(), resDesc.constantType());
}
Also used : VarHandle(java.lang.invoke.VarHandle) ClassDesc(java.lang.constant.ClassDesc) Test(org.testng.annotations.Test)

Example 5 with ClassDesc

use of java.lang.constant.ClassDesc in project openj9 by eclipse.

the class Test_VarHandleDesc method toStringTestGeneral.

/* generic test for toString() */
private void toStringTestGeneral(String testName, int test_type, VarHandle handle) {
    Optional<ClassDesc> descOptional = null;
    if (test_type == Jep334MHHelperImpl.array_test) {
        descOptional = handle.varType().arrayType().describeConstable();
    } else {
        descOptional = handle.varType().describeConstable();
    }
    if (!descOptional.isPresent()) {
        Assert.fail(testName + ": error with tests ClassDesc could not be generated.");
        return;
    }
    ClassDesc desc = descOptional.get();
    VarHandleDesc varDesc = null;
    String resultString = null;
    switch(test_type) {
        case Jep334MHHelperImpl.array_test:
            varDesc = VarHandleDesc.ofArray(desc);
            resultString = "VarHandleDesc[" + desc.displayName() + "[]]";
            break;
        case Jep334MHHelperImpl.instance_test:
            varDesc = VarHandleDesc.ofField(declaringClassDesc, desc.displayName(), desc);
            resultString = "VarHandleDesc[" + declaringClassDesc.displayName() + "." + varDesc.constantName() + ":" + varDesc.varType().displayName() + "]";
            break;
        case Jep334MHHelperImpl.static_test:
            varDesc = VarHandleDesc.ofStaticField(declaringClassDesc, desc.displayName(), desc);
            resultString = "VarHandleDesc[static " + declaringClassDesc.displayName() + "." + varDesc.constantName() + ":" + varDesc.varType().displayName() + "]";
            break;
        default:
            Assert.fail(testName + ": unsupported test type.");
    }
    String outputString = varDesc.toString();
    logger.debug(testName + ": toString output is: " + outputString + " and should be: " + resultString);
    Assert.assertTrue(outputString.equals(resultString));
}
Also used : VarHandleDesc(java.lang.invoke.VarHandle.VarHandleDesc) ClassDesc(java.lang.constant.ClassDesc)

Aggregations

ClassDesc (java.lang.constant.ClassDesc)7 MethodTypeDesc (java.lang.constant.MethodTypeDesc)3 DirectMethodHandleDesc (java.lang.constant.DirectMethodHandleDesc)2 VarHandleDesc (java.lang.invoke.VarHandle.VarHandleDesc)2 NoSuchElementException (java.util.NoSuchElementException)2 Test (org.testng.annotations.Test)2 Kind (java.lang.constant.DirectMethodHandleDesc.Kind)1 DynamicCallSiteDesc (java.lang.constant.DynamicCallSiteDesc)1 MethodHandleDesc (java.lang.constant.MethodHandleDesc)1 CallSite (java.lang.invoke.CallSite)1 ConstantCallSite (java.lang.invoke.ConstantCallSite)1 VarHandle (java.lang.invoke.VarHandle)1