< prev index next >

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

Print this page

 41  * A <a href="package-summary.html#nominal">nominal descriptor</a> for a
 42  * {@link Class} constant.
 43  *
 44  * <p>For common system types, including all the primitive types, there are
 45  * predefined {@linkplain ClassDesc} constants in {@link ConstantDescs}.
 46  * (The {@code java.lang.constant} APIs consider {@code void} to be a primitive type.)
 47  * To create a {@linkplain ClassDesc} for a class or interface type, use {@link #of} or
 48  * {@link #ofDescriptor(String)}; to create a {@linkplain ClassDesc} for an array
 49  * type, use {@link #ofDescriptor(String)}, or first obtain a
 50  * {@linkplain ClassDesc} for the component type and then call the {@link #arrayType()}
 51  * or {@link #arrayType(int)} methods.
 52  *
 53  * @see ConstantDescs
 54  *
 55  * @since 12
 56  */
 57 public sealed interface ClassDesc
 58         extends ConstantDesc,
 59                 TypeDescriptor.OfField<ClassDesc>
 60         permits PrimitiveClassDescImpl,
 61                 ReferenceClassDescImpl {
 62 
 63     /**
 64      * Returns a {@linkplain ClassDesc} for a class or interface type,
 65      * given the name of the class or interface, such as {@code "java.lang.String"}.
 66      * (To create a descriptor for an array type, either use {@link #ofDescriptor(String)}
 67      * or {@link #arrayType()}; to create a descriptor for a primitive type, use
 68      * {@link #ofDescriptor(String)} or use the predefined constants in
 69      * {@link ConstantDescs}).
 70      *
 71      * @param name the fully qualified (dot-separated) binary class name
 72      * @return a {@linkplain ClassDesc} describing the desired class
 73      * @throws NullPointerException if the argument is {@code null}
 74      * @throws IllegalArgumentException if the name string is not in the
 75      * correct format
 76      * @see ClassDesc#ofDescriptor(String)
 77      * @see ClassDesc#ofInternalName(String)
 78      */
 79     static ClassDesc of(String name) {
 80         ConstantUtils.validateBinaryClassName(requireNonNull(name));
 81         return ClassDesc.ofDescriptor("L" + binaryToInternal(name) + ";");

154      * correct format
155      * @jvms 4.3.2 Field Descriptors
156      * @jvms 4.4.1 The CONSTANT_Class_info Structure
157      * @see ClassDesc#of(String)
158      * @see ClassDesc#ofInternalName(String)
159      */
160     static ClassDesc ofDescriptor(String descriptor) {
161         requireNonNull(descriptor);
162         if (descriptor.isEmpty()) {
163             throw new IllegalArgumentException(
164                     "not a valid reference type descriptor: " + descriptor);
165         }
166         int depth = ConstantUtils.arrayDepth(descriptor);
167         if (depth > ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
168             throw new IllegalArgumentException(
169                     "Cannot create an array type descriptor with more than " +
170                     ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
171         }
172         return (descriptor.length() == 1)
173                ? new PrimitiveClassDescImpl(descriptor)
174                : new ReferenceClassDescImpl(descriptor);
175     }
176 
177     /**
178      * Returns a {@linkplain ClassDesc} for an array type whose component type
179      * is described by this {@linkplain ClassDesc}.
180      *
181      * @return a {@linkplain ClassDesc} describing the array type
182      * @throws IllegalStateException if the resulting {@linkplain
183      * ClassDesc} would have an array rank of greater than 255
184      * @jvms 4.4.1 The CONSTANT_Class_info Structure
185      */
186     default ClassDesc arrayType() {
187         int depth = ConstantUtils.arrayDepth(descriptorString());
188         if (depth >= ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
189             throw new IllegalStateException(
190                     "Cannot create an array type descriptor with more than " +
191                     ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
192         }
193         return arrayType(1);
194     }

 41  * A <a href="package-summary.html#nominal">nominal descriptor</a> for a
 42  * {@link Class} constant.
 43  *
 44  * <p>For common system types, including all the primitive types, there are
 45  * predefined {@linkplain ClassDesc} constants in {@link ConstantDescs}.
 46  * (The {@code java.lang.constant} APIs consider {@code void} to be a primitive type.)
 47  * To create a {@linkplain ClassDesc} for a class or interface type, use {@link #of} or
 48  * {@link #ofDescriptor(String)}; to create a {@linkplain ClassDesc} for an array
 49  * type, use {@link #ofDescriptor(String)}, or first obtain a
 50  * {@linkplain ClassDesc} for the component type and then call the {@link #arrayType()}
 51  * or {@link #arrayType(int)} methods.
 52  *
 53  * @see ConstantDescs
 54  *
 55  * @since 12
 56  */
 57 public sealed interface ClassDesc
 58         extends ConstantDesc,
 59                 TypeDescriptor.OfField<ClassDesc>
 60         permits PrimitiveClassDescImpl,
 61                 ClassDescImpl {
 62 
 63     /**
 64      * Returns a {@linkplain ClassDesc} for a class or interface type,
 65      * given the name of the class or interface, such as {@code "java.lang.String"}.
 66      * (To create a descriptor for an array type, either use {@link #ofDescriptor(String)}
 67      * or {@link #arrayType()}; to create a descriptor for a primitive type, use
 68      * {@link #ofDescriptor(String)} or use the predefined constants in
 69      * {@link ConstantDescs}).
 70      *
 71      * @param name the fully qualified (dot-separated) binary class name
 72      * @return a {@linkplain ClassDesc} describing the desired class
 73      * @throws NullPointerException if the argument is {@code null}
 74      * @throws IllegalArgumentException if the name string is not in the
 75      * correct format
 76      * @see ClassDesc#ofDescriptor(String)
 77      * @see ClassDesc#ofInternalName(String)
 78      */
 79     static ClassDesc of(String name) {
 80         ConstantUtils.validateBinaryClassName(requireNonNull(name));
 81         return ClassDesc.ofDescriptor("L" + binaryToInternal(name) + ";");

154      * correct format
155      * @jvms 4.3.2 Field Descriptors
156      * @jvms 4.4.1 The CONSTANT_Class_info Structure
157      * @see ClassDesc#of(String)
158      * @see ClassDesc#ofInternalName(String)
159      */
160     static ClassDesc ofDescriptor(String descriptor) {
161         requireNonNull(descriptor);
162         if (descriptor.isEmpty()) {
163             throw new IllegalArgumentException(
164                     "not a valid reference type descriptor: " + descriptor);
165         }
166         int depth = ConstantUtils.arrayDepth(descriptor);
167         if (depth > ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
168             throw new IllegalArgumentException(
169                     "Cannot create an array type descriptor with more than " +
170                     ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
171         }
172         return (descriptor.length() == 1)
173                ? new PrimitiveClassDescImpl(descriptor)
174                : new ClassDescImpl(descriptor);
175     }
176 
177     /**
178      * Returns a {@linkplain ClassDesc} for an array type whose component type
179      * is described by this {@linkplain ClassDesc}.
180      *
181      * @return a {@linkplain ClassDesc} describing the array type
182      * @throws IllegalStateException if the resulting {@linkplain
183      * ClassDesc} would have an array rank of greater than 255
184      * @jvms 4.4.1 The CONSTANT_Class_info Structure
185      */
186     default ClassDesc arrayType() {
187         int depth = ConstantUtils.arrayDepth(descriptorString());
188         if (depth >= ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
189             throw new IllegalStateException(
190                     "Cannot create an array type descriptor with more than " +
191                     ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
192         }
193         return arrayType(1);
194     }
< prev index next >