8333749: Consolidate ConstantDesc conversion in java.base

Co-authored-by: Claes Redestad <redestad@openjdk.org>
Reviewed-by: redestad, jvernee
This commit is contained in:
Chen Liang 2024-06-08 13:05:36 +00:00 committed by Claes Redestad
parent a6fc2f839a
commit 8d2f9e57c3
13 changed files with 208 additions and 142 deletions

View file

@ -26,7 +26,6 @@
package java.lang.runtime;
import java.lang.Enum.EnumDesc;
import java.lang.classfile.ClassBuilder;
import java.lang.classfile.CodeBuilder;
import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDesc;
@ -50,6 +49,7 @@ import java.lang.classfile.ClassFile;
import java.lang.classfile.Label;
import java.lang.classfile.instruction.SwitchCase;
import jdk.internal.constant.ConstantUtils;
import jdk.internal.constant.ReferenceClassDescImpl;
import jdk.internal.misc.PreviewFeatures;
import jdk.internal.vm.annotation.Stable;
@ -59,6 +59,9 @@ import static java.lang.invoke.MethodHandles.Lookup.ClassOption.STRONG;
import java.util.HashMap;
import java.util.Map;
import static java.util.Objects.requireNonNull;
import static jdk.internal.constant.ConstantUtils.classDesc;
import static jdk.internal.constant.ConstantUtils.referenceClassDesc;
import sun.invoke.util.Wrapper;
/**
@ -321,7 +324,7 @@ public class SwitchBootstraps {
}
return label;
} else if (labelClass == String.class) {
return EnumDesc.of(enumClassTemplate.describeConstable().orElseThrow(), (String) label);
return EnumDesc.of(referenceClassDesc(enumClassTemplate), (String) label);
} else {
throw new IllegalArgumentException("label with illegal type found: " + labelClass +
", expected label of type either String or Class");
@ -464,10 +467,7 @@ public class SwitchBootstraps {
// Object o = ...
// o instanceof Wrapped(float)
cb.aload(SELECTOR_OBJ);
cb.instanceOf(Wrapper.forBasicType(classLabel)
.wrapperType()
.describeConstable()
.orElseThrow());
cb.instanceOf(Wrapper.forBasicType(classLabel).wrapperClassDescriptor());
cb.ifeq(next);
} else if (!unconditionalExactnessCheck(Wrapper.asPrimitiveType(selectorType), classLabel)) {
// Integer i = ... or int i = ...
@ -515,9 +515,9 @@ public class SwitchBootstraps {
TypePairs typePair = TypePairs.of(Wrapper.asPrimitiveType(selectorType), classLabel);
String methodName = TypePairs.typePairToName.get(typePair);
cb.invokestatic(ExactConversionsSupport.class.describeConstable().orElseThrow(),
cb.invokestatic(referenceClassDesc(ExactConversionsSupport.class),
methodName,
MethodTypeDesc.of(ConstantDescs.CD_boolean, typePair.from.describeConstable().orElseThrow()));
MethodTypeDesc.of(ConstantDescs.CD_boolean, classDesc(typePair.from)));
cb.ifeq(next);
}
} else {
@ -553,7 +553,7 @@ public class SwitchBootstraps {
MethodTypeDesc.of(ConstantDescs.CD_Integer,
ConstantDescs.CD_int));
cb.aload(SELECTOR_OBJ);
cb.invokeinterface(BiPredicate.class.describeConstable().orElseThrow(),
cb.invokeinterface(referenceClassDesc(BiPredicate.class),
"test",
MethodTypeDesc.of(ConstantDescs.CD_boolean,
ConstantDescs.CD_Object,
@ -601,10 +601,11 @@ public class SwitchBootstraps {
} else {
cb.loadConstant((ConstantDesc) element.caseLabel());
}
cb.invokestatic(element.caseLabel().getClass().describeConstable().orElseThrow(),
var caseLabelWrapper = Wrapper.forWrapperType(element.caseLabel().getClass());
cb.invokestatic(caseLabelWrapper.wrapperClassDescriptor(),
"valueOf",
MethodTypeDesc.of(element.caseLabel().getClass().describeConstable().orElseThrow(),
Wrapper.asPrimitiveType(element.caseLabel().getClass()).describeConstable().orElseThrow()));
MethodTypeDesc.of(caseLabelWrapper.wrapperClassDescriptor(),
caseLabelWrapper.basicClassDescriptor()));
cb.aload(SELECTOR_OBJ);
cb.invokevirtual(ConstantDescs.CD_Object,
"equals",
@ -631,7 +632,7 @@ public class SwitchBootstraps {
List<EnumDesc<?>> enumDescs = new ArrayList<>();
List<Class<?>> extraClassLabels = new ArrayList<>();
byte[] classBytes = ClassFile.of().build(ReferenceClassDescImpl.ofValidatedBinaryName(typeSwitchClassName(caller.lookupClass())),
byte[] classBytes = ClassFile.of().build(ConstantUtils.binaryNameToDesc(typeSwitchClassName(caller.lookupClass())),
clb -> {
clb.withFlags(AccessFlag.FINAL, AccessFlag.SUPER, AccessFlag.SYNTHETIC)
.withMethodBody("typeSwitch",