< 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      */
 76     static ClassDesc of(String name) {
 77         ConstantUtils.validateBinaryClassName(requireNonNull(name));
 78         return ClassDesc.ofDescriptor("L" + binaryToInternal(name) + ";");
 79     }
 80 
 81     /**
 82      * Returns a {@linkplain ClassDesc} for a class or interface type,
 83      * given a package name and the unqualified (simple) name for the
 84      * class or interface.
 85      *
 86      * @param packageName the package name (dot-separated); if the package
 87      *                    name is the empty string, the class is considered to
 88      *                    be in the unnamed package

 93      * not in the correct format
 94      */
 95     static ClassDesc of(String packageName, String className) {
 96         ConstantUtils.validateBinaryClassName(requireNonNull(packageName));
 97         if (packageName.isEmpty()) {
 98             return of(className);
 99         }
100         validateMemberName(requireNonNull(className), false);
101         return ofDescriptor("L" + binaryToInternal(packageName) +
102                 "/" + className + ";");
103     }
104 
105     /**
106      * Returns a {@linkplain ClassDesc} given a descriptor string for a class,
107      * interface, array, or primitive type.
108      *
109      * @apiNote
110      *
111      * A field type descriptor string for a non-array type is either
112      * a one-letter code corresponding to a primitive type
113      * ({@code "J", "I", "C", "S", "B", "D", "F", "Z", "V"}), or the letter {@code "L"}, followed

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

119      * See JVMS 4.3.2 ("Field Descriptors") for more detail.
120      *
121      * @param descriptor a field descriptor string
122      * @return a {@linkplain ClassDesc} describing the desired class
123      * @throws NullPointerException if the argument is {@code null}
124      * @throws IllegalArgumentException if the name string is not in the
125      * correct format
126      * @jvms 4.3.2 Field Descriptors
127      * @jvms 4.4.1 The CONSTANT_Class_info Structure
128      */
129     static ClassDesc ofDescriptor(String descriptor) {
130         requireNonNull(descriptor);
131         if (descriptor.isEmpty()) {
132             throw new IllegalArgumentException(
133                     "not a valid reference type descriptor: " + descriptor);
134         }
135         int depth = ConstantUtils.arrayDepth(descriptor);
136         if (depth > ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
137             throw new IllegalArgumentException(
138                     "Cannot create an array type descriptor with more than " +
139                     ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
140         }
141         return (descriptor.length() == 1)
142                ? new PrimitiveClassDescImpl(descriptor)
143                : new ReferenceClassDescImpl(descriptor);
144     }
145 
146     /**
147      * Returns a {@linkplain ClassDesc} for an array type whose component type
148      * is described by this {@linkplain ClassDesc}.
149      *
150      * @return a {@linkplain ClassDesc} describing the array type
151      * @throws IllegalStateException if the resulting {@linkplain ClassDesc} would have an array rank of greater than 255
152      * @jvms 4.4.1 The CONSTANT_Class_info Structure
153      */
154     default ClassDesc arrayType() {
155         int depth = ConstantUtils.arrayDepth(descriptorString());
156         if (depth >= ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
157             throw new IllegalStateException(
158                     "Cannot create an array type descriptor with more than " +
159                     ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
160         }
161         return arrayType(1);
162     }
163 

235      */
236     default boolean isArray() {
237         return descriptorString().startsWith("[");
238     }
239 
240     /**
241      * Returns whether this {@linkplain ClassDesc} describes a primitive type.
242      *
243      * @return whether this {@linkplain ClassDesc} describes a primitive type
244      */
245     default boolean isPrimitive() {
246         return descriptorString().length() == 1;
247     }
248 
249     /**
250      * Returns whether this {@linkplain ClassDesc} describes a class or interface type.
251      *
252      * @return whether this {@linkplain ClassDesc} describes a class or interface type
253      */
254     default boolean isClassOrInterface() {
255         return descriptorString().startsWith("L");











256     }
257 
258     /**
259      * Returns the component type of this {@linkplain ClassDesc}, if it describes
260      * an array type, or {@code null} otherwise.
261      *
262      * @return a {@linkplain ClassDesc} describing the component type, or {@code null}
263      * if this descriptor does not describe an array type
264      */
265     default ClassDesc componentType() {
266         return isArray() ? ClassDesc.ofDescriptor(descriptorString().substring(1)) : null;
267     }
268 
269     /**
270      * Returns the package name of this {@linkplain ClassDesc}, if it describes
271      * a class or interface type.
272      *
273      * @return the package name, or the empty string if the class is in the
274      * default package, or this {@linkplain ClassDesc} does not describe a class or interface type
275      */

 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      */
 77     static ClassDesc of(String name) {
 78         ConstantUtils.validateBinaryClassName(requireNonNull(name));
 79         return ClassDesc.ofDescriptor("L" + binaryToInternal(name) + ";");
 80     }
 81 
 82     /**
 83      * Returns a {@linkplain ClassDesc} for a class or interface type,
 84      * given a package name and the unqualified (simple) name for the
 85      * class or interface.
 86      *
 87      * @param packageName the package name (dot-separated); if the package
 88      *                    name is the empty string, the class is considered to
 89      *                    be in the unnamed package

 94      * not in the correct format
 95      */
 96     static ClassDesc of(String packageName, String className) {
 97         ConstantUtils.validateBinaryClassName(requireNonNull(packageName));
 98         if (packageName.isEmpty()) {
 99             return of(className);
100         }
101         validateMemberName(requireNonNull(className), false);
102         return ofDescriptor("L" + binaryToInternal(packageName) +
103                 "/" + className + ";");
104     }
105 
106     /**
107      * Returns a {@linkplain ClassDesc} given a descriptor string for a class,
108      * interface, array, or primitive type.
109      *
110      * @apiNote
111      *
112      * A field type descriptor string for a non-array type is either
113      * a one-letter code corresponding to a primitive type
114      * ({@code "J", "I", "C", "S", "B", "D", "F", "Z", "V"}),
115      * or the letter {@code "L"} or {@code "Q"} followed
116      * by the fully qualified binary name of a class, followed by {@code ";"}.
117      * A field type descriptor for an array type is the character {@code "["}
118      * followed by the field descriptor for the component type.  Examples of
119      * valid type descriptor strings include {@code "Ljava/lang/String;"},
120      * {@code "QPoint;}, {@code "I"}, {@code "[I"}, {@code "V"},
121      * {@code "[Ljava/lang/String;"}, {@code "[LPoint;"}, {@code "[[QPoint;} etc.
122      * See JVMS 4.3.2 ("Field Descriptors") for more detail.
123      *
124      * @param descriptor a field descriptor string
125      * @return a {@linkplain ClassDesc} describing the desired class
126      * @throws NullPointerException if the argument is {@code null}
127      * @throws IllegalArgumentException if the name string is not in the
128      * correct format
129      * @jvms 4.3.2 Field Descriptors
130      * @jvms 4.4.1 The CONSTANT_Class_info Structure
131      */
132     static ClassDesc ofDescriptor(String descriptor) {
133         requireNonNull(descriptor);
134         if (descriptor.isEmpty()) {
135             throw new IllegalArgumentException(
136                     "not a valid reference type descriptor: " + descriptor);
137         }
138         int depth = ConstantUtils.arrayDepth(descriptor);
139         if (depth > ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
140             throw new IllegalArgumentException(
141                     "Cannot create an array type descriptor with more than " +
142                     ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
143         }
144         return (descriptor.length() == 1)
145                ? new PrimitiveClassDescImpl(descriptor)
146                : new ClassDescImpl(descriptor);
147     }
148 
149     /**
150      * Returns a {@linkplain ClassDesc} for an array type whose component type
151      * is described by this {@linkplain ClassDesc}.
152      *
153      * @return a {@linkplain ClassDesc} describing the array type
154      * @throws IllegalStateException if the resulting {@linkplain ClassDesc} would have an array rank of greater than 255
155      * @jvms 4.4.1 The CONSTANT_Class_info Structure
156      */
157     default ClassDesc arrayType() {
158         int depth = ConstantUtils.arrayDepth(descriptorString());
159         if (depth >= ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
160             throw new IllegalStateException(
161                     "Cannot create an array type descriptor with more than " +
162                     ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
163         }
164         return arrayType(1);
165     }
166 

238      */
239     default boolean isArray() {
240         return descriptorString().startsWith("[");
241     }
242 
243     /**
244      * Returns whether this {@linkplain ClassDesc} describes a primitive type.
245      *
246      * @return whether this {@linkplain ClassDesc} describes a primitive type
247      */
248     default boolean isPrimitive() {
249         return descriptorString().length() == 1;
250     }
251 
252     /**
253      * Returns whether this {@linkplain ClassDesc} describes a class or interface type.
254      *
255      * @return whether this {@linkplain ClassDesc} describes a class or interface type
256      */
257     default boolean isClassOrInterface() {
258         return descriptorString().startsWith("L") || descriptorString().startsWith("Q");
259     }
260 
261     /**
262      * Returns whether this {@linkplain ClassDesc} describes a
263      * {@linkplain Class#isValueType() primitive value type}.
264      *
265      * @return whether this {@linkplain ClassDesc} describes a primitive value type.
266      * @since Valhalla
267      */
268     default boolean isValueType() {
269         return descriptorString().startsWith("Q");
270     }
271 
272     /**
273      * Returns the component type of this {@linkplain ClassDesc}, if it describes
274      * an array type, or {@code null} otherwise.
275      *
276      * @return a {@linkplain ClassDesc} describing the component type, or {@code null}
277      * if this descriptor does not describe an array type
278      */
279     default ClassDesc componentType() {
280         return isArray() ? ClassDesc.ofDescriptor(descriptorString().substring(1)) : null;
281     }
282 
283     /**
284      * Returns the package name of this {@linkplain ClassDesc}, if it describes
285      * a class or interface type.
286      *
287      * @return the package name, or the empty string if the class is in the
288      * default package, or this {@linkplain ClassDesc} does not describe a class or interface type
289      */
< prev index next >