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();
}
}
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()));
}
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));
}
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();
}
}
Aggregations