< prev index next >

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

Print this page

 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) + ";");
 81     }
 82 
 83     /**
 84      * Returns a {@linkplain ClassDesc} for a class or interface type,
 85      * given the name of the class or interface in internal form,
 86      * such as {@code "java/lang/String"}.
 87      *
 88      * @apiNote

120      * not in the correct format
121      */
122     static ClassDesc of(String packageName, String className) {
123         ConstantUtils.validateBinaryClassName(requireNonNull(packageName));
124         if (packageName.isEmpty()) {
125             return of(className);
126         }
127         validateMemberName(requireNonNull(className), false);
128         return ofDescriptor("L" + binaryToInternal(packageName) +
129                 "/" + className + ";");
130     }
131 
132     /**
133      * Returns a {@linkplain ClassDesc} given a descriptor string for a class,
134      * interface, array, or primitive type.
135      *
136      * @apiNote
137      *
138      * A field type descriptor string for a non-array type is either
139      * a one-letter code corresponding to a primitive type
140      * ({@code "J", "I", "C", "S", "B", "D", "F", "Z", "V"}), or the letter {@code "L"}, followed

141      * by the fully qualified binary name of a class, followed by {@code ";"}.
142      * A field type descriptor for an array type is the character {@code "["}
143      * followed by the field descriptor for the component type.  Examples of
144      * valid type descriptor strings include {@code "Ljava/lang/String;"}, {@code "I"},
145      * {@code "[I"}, {@code "V"}, {@code "[Ljava/lang/String;"}, etc.

146      * See JVMS {@jvms 4.3.2 }("Field Descriptors") for more detail.
147      *
148      * @param descriptor a field descriptor string
149      * @return a {@linkplain ClassDesc} describing the desired class
150      * @throws NullPointerException if the argument is {@code null}
151      * @throws IllegalArgumentException if the name string is not in the
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}; to create a descriptor for a primitive value type,
 69      * use {@link #ofDescriptor(String)}).
 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) + ";");
 82     }
 83 
 84     /**
 85      * Returns a {@linkplain ClassDesc} for a class or interface type,
 86      * given the name of the class or interface in internal form,
 87      * such as {@code "java/lang/String"}.
 88      *
 89      * @apiNote

121      * not in the correct format
122      */
123     static ClassDesc of(String packageName, String className) {
124         ConstantUtils.validateBinaryClassName(requireNonNull(packageName));
125         if (packageName.isEmpty()) {
126             return of(className);
127         }
128         validateMemberName(requireNonNull(className), false);
129         return ofDescriptor("L" + binaryToInternal(packageName) +
130                 "/" + className + ";");
131     }
132 
133     /**
134      * Returns a {@linkplain ClassDesc} given a descriptor string for a class,
135      * interface, array, or primitive type.
136      *
137      * @apiNote
138      *
139      * A field type descriptor string for a non-array type is either
140      * a one-letter code corresponding to a primitive type
141      * ({@code "J", "I", "C", "S", "B", "D", "F", "Z", "V"}),
142      * or the letter {@code "L"} or {@code "Q"} followed
143      * by the fully qualified binary name of a class, followed by {@code ";"}.
144      * A field type descriptor for an array type is the character {@code "["}
145      * followed by the field descriptor for the component type.  Examples of
146      * valid type descriptor strings include {@code "Ljava/lang/String;"},
147      * {@code "QPoint;}, {@code "I"}, {@code "[I"}, {@code "V"},
148      * {@code "[Ljava/lang/String;"}, {@code "[LPoint;"}, {@code "[[QPoint;} etc.
149      * See JVMS {@jvms 4.3.2 }("Field Descriptors") for more detail.
150      *
151      * @param descriptor a field descriptor string
152      * @return a {@linkplain ClassDesc} describing the desired class
153      * @throws NullPointerException if the argument is {@code null}
154      * @throws IllegalArgumentException if the name string is not in the
155      * correct format
156      * @jvms 4.3.2 Field Descriptors
157      * @jvms 4.4.1 The CONSTANT_Class_info Structure
158      * @see ClassDesc#of(String)
159      * @see ClassDesc#ofInternalName(String)
160      */
161     static ClassDesc ofDescriptor(String descriptor) {
162         requireNonNull(descriptor);
163         if (descriptor.isEmpty()) {
164             throw new IllegalArgumentException(
165                     "not a valid reference type descriptor: " + descriptor);
166         }
167         int depth = ConstantUtils.arrayDepth(descriptor);
168         if (depth > ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
169             throw new IllegalArgumentException(
170                     "Cannot create an array type descriptor with more than " +
171                     ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
172         }
173         return (descriptor.length() == 1)
174                ? new PrimitiveClassDescImpl(descriptor)
175                : new ClassDescImpl(descriptor);
176     }
177 
178     /**
179      * Returns a {@linkplain ClassDesc} for an array type whose component type
180      * is described by this {@linkplain ClassDesc}.
181      *
182      * @return a {@linkplain ClassDesc} describing the array type
183      * @throws IllegalStateException if the resulting {@linkplain
184      * ClassDesc} would have an array rank of greater than 255
185      * @jvms 4.4.1 The CONSTANT_Class_info Structure
186      */
187     default ClassDesc arrayType() {
188         int depth = ConstantUtils.arrayDepth(descriptorString());
189         if (depth >= ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
190             throw new IllegalStateException(
191                     "Cannot create an array type descriptor with more than " +
192                     ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
193         }
194         return arrayType(1);
195     }

281      */
282     default boolean isArray() {
283         return descriptorString().startsWith("[");
284     }
285 
286     /**
287      * Returns whether this {@linkplain ClassDesc} describes a primitive type.
288      *
289      * @return whether this {@linkplain ClassDesc} describes a primitive type
290      */
291     default boolean isPrimitive() {
292         return descriptorString().length() == 1;
293     }
294 
295     /**
296      * Returns whether this {@linkplain ClassDesc} describes a class or interface type.
297      *
298      * @return whether this {@linkplain ClassDesc} describes a class or interface type
299      */
300     default boolean isClassOrInterface() {
301         return descriptorString().startsWith("L") || descriptorString().startsWith("Q");
302     }
303 
304     /**
305      * Returns whether this {@linkplain ClassDesc} describes a
306      * {@linkplain Class#isPrimitiveValueType() primitive value type}.
307      *
308      * @return whether this {@linkplain ClassDesc} describes a primitive value type.
309      * @since Valhalla
310      */
311     default boolean isPrimitiveValueType() {
312         return descriptorString().startsWith("Q");
313     }
314 
315     /**
316      * Returns the component type of this {@linkplain ClassDesc}, if it describes
317      * an array type, or {@code null} otherwise.
318      *
319      * @return a {@linkplain ClassDesc} describing the component type, or {@code null}
320      * if this descriptor does not describe an array type
321      */
322     default ClassDesc componentType() {
323         return isArray() ? ClassDesc.ofDescriptor(descriptorString().substring(1)) : null;
324     }
325 
326     /**
327      * Returns the package name of this {@linkplain ClassDesc}, if it describes
328      * a class or interface type.
329      *
330      * @return the package name, or the empty string if the class is in the
331      * default package, or this {@linkplain ClassDesc} does not describe a class or interface type
332      */
< prev index next >