47
48 private static final Set<String> pointyNames = Set.of(ConstantDescs.INIT_NAME, ConstantDescs.CLASS_INIT_NAME);
49
50 /** No instantiation */
51 private ConstantUtils() {}
52
53 // Note:
54 // Non-JDK users should create their own utilities that wrap
55 // {@code .describeConstable().orElseThrow()} calls;
56 // these xxDesc methods has undefined and unsafe exceptional
57 // behavior, so they are not suitable as public APIs.
58
59 /**
60 * Creates a {@linkplain ClassDesc} from a pre-validated binary name
61 * for a class or interface type. Validated version of {@link
62 * ClassDesc#of(String)}.
63 *
64 * @param binaryName a binary name
65 */
66 public static ClassDesc binaryNameToDesc(String binaryName) {
67 return ReferenceClassDescImpl.ofValidated("L" + binaryToInternal(binaryName) + ";");
68 }
69
70 /**
71 * Creates a ClassDesc from a Class object, requires that this class
72 * can always be described nominally, i.e. this class is not a
73 * hidden class or interface or an array with a hidden component
74 * type.
75 */
76 public static ClassDesc classDesc(Class<?> type) {
77 if (type.isPrimitive()) {
78 return Wrapper.forPrimitiveType(type).basicClassDescriptor();
79 }
80 return referenceClassDesc(type);
81 }
82
83 /**
84 * Creates a ClassDesc from a Class object representing a non-hidden
85 * class or interface or an array type with a non-hidden component type.
86 */
87 public static ClassDesc referenceClassDesc(Class<?> type) {
88 return ReferenceClassDescImpl.ofValidated(type.descriptorString());
89 }
90
91 /**
92 * Creates a MethodTypeDesc from a MethodType object, requires that
93 * the type can be described nominally, i.e. all of its return
94 * type and parameter types can be described nominally.
95 */
96 public static MethodTypeDesc methodTypeDesc(MethodType type) {
97 var returnDesc = classDesc(type.returnType());
98 if (type.parameterCount() == 0) {
99 return MethodTypeDescImpl.ofValidated(returnDesc, EMPTY_CLASSDESC);
100 }
101 var paramDescs = new ClassDesc[type.parameterCount()];
102 for (int i = 0; i < type.parameterCount(); i++) {
103 paramDescs[i] = classDesc(type.parameterType(i));
104 }
105 return MethodTypeDescImpl.ofValidated(returnDesc, paramDescs);
106 }
107
108 /**
292 throw new IllegalArgumentException("Bad method descriptor: " + descriptor);
293 ptypes.add(resolveClassDesc(descriptor, cur, len));
294 cur += len;
295 }
296 if (cur >= end)
297 throw new IllegalArgumentException("Bad method descriptor: " + descriptor);
298 ++cur; // skip ')'
299
300 int rLen = skipOverFieldSignature(descriptor, cur, end, true);
301 if (rLen == 0 || cur + rLen != end)
302 throw new IllegalArgumentException("Bad method descriptor: " + descriptor);
303 ptypes.set(0, resolveClassDesc(descriptor, cur, rLen));
304 return ptypes;
305 }
306
307 private static ClassDesc resolveClassDesc(String descriptor, int start, int len) {
308 if (len == 1) {
309 return Wrapper.forPrimitiveType(descriptor.charAt(start)).basicClassDescriptor();
310 }
311 // Pre-verified in parseMethodDescriptor; avoid redundant verification
312 return ReferenceClassDescImpl.ofValidated(descriptor.substring(start, start + len));
313 }
314
315 private static final char JVM_SIGNATURE_ARRAY = '[';
316 private static final char JVM_SIGNATURE_BYTE = 'B';
317 private static final char JVM_SIGNATURE_CHAR = 'C';
318 private static final char JVM_SIGNATURE_CLASS = 'L';
319 private static final char JVM_SIGNATURE_ENDCLASS = ';';
320 private static final char JVM_SIGNATURE_ENUM = 'E';
321 private static final char JVM_SIGNATURE_FLOAT = 'F';
322 private static final char JVM_SIGNATURE_DOUBLE = 'D';
323 private static final char JVM_SIGNATURE_FUNC = '(';
324 private static final char JVM_SIGNATURE_ENDFUNC = ')';
325 private static final char JVM_SIGNATURE_INT = 'I';
326 private static final char JVM_SIGNATURE_LONG = 'J';
327 private static final char JVM_SIGNATURE_SHORT = 'S';
328 private static final char JVM_SIGNATURE_VOID = 'V';
329 private static final char JVM_SIGNATURE_BOOLEAN = 'Z';
330
331 /**
332 * Validates that the characters at [start, end) within the provided string
|
47
48 private static final Set<String> pointyNames = Set.of(ConstantDescs.INIT_NAME, ConstantDescs.CLASS_INIT_NAME);
49
50 /** No instantiation */
51 private ConstantUtils() {}
52
53 // Note:
54 // Non-JDK users should create their own utilities that wrap
55 // {@code .describeConstable().orElseThrow()} calls;
56 // these xxDesc methods has undefined and unsafe exceptional
57 // behavior, so they are not suitable as public APIs.
58
59 /**
60 * Creates a {@linkplain ClassDesc} from a pre-validated binary name
61 * for a class or interface type. Validated version of {@link
62 * ClassDesc#of(String)}.
63 *
64 * @param binaryName a binary name
65 */
66 public static ClassDesc binaryNameToDesc(String binaryName) {
67 return ClassDescImpl.ofValidated("L" + binaryToInternal(binaryName) + ";");
68 }
69
70 /**
71 * Creates a ClassDesc from a Class object, requires that this class
72 * can always be described nominally, i.e. this class is not a
73 * hidden class or interface or an array with a hidden component
74 * type.
75 */
76 public static ClassDesc classDesc(Class<?> type) {
77 if (type.isPrimitive()) {
78 return Wrapper.forPrimitiveType(type).basicClassDescriptor();
79 }
80 return referenceClassDesc(type);
81 }
82
83 /**
84 * Creates a ClassDesc from a Class object representing a non-hidden
85 * class or interface or an array type with a non-hidden component type.
86 */
87 public static ClassDesc referenceClassDesc(Class<?> type) {
88 return ClassDescImpl.ofValidated(type.descriptorString());
89 }
90
91 /**
92 * Creates a MethodTypeDesc from a MethodType object, requires that
93 * the type can be described nominally, i.e. all of its return
94 * type and parameter types can be described nominally.
95 */
96 public static MethodTypeDesc methodTypeDesc(MethodType type) {
97 var returnDesc = classDesc(type.returnType());
98 if (type.parameterCount() == 0) {
99 return MethodTypeDescImpl.ofValidated(returnDesc, EMPTY_CLASSDESC);
100 }
101 var paramDescs = new ClassDesc[type.parameterCount()];
102 for (int i = 0; i < type.parameterCount(); i++) {
103 paramDescs[i] = classDesc(type.parameterType(i));
104 }
105 return MethodTypeDescImpl.ofValidated(returnDesc, paramDescs);
106 }
107
108 /**
292 throw new IllegalArgumentException("Bad method descriptor: " + descriptor);
293 ptypes.add(resolveClassDesc(descriptor, cur, len));
294 cur += len;
295 }
296 if (cur >= end)
297 throw new IllegalArgumentException("Bad method descriptor: " + descriptor);
298 ++cur; // skip ')'
299
300 int rLen = skipOverFieldSignature(descriptor, cur, end, true);
301 if (rLen == 0 || cur + rLen != end)
302 throw new IllegalArgumentException("Bad method descriptor: " + descriptor);
303 ptypes.set(0, resolveClassDesc(descriptor, cur, rLen));
304 return ptypes;
305 }
306
307 private static ClassDesc resolveClassDesc(String descriptor, int start, int len) {
308 if (len == 1) {
309 return Wrapper.forPrimitiveType(descriptor.charAt(start)).basicClassDescriptor();
310 }
311 // Pre-verified in parseMethodDescriptor; avoid redundant verification
312 return ClassDescImpl.ofValidated(descriptor.substring(start, start + len));
313 }
314
315 private static final char JVM_SIGNATURE_ARRAY = '[';
316 private static final char JVM_SIGNATURE_BYTE = 'B';
317 private static final char JVM_SIGNATURE_CHAR = 'C';
318 private static final char JVM_SIGNATURE_CLASS = 'L';
319 private static final char JVM_SIGNATURE_ENDCLASS = ';';
320 private static final char JVM_SIGNATURE_ENUM = 'E';
321 private static final char JVM_SIGNATURE_FLOAT = 'F';
322 private static final char JVM_SIGNATURE_DOUBLE = 'D';
323 private static final char JVM_SIGNATURE_FUNC = '(';
324 private static final char JVM_SIGNATURE_ENDFUNC = ')';
325 private static final char JVM_SIGNATURE_INT = 'I';
326 private static final char JVM_SIGNATURE_LONG = 'J';
327 private static final char JVM_SIGNATURE_SHORT = 'S';
328 private static final char JVM_SIGNATURE_VOID = 'V';
329 private static final char JVM_SIGNATURE_BOOLEAN = 'Z';
330
331 /**
332 * Validates that the characters at [start, end) within the provided string
|