Skip to content

Commit 8d2f9e5

Browse files
liachcl4es
andcommittedJun 8, 2024
8333749: Consolidate ConstantDesc conversion in java.base
Co-authored-by: Claes Redestad <redestad@openjdk.org> Reviewed-by: redestad, jvernee
1 parent a6fc2f8 commit 8d2f9e5

File tree

13 files changed

+209
-143
lines changed

13 files changed

+209
-143
lines changed
 

‎src/java.base/share/classes/java/lang/Class.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import java.util.Set;
7171
import java.util.stream.Collectors;
7272

73+
import jdk.internal.constant.ConstantUtils;
7374
import jdk.internal.javac.PreviewFeature;
7475
import jdk.internal.loader.BootLoader;
7576
import jdk.internal.loader.BuiltinClassLoader;
@@ -4709,7 +4710,7 @@ public Class<?> arrayType() {
47094710
public Optional<ClassDesc> describeConstable() {
47104711
Class<?> c = isArray() ? elementType() : this;
47114712
return c.isHidden() ? Optional.empty()
4712-
: Optional.of(ClassDesc.ofDescriptor(descriptorString()));
4713+
: Optional.of(ConstantUtils.classDesc(this));
47134714
}
47144715

47154716
/**

‎src/java.base/share/classes/java/lang/classfile/ClassFile.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.lang.classfile.instruction.ExceptionCatch;
4646
import java.util.List;
4747
import static java.util.Objects.requireNonNull;
48+
import static jdk.internal.constant.ConstantUtils.CD_module_info;
4849
import jdk.internal.javac.PreviewFeature;
4950

5051
/**
@@ -392,7 +393,7 @@ default byte[] buildModule(ModuleAttribute moduleAttribute) {
392393
*/
393394
default byte[] buildModule(ModuleAttribute moduleAttribute,
394395
Consumer<? super ClassBuilder> handler) {
395-
return build(ClassDesc.of("module-info"), clb -> {
396+
return build(CD_module_info, clb -> {
396397
clb.withFlags(AccessFlag.MODULE);
397398
clb.with(moduleAttribute);
398399
handler.accept(clb);

‎src/java.base/share/classes/java/lang/constant/ClassDesc.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import static jdk.internal.constant.ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS;
3737
import static jdk.internal.constant.ConstantUtils.arrayDepth;
3838
import static jdk.internal.constant.ConstantUtils.binaryToInternal;
39-
import static jdk.internal.constant.ConstantUtils.dropFirstAndLastChar;
4039
import static jdk.internal.constant.ConstantUtils.internalToBinary;
4140
import static jdk.internal.constant.ConstantUtils.validateBinaryClassName;
4241
import static jdk.internal.constant.ConstantUtils.validateInternalClassName;
@@ -165,7 +164,7 @@ static ClassDesc of(String packageName, String className) {
165164
static ClassDesc ofDescriptor(String descriptor) {
166165
// implicit null-check
167166
return (descriptor.length() == 1)
168-
? Wrapper.forPrimitiveType(descriptor.charAt(0)).classDescriptor()
167+
? Wrapper.forPrimitiveType(descriptor.charAt(0)).basicClassDescriptor()
169168
// will throw IAE on descriptor.length == 0 or if array dimensions too long
170169
: ReferenceClassDescImpl.of(descriptor);
171170
}
@@ -315,7 +314,7 @@ default ClassDesc componentType() {
315314
if (isArray()) {
316315
String desc = descriptorString();
317316
if (desc.length() == 2) {
318-
return Wrapper.forBasicType(desc.charAt(1)).classDescriptor();
317+
return Wrapper.forBasicType(desc.charAt(1)).basicClassDescriptor();
319318
} else {
320319
return ReferenceClassDescImpl.ofValidated(desc.substring(1));
321320
}

‎src/java.base/share/classes/java/lang/invoke/MethodHandleProxies.java

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -52,6 +52,8 @@
5252
import java.lang.classfile.ClassFile;
5353
import java.lang.classfile.CodeBuilder;
5454
import java.lang.classfile.TypeKind;
55+
56+
import jdk.internal.constant.ConstantUtils;
5557
import jdk.internal.module.Modules;
5658
import jdk.internal.reflect.CallerSensitive;
5759
import jdk.internal.reflect.Reflection;
@@ -63,6 +65,7 @@
6365
import static java.lang.invoke.MethodType.methodType;
6466
import static java.lang.module.ModuleDescriptor.Modifier.SYNTHETIC;
6567
import static java.lang.classfile.ClassFile.*;
68+
import static jdk.internal.constant.ConstantUtils.*;
6669

6770
/**
6871
* This class consists exclusively of static methods that help adapt
@@ -249,14 +252,14 @@ private static Class<?> newProxyClass(Class<?> intfc) {
249252

250253
// the field name holding the method handle for this method
251254
String fieldName = "m" + count++;
252-
var mt = methodType(m.getReturnType(), JLRA.getExecutableSharedParameterTypes(m), true);
255+
var md = methodTypeDesc(m.getReturnType(), JLRA.getExecutableSharedParameterTypes(m));
253256
var thrown = JLRA.getExecutableSharedExceptionTypes(m);
254257
var exceptionTypeDescs =
255258
thrown.length == 0 ? DEFAULT_RETHROWS
256259
: Stream.concat(DEFAULT_RETHROWS.stream(),
257-
Arrays.stream(thrown).map(MethodHandleProxies::desc))
260+
Arrays.stream(thrown).map(ConstantUtils::referenceClassDesc))
258261
.distinct().toList();
259-
methods.add(new MethodInfo(desc(mt), exceptionTypeDescs, fieldName));
262+
methods.add(new MethodInfo(md, exceptionTypeDescs, fieldName));
260263

261264
// find the types referenced by this method
262265
addElementType(referencedTypes, m.getReturnType());
@@ -279,7 +282,8 @@ private static Class<?> newProxyClass(Class<?> intfc) {
279282
int i = intfcName.lastIndexOf('.');
280283
// jdk.MHProxy#.Interface
281284
String className = packageName + "." + (i > 0 ? intfcName.substring(i + 1) : intfcName);
282-
byte[] template = createTemplate(loader, ClassDesc.of(className), desc(intfc), uniqueName, methods);
285+
byte[] template = createTemplate(loader, binaryNameToDesc(className),
286+
referenceClassDesc(intfc), uniqueName, methods);
283287
// define the dynamic module to the class loader of the interface
284288
var definer = new Lookup(intfc).makeHiddenClassDefiner(className, template, Set.of(), DUMPER);
285289

@@ -335,17 +339,17 @@ private static Class<?> getProxyClass(Class<?> intfc) {
335339
}
336340
}
337341

338-
private static final List<ClassDesc> DEFAULT_RETHROWS = List.of(desc(RuntimeException.class), desc(Error.class));
339-
private static final ClassDesc CD_UndeclaredThrowableException = desc(UndeclaredThrowableException.class);
340-
private static final ClassDesc CD_IllegalAccessException = desc(IllegalAccessException.class);
342+
private static final List<ClassDesc> DEFAULT_RETHROWS = List.of(referenceClassDesc(RuntimeException.class), referenceClassDesc(Error.class));
343+
private static final ClassDesc CD_UndeclaredThrowableException = referenceClassDesc(UndeclaredThrowableException.class);
344+
private static final ClassDesc CD_IllegalAccessException = referenceClassDesc(IllegalAccessException.class);
341345
private static final MethodTypeDesc MTD_void_Throwable = MethodTypeDesc.of(CD_void, CD_Throwable);
342346
private static final MethodType MT_void_Lookup_MethodHandle_MethodHandle =
343347
methodType(void.class, Lookup.class, MethodHandle.class, MethodHandle.class);
344348
private static final MethodType MT_Object_Lookup_MethodHandle_MethodHandle =
345349
MT_void_Lookup_MethodHandle_MethodHandle.changeReturnType(Object.class);
346350
private static final MethodType MT_MethodHandle_Object = methodType(MethodHandle.class, Object.class);
347-
private static final MethodTypeDesc MTD_void_Lookup_MethodHandle_MethodHandle =
348-
desc(MT_void_Lookup_MethodHandle_MethodHandle);
351+
private static final MethodTypeDesc MTD_void_Lookup_MethodHandle_MethodHandle
352+
= methodTypeDesc(MT_void_Lookup_MethodHandle_MethodHandle);
349353
private static final MethodTypeDesc MTD_void_Lookup = MethodTypeDesc.of(CD_void, CD_MethodHandles_Lookup);
350354
private static final MethodTypeDesc MTD_MethodHandle_MethodType = MethodTypeDesc.of(CD_MethodHandle, CD_MethodType);
351355
private static final MethodTypeDesc MTD_Class = MethodTypeDesc.of(CD_Class);
@@ -531,16 +535,6 @@ public static Class<?> wrapperInstanceType(Object x) {
531535
}
532536
}
533537

534-
private static ClassDesc desc(Class<?> cl) {
535-
return cl.describeConstable().orElseThrow(() -> newInternalError("Cannot convert class "
536-
+ cl.getName() + " to a constant"));
537-
}
538-
539-
private static MethodTypeDesc desc(MethodType mt) {
540-
return mt.describeConstable().orElseThrow(() -> newInternalError("Cannot convert method type "
541-
+ mt + " to a constant"));
542-
}
543-
544538
private static final JavaLangReflectAccess JLRA = SharedSecrets.getJavaLangReflectAccess();
545539
private static final AtomicInteger counter = new AtomicInteger();
546540

‎src/java.base/share/classes/java/lang/invoke/StringConcatFactory.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import jdk.internal.access.JavaLangAccess;
2929
import jdk.internal.access.SharedSecrets;
30+
import jdk.internal.constant.ConstantUtils;
3031
import jdk.internal.misc.VM;
3132
import jdk.internal.util.ClassFileDumper;
3233
import jdk.internal.vm.annotation.Stable;
@@ -1090,13 +1091,13 @@ private SimpleStringBuilderStrategy() {
10901091
private static MethodHandle generate(Lookup lookup, MethodType args, String[] constants) throws Exception {
10911092
String className = getClassName(lookup.lookupClass());
10921093

1093-
byte[] classBytes = ClassFile.of().build(ClassDesc.of(className),
1094+
byte[] classBytes = ClassFile.of().build(ConstantUtils.binaryNameToDesc(className),
10941095
new Consumer<ClassBuilder>() {
10951096
@Override
10961097
public void accept(ClassBuilder clb) {
10971098
clb.withFlags(AccessFlag.FINAL, AccessFlag.SUPER, AccessFlag.SYNTHETIC)
10981099
.withMethodBody(METHOD_NAME,
1099-
MethodTypeDesc.ofDescriptor(args.toMethodDescriptorString()),
1100+
ConstantUtils.methodTypeDesc(args),
11001101
ClassFile.ACC_FINAL | ClassFile.ACC_PRIVATE | ClassFile.ACC_STATIC,
11011102
generateMethod(constants, args));
11021103
}});

‎src/java.base/share/classes/java/lang/reflect/ProxyGenerator.java

+14-25
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import java.lang.classfile.attribute.StackMapTableAttribute;
5050
import java.lang.constant.ConstantDescs;
5151
import static java.lang.constant.ConstantDescs.*;
52+
import static jdk.internal.constant.ConstantUtils.*;
53+
5254
import java.lang.constant.DirectMethodHandleDesc;
5355
import java.lang.constant.DynamicConstantDesc;
5456

@@ -134,7 +136,7 @@ final class ProxyGenerator {
134136
/**
135137
* Name of proxy class
136138
*/
137-
private ClassEntry classEntry;
139+
private final ClassEntry classEntry;
138140

139141
/**
140142
* Proxy interfaces
@@ -160,10 +162,10 @@ final class ProxyGenerator {
160162
* A ProxyGenerator object contains the state for the ongoing
161163
* generation of a particular proxy class.
162164
*/
163-
private ProxyGenerator(ClassLoader loader, String className, List<Class<?>> interfaces,
165+
private ProxyGenerator(String className, List<Class<?>> interfaces,
164166
int accessFlags) {
165167
this.cp = ConstantPoolBuilder.of();
166-
this.classEntry = cp.classEntry(ReferenceClassDescImpl.ofValidatedBinaryName(className));
168+
this.classEntry = cp.classEntry(ConstantUtils.binaryNameToDesc(className));
167169
this.interfaces = interfaces;
168170
this.accessFlags = accessFlags;
169171
this.throwableStack = List.of(StackMapFrameInfo.ObjectVerificationTypeInfo.of(cp.classEntry(CD_Throwable)));
@@ -190,7 +192,7 @@ static byte[] generateProxyClass(ClassLoader loader,
190192
List<Class<?>> interfaces,
191193
int accessFlags) {
192194
Objects.requireNonNull(interfaces);
193-
ProxyGenerator gen = new ProxyGenerator(loader, name, interfaces, accessFlags);
195+
ProxyGenerator gen = new ProxyGenerator(name, interfaces, accessFlags);
194196
final byte[] classFile = gen.generateClassFile();
195197

196198
if (SAVE_GENERATED_FILES) {
@@ -227,18 +229,10 @@ public Void run() {
227229
private static List<ClassEntry> toClassEntries(ConstantPoolBuilder cp, List<Class<?>> types) {
228230
var ces = new ArrayList<ClassEntry>(types.size());
229231
for (var t : types)
230-
ces.add(cp.classEntry(ReferenceClassDescImpl.ofValidatedBinaryName(t.getName())));
232+
ces.add(cp.classEntry(ConstantUtils.binaryNameToDesc(t.getName())));
231233
return ces;
232234
}
233235

234-
/**
235-
* {@return the {@code ClassDesc} of the given type}
236-
* @param type the {@code Class} object
237-
*/
238-
private static ClassDesc toClassDesc(Class<?> type) {
239-
return ClassDesc.ofDescriptor(type.descriptorString());
240-
}
241-
242236
/**
243237
* For a given set of proxy methods with the same signature, check
244238
* that their return types are compatible according to the Proxy
@@ -325,7 +319,7 @@ private static void checkReturnTypes(List<ProxyMethod> methods) {
325319
* not assignable from any of the others.
326320
*/
327321
if (uncoveredReturnTypes.size() > 1) {
328-
ProxyMethod pm = methods.get(0);
322+
ProxyMethod pm = methods.getFirst();
329323
throw new IllegalArgumentException(
330324
"methods with same signature " +
331325
pm.shortSignature +
@@ -501,7 +495,7 @@ private void addProxyMethod(Method m, Class<?> fromClass) {
501495

502496
String sig = m.toShortSignature();
503497
List<ProxyMethod> sigmethods = proxyMethods.computeIfAbsent(sig,
504-
(f) -> new ArrayList<>(3));
498+
_ -> new ArrayList<>(3));
505499
for (ProxyMethod pm : sigmethods) {
506500
if (returnType == pm.returnType) {
507501
/*
@@ -531,7 +525,7 @@ private void addProxyMethod(Method m, Class<?> fromClass) {
531525
private void addProxyMethod(ProxyMethod pm) {
532526
String sig = pm.shortSignature;
533527
List<ProxyMethod> sigmethods = proxyMethods.computeIfAbsent(sig,
534-
(f) -> new ArrayList<>(3));
528+
_ -> new ArrayList<>(3));
535529
sigmethods.add(pm);
536530
}
537531

@@ -637,7 +631,6 @@ private ProxyMethod(Method method, String sig, Class<?>[] parameterTypes,
637631
* Create a new specific ProxyMethod with a specific field name
638632
*
639633
* @param method The method for which to create a proxy
640-
* @param methodFieldName the fieldName to generate
641634
*/
642635
private ProxyMethod(Method method) {
643636
this(method, method.toShortSignature(),
@@ -650,11 +643,7 @@ private ProxyMethod(Method method) {
650643
*/
651644
private void generateMethod(ProxyGenerator pg, ClassBuilder clb) {
652645
var cp = pg.cp;
653-
var pTypes = new ClassDesc[parameterTypes.length];
654-
for (int i = 0; i < pTypes.length; i++) {
655-
pTypes[i] = toClassDesc(parameterTypes[i]);
656-
}
657-
MethodTypeDesc desc = MethodTypeDescImpl.ofTrusted(toClassDesc(returnType), pTypes);
646+
var desc = methodTypeDesc(returnType, parameterTypes);
658647
int accessFlags = (method.isVarArgs()) ? ACC_VARARGS | ACC_PUBLIC | ACC_FINAL
659648
: ACC_PUBLIC | ACC_FINAL;
660649
var catchList = computeUniqueCatchList(exceptionTypes);
@@ -665,7 +654,7 @@ private void generateMethod(ProxyGenerator pg, ClassBuilder clb) {
665654
.getfield(pg.handlerField)
666655
.aload(0)
667656
.ldc(DynamicConstantDesc.of(pg.bsm,
668-
toClassDesc(fromClass),
657+
referenceClassDesc(fromClass),
669658
method.getName(),
670659
desc));
671660
if (parameterTypes.length > 0) {
@@ -693,7 +682,7 @@ private void generateMethod(ProxyGenerator pg, ClassBuilder clb) {
693682
if (!catchList.isEmpty()) {
694683
var c1 = cob.newBoundLabel();
695684
for (var exc : catchList) {
696-
cob.exceptionCatch(cob.startLabel(), c1, c1, toClassDesc(exc));
685+
cob.exceptionCatch(cob.startLabel(), c1, c1, referenceClassDesc(exc));
697686
}
698687
cob.athrow(); // just rethrow the exception
699688
var c2 = cob.newBoundLabel();
@@ -739,7 +728,7 @@ private void codeUnwrapReturnValue(CodeBuilder cob, Class<?> type) {
739728
.invokevirtual(prim.unwrapMethodRef(cob.constantPool()))
740729
.return_(TypeKind.from(type).asLoadable());
741730
} else {
742-
cob.checkcast(toClassDesc(type))
731+
cob.checkcast(referenceClassDesc(type))
743732
.areturn();
744733
}
745734
}

0 commit comments

Comments
 (0)
Please sign in to comment.