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