Search in sources :

Example 1 with MethodTypeDesc

use of java.lang.constant.MethodTypeDesc 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 MethodTypeDesc

use of java.lang.constant.MethodTypeDesc 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 3 with MethodTypeDesc

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

the class Test_MethodType method describeConstableTestGeneral.

private void describeConstableTestGeneral(String testName, MethodType type) throws Throwable {
    Optional<MethodTypeDesc> descOp = type.describeConstable();
    MethodTypeDesc desc = descOp.orElseThrow();
    /* verify that descriptor can be resolved. Otherwise a ReflectiveOperationException will be thrown */
    MethodType resolveType = (MethodType) desc.resolveConstantDesc(MethodHandles.lookup());
    logger.debug(testName + ": Descriptor of original type is: " + type.descriptorString() + " descriptor of descriptor is: " + resolveType.descriptorString());
    Assert.assertTrue(type.equals(resolveType));
}
Also used : MethodType(java.lang.invoke.MethodType) MethodTypeDesc(java.lang.constant.MethodTypeDesc)

Example 4 with MethodTypeDesc

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

the class StructuralComparator method describeConstable.

/*[IF JAVA_SPEC_VERSION >= 12]*/
/**
 * Returns the nominal descriptor of this MethodHandle instance, or an empty Optional
 * if construction is not possible.
 *
 * @return Optional with a nominal descriptor of MethodHandle instance
 */
public Optional<MethodHandleDesc> describeConstable() {
    try {
        DirectMethodHandleDesc.Kind handleKind;
        /* Define handle as a crackable type. If its not possible to do so this method should fail */
        MethodHandleInfo handleInfo = Lookup.IMPL_LOOKUP.revealDirect(this);
        switch(handleInfo.getReferenceKind()) {
            case MethodHandleInfo.REF_getField:
                handleKind = DirectMethodHandleDesc.Kind.GETTER;
                break;
            case MethodHandleInfo.REF_getStatic:
                handleKind = DirectMethodHandleDesc.Kind.STATIC_GETTER;
                break;
            case MethodHandleInfo.REF_putField:
                handleKind = DirectMethodHandleDesc.Kind.SETTER;
                break;
            case MethodHandleInfo.REF_putStatic:
                handleKind = DirectMethodHandleDesc.Kind.STATIC_SETTER;
                break;
            case MethodHandleInfo.REF_invokeVirtual:
                handleKind = DirectMethodHandleDesc.Kind.VIRTUAL;
                break;
            case MethodHandleInfo.REF_invokeStatic:
                if (handleInfo.getDeclaringClass().isInterface()) {
                    handleKind = DirectMethodHandleDesc.Kind.INTERFACE_STATIC;
                } else {
                    handleKind = DirectMethodHandleDesc.Kind.STATIC;
                }
                break;
            case MethodHandleInfo.REF_invokeSpecial:
                if (handleInfo.getDeclaringClass().isInterface()) {
                    handleKind = DirectMethodHandleDesc.Kind.INTERFACE_SPECIAL;
                } else {
                    handleKind = DirectMethodHandleDesc.Kind.SPECIAL;
                }
                break;
            case MethodHandleInfo.REF_newInvokeSpecial:
                handleKind = DirectMethodHandleDesc.Kind.CONSTRUCTOR;
                break;
            case MethodHandleInfo.REF_invokeInterface:
                handleKind = DirectMethodHandleDesc.Kind.INTERFACE_VIRTUAL;
                break;
            default:
                /* fail */
                return Optional.empty();
        }
        /* create descriptor for the declaring class */
        ClassDesc declaringDesc = handleInfo.getDeclaringClass().describeConstable().orElseThrow();
        /* create handle descriptor */
        MethodHandleDesc handleDesc;
        switch(handleInfo.getReferenceKind()) {
            /* field types */
            case MethodHandleInfo.REF_getField:
            case MethodHandleInfo.REF_getStatic:
            case MethodHandleInfo.REF_putField:
            case MethodHandleInfo.REF_putStatic:
                ClassDesc fieldType = ((FieldHandle) this).fieldClass.describeConstable().orElseThrow();
                handleDesc = MethodHandleDesc.ofField(handleKind, declaringDesc, handleInfo.getName(), fieldType);
                break;
            /* method types */
            case MethodHandleInfo.REF_invokeVirtual:
            case MethodHandleInfo.REF_invokeStatic:
            case MethodHandleInfo.REF_invokeSpecial:
            case MethodHandleInfo.REF_invokeInterface:
                MethodTypeDesc lookupType = handleInfo.getMethodType().describeConstable().orElseThrow();
                handleDesc = MethodHandleDesc.ofMethod(handleKind, declaringDesc, handleInfo.getName(), lookupType);
                break;
            /* constructor types */
            case MethodHandleInfo.REF_newInvokeSpecial:
                /* create list of descriptors for constructor parameters */
                Class<?>[] handleParams = handleInfo.getMethodType().parameterArray();
                int paramCount = handleParams.length;
                ClassDesc[] paramDescs = new ClassDesc[paramCount];
                for (int i = 0; i < paramCount; i++) {
                    paramDescs[i] = handleParams[i].describeConstable().orElseThrow();
                }
                handleDesc = MethodHandleDesc.ofConstructor(declaringDesc, paramDescs);
                break;
            default:
                /* fail */
                return Optional.empty();
        }
        return Optional.ofNullable(handleDesc);
    } catch (IllegalArgumentException | SecurityException | NoSuchElementException e) {
        /* fail */
        return Optional.empty();
    }
}
Also used : Kind(java.lang.constant.DirectMethodHandleDesc.Kind) MethodTypeDesc(java.lang.constant.MethodTypeDesc) DirectMethodHandleDesc(java.lang.constant.DirectMethodHandleDesc) MethodHandleDesc(java.lang.constant.MethodHandleDesc) DirectMethodHandleDesc(java.lang.constant.DirectMethodHandleDesc) NoSuchElementException(java.util.NoSuchElementException) ClassDesc(java.lang.constant.ClassDesc)

Aggregations

MethodTypeDesc (java.lang.constant.MethodTypeDesc)4 ClassDesc (java.lang.constant.ClassDesc)3 DirectMethodHandleDesc (java.lang.constant.DirectMethodHandleDesc)2 NoSuchElementException (java.util.NoSuchElementException)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 MethodType (java.lang.invoke.MethodType)1 Test (org.testng.annotations.Test)1