40 * A <a href="package-summary.html#nominal">nominal descriptor</a> for a
41 * {@link Class} constant.
42 *
43 * <p>For common system types, including all the primitive types, there are
44 * predefined {@linkplain ClassDesc} constants in {@link ConstantDescs}.
45 * (The {@code java.lang.constant} APIs consider {@code void} to be a primitive type.)
46 * To create a {@linkplain ClassDesc} for a class or interface type, use {@link #of} or
47 * {@link #ofDescriptor(String)}; to create a {@linkplain ClassDesc} for an array
48 * type, use {@link #ofDescriptor(String)}, or first obtain a
49 * {@linkplain ClassDesc} for the component type and then call the {@link #arrayType()}
50 * or {@link #arrayType(int)} methods.
51 *
52 * @see ConstantDescs
53 *
54 * @since 12
55 */
56 public sealed interface ClassDesc
57 extends ConstantDesc,
58 TypeDescriptor.OfField<ClassDesc>
59 permits PrimitiveClassDescImpl,
60 ReferenceClassDescImpl {
61
62 /**
63 * Returns a {@linkplain ClassDesc} for a class or interface type,
64 * given the name of the class or interface, such as {@code "java.lang.String"}.
65 * (To create a descriptor for an array type, either use {@link #ofDescriptor(String)}
66 * or {@link #arrayType()}; to create a descriptor for a primitive type, use
67 * {@link #ofDescriptor(String)} or use the predefined constants in
68 * {@link ConstantDescs}).
69 *
70 * @param name the fully qualified (dot-separated) binary class name
71 * @return a {@linkplain ClassDesc} describing the desired class
72 * @throws NullPointerException if the argument is {@code null}
73 * @throws IllegalArgumentException if the name string is not in the
74 * correct format
75 * @see ClassDesc#ofDescriptor(String)
76 * @see ClassDesc#ofInternalName(String)
77 */
78 static ClassDesc of(String name) {
79 ConstantUtils.validateBinaryClassName(requireNonNull(name));
80 return ClassDesc.ofDescriptor("L" + binaryToInternal(name) + ";");
152 * correct format
153 * @jvms 4.3.2 Field Descriptors
154 * @jvms 4.4.1 The CONSTANT_Class_info Structure
155 * @see ClassDesc#of(String)
156 * @see ClassDesc#ofInternalName(String)
157 */
158 static ClassDesc ofDescriptor(String descriptor) {
159 requireNonNull(descriptor);
160 if (descriptor.isEmpty()) {
161 throw new IllegalArgumentException(
162 "not a valid reference type descriptor: " + descriptor);
163 }
164 int depth = ConstantUtils.arrayDepth(descriptor);
165 if (depth > ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
166 throw new IllegalArgumentException(
167 "Cannot create an array type descriptor with more than " +
168 ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
169 }
170 return (descriptor.length() == 1)
171 ? new PrimitiveClassDescImpl(descriptor)
172 : new ReferenceClassDescImpl(descriptor);
173 }
174
175 /**
176 * Returns a {@linkplain ClassDesc} for an array type whose component type
177 * is described by this {@linkplain ClassDesc}.
178 *
179 * @return a {@linkplain ClassDesc} describing the array type
180 * @throws IllegalStateException if the resulting {@linkplain
181 * ClassDesc} would have an array rank of greater than 255
182 * @jvms 4.4.1 The CONSTANT_Class_info Structure
183 */
184 default ClassDesc arrayType() {
185 int depth = ConstantUtils.arrayDepth(descriptorString());
186 if (depth >= ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
187 throw new IllegalStateException(
188 "Cannot create an array type descriptor with more than " +
189 ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
190 }
191 return arrayType(1);
192 }
278 */
279 default boolean isArray() {
280 return descriptorString().startsWith("[");
281 }
282
283 /**
284 * Returns whether this {@linkplain ClassDesc} describes a primitive type.
285 *
286 * @return whether this {@linkplain ClassDesc} describes a primitive type
287 */
288 default boolean isPrimitive() {
289 return descriptorString().length() == 1;
290 }
291
292 /**
293 * Returns whether this {@linkplain ClassDesc} describes a class or interface type.
294 *
295 * @return whether this {@linkplain ClassDesc} describes a class or interface type
296 */
297 default boolean isClassOrInterface() {
298 return descriptorString().startsWith("L");
299 }
300
301 /**
302 * Returns the component type of this {@linkplain ClassDesc}, if it describes
303 * an array type, or {@code null} otherwise.
304 *
305 * @return a {@linkplain ClassDesc} describing the component type, or {@code null}
306 * if this descriptor does not describe an array type
307 */
308 default ClassDesc componentType() {
309 return isArray() ? ClassDesc.ofDescriptor(descriptorString().substring(1)) : null;
310 }
311
312 /**
313 * Returns the package name of this {@linkplain ClassDesc}, if it describes
314 * a class or interface type.
315 *
316 * @return the package name, or the empty string if the class is in the
317 * default package, or this {@linkplain ClassDesc} does not describe a class or interface type
318 */
|
40 * A <a href="package-summary.html#nominal">nominal descriptor</a> for a
41 * {@link Class} constant.
42 *
43 * <p>For common system types, including all the primitive types, there are
44 * predefined {@linkplain ClassDesc} constants in {@link ConstantDescs}.
45 * (The {@code java.lang.constant} APIs consider {@code void} to be a primitive type.)
46 * To create a {@linkplain ClassDesc} for a class or interface type, use {@link #of} or
47 * {@link #ofDescriptor(String)}; to create a {@linkplain ClassDesc} for an array
48 * type, use {@link #ofDescriptor(String)}, or first obtain a
49 * {@linkplain ClassDesc} for the component type and then call the {@link #arrayType()}
50 * or {@link #arrayType(int)} methods.
51 *
52 * @see ConstantDescs
53 *
54 * @since 12
55 */
56 public sealed interface ClassDesc
57 extends ConstantDesc,
58 TypeDescriptor.OfField<ClassDesc>
59 permits PrimitiveClassDescImpl,
60 ClassDescImpl {
61
62 /**
63 * Returns a {@linkplain ClassDesc} for a class or interface type,
64 * given the name of the class or interface, such as {@code "java.lang.String"}.
65 * (To create a descriptor for an array type, either use {@link #ofDescriptor(String)}
66 * or {@link #arrayType()}; to create a descriptor for a primitive type, use
67 * {@link #ofDescriptor(String)} or use the predefined constants in
68 * {@link ConstantDescs}).
69 *
70 * @param name the fully qualified (dot-separated) binary class name
71 * @return a {@linkplain ClassDesc} describing the desired class
72 * @throws NullPointerException if the argument is {@code null}
73 * @throws IllegalArgumentException if the name string is not in the
74 * correct format
75 * @see ClassDesc#ofDescriptor(String)
76 * @see ClassDesc#ofInternalName(String)
77 */
78 static ClassDesc of(String name) {
79 ConstantUtils.validateBinaryClassName(requireNonNull(name));
80 return ClassDesc.ofDescriptor("L" + binaryToInternal(name) + ";");
152 * correct format
153 * @jvms 4.3.2 Field Descriptors
154 * @jvms 4.4.1 The CONSTANT_Class_info Structure
155 * @see ClassDesc#of(String)
156 * @see ClassDesc#ofInternalName(String)
157 */
158 static ClassDesc ofDescriptor(String descriptor) {
159 requireNonNull(descriptor);
160 if (descriptor.isEmpty()) {
161 throw new IllegalArgumentException(
162 "not a valid reference type descriptor: " + descriptor);
163 }
164 int depth = ConstantUtils.arrayDepth(descriptor);
165 if (depth > ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
166 throw new IllegalArgumentException(
167 "Cannot create an array type descriptor with more than " +
168 ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
169 }
170 return (descriptor.length() == 1)
171 ? new PrimitiveClassDescImpl(descriptor)
172 : new ClassDescImpl(descriptor);
173 }
174
175 /**
176 * Returns a {@linkplain ClassDesc} for an array type whose component type
177 * is described by this {@linkplain ClassDesc}.
178 *
179 * @return a {@linkplain ClassDesc} describing the array type
180 * @throws IllegalStateException if the resulting {@linkplain
181 * ClassDesc} would have an array rank of greater than 255
182 * @jvms 4.4.1 The CONSTANT_Class_info Structure
183 */
184 default ClassDesc arrayType() {
185 int depth = ConstantUtils.arrayDepth(descriptorString());
186 if (depth >= ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
187 throw new IllegalStateException(
188 "Cannot create an array type descriptor with more than " +
189 ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
190 }
191 return arrayType(1);
192 }
278 */
279 default boolean isArray() {
280 return descriptorString().startsWith("[");
281 }
282
283 /**
284 * Returns whether this {@linkplain ClassDesc} describes a primitive type.
285 *
286 * @return whether this {@linkplain ClassDesc} describes a primitive type
287 */
288 default boolean isPrimitive() {
289 return descriptorString().length() == 1;
290 }
291
292 /**
293 * Returns whether this {@linkplain ClassDesc} describes a class or interface type.
294 *
295 * @return whether this {@linkplain ClassDesc} describes a class or interface type
296 */
297 default boolean isClassOrInterface() {
298 return descriptorString().startsWith("L") || descriptorString().startsWith("Q");
299 }
300
301 /**
302 * Returns the component type of this {@linkplain ClassDesc}, if it describes
303 * an array type, or {@code null} otherwise.
304 *
305 * @return a {@linkplain ClassDesc} describing the component type, or {@code null}
306 * if this descriptor does not describe an array type
307 */
308 default ClassDesc componentType() {
309 return isArray() ? ClassDesc.ofDescriptor(descriptorString().substring(1)) : null;
310 }
311
312 /**
313 * Returns the package name of this {@linkplain ClassDesc}, if it describes
314 * a class or interface type.
315 *
316 * @return the package name, or the empty string if the class is in the
317 * default package, or this {@linkplain ClassDesc} does not describe a class or interface type
318 */
|