82 import sun.reflect.generics.repository.ClassRepository;
83 import sun.reflect.generics.repository.MethodRepository;
84 import sun.reflect.generics.repository.ConstructorRepository;
85 import sun.reflect.generics.scope.ClassScope;
86 import sun.security.util.SecurityConstants;
87 import sun.reflect.annotation.*;
88 import sun.reflect.misc.ReflectUtil;
89
90 /**
91 * Instances of the class {@code Class} represent classes and
92 * interfaces in a running Java application. An enum type and a record
93 * type are kinds of class; an annotation type is a kind of
94 * interface. Every array also belongs to a class that is reflected as
95 * a {@code Class} object that is shared by all arrays with the same
96 * element type and number of dimensions. The primitive Java types
97 * ({@code boolean}, {@code byte}, {@code char}, {@code short}, {@code
98 * int}, {@code long}, {@code float}, and {@code double}), and the
99 * keyword {@code void} are also represented as {@code Class} objects.
100 *
101 * <p> {@code Class} has no public constructor. Instead a {@code Class}
102 * object is constructed automatically by the Java Virtual Machine
103 * when a class loader invokes one of the
104 * {@link ClassLoader#defineClass(String,byte[], int,int) defineClass} methods
105 * and passes the bytes of a {@code class} file.
106 *
107 * <p> The methods of class {@code Class} expose many characteristics of a
108 * class or interface. Most characteristics are derived from the {@code class}
109 * file that the class loader passed to the Java Virtual Machine. A few
110 * characteristics are determined by the class loading environment at run time,
111 * such as the module returned by {@link #getModule() getModule()}.
112 *
113 * <p> Some methods of class {@code Class} expose whether the declaration of
114 * a class or interface in Java source code was <em>enclosed</em> within
115 * another declaration. Other methods describe how a class or interface
116 * is situated in a <em>nest</em>. A <a id="nest">nest</a> is a set of
117 * classes and interfaces, in the same run-time package, that
118 * allow mutual access to their {@code private} members.
119 * The classes and interfaces are known as <em>nestmates</em>.
120 * One nestmate acts as the
121 * <em>nest host</em>, and enumerates the other nestmates which
122 * belong to the nest; each of them in turn records it as the nest host.
123 * The classes and interfaces which belong to a nest, including its host, are
124 * determined when
125 * {@code class} files are generated, for example, a Java compiler
126 * will typically record a top-level class as the host of a nest where the
127 * other members are the classes and interfaces whose declarations are
128 * enclosed within the top-level class declaration.
129 *
130 * <p> Some methods of class {@code Class} expose some characteristics of
131 * <em>hidden classes</em>. Hidden classes are created by calling
132 * {@link java.lang.invoke.MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)
133 * Lookup::defineHiddenClass}.
134 *
135 * <p> The following example uses a {@code Class} object to print the
136 * class name of an object:
137 *
138 * <blockquote><pre>
139 * void printClassName(Object obj) {
140 * System.out.println("The class of " + obj +
141 * " is " + obj.getClass().getName());
142 * }
143 * </pre></blockquote>
144 *
145 * <p> It is also possible to get the {@code Class} object for a named
146 * type (or for void) using a class literal. See Section 15.8.2 of
147 * <cite>The Java™ Language Specification</cite>.
148 * For example:
149 *
150 * <blockquote>
151 * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
152 * </blockquote>
153 *
154 * @param <T> the type of the class modeled by this {@code Class}
155 * object. For example, the type of {@code String.class} is {@code
156 * Class<String>}. Use {@code Class<?>} if the class being modeled is
157 * unknown.
158 *
159 * @author unascribed
160 * @see java.lang.ClassLoader#defineClass(byte[], int, int)
161 * @since 1.0
162 */
163 public final class Class<T> implements java.io.Serializable,
164 GenericDeclaration,
165 Type,
166 AnnotatedElement,
167 TypeDescriptor.OfField<Class<?>>,
168 Constable {
169 private static final int ANNOTATION= 0x00002000;
170 private static final int ENUM = 0x00004000;
171 private static final int SYNTHETIC = 0x00001000;
172
173 private static native void registerNatives();
174 static {
175 registerNatives();
176 }
177
178 /*
179 * Private constructor. Only the Java Virtual Machine creates Class objects.
180 * This constructor is not used and prevents the default constructor being
181 * generated.
182 */
183 private Class(ClassLoader loader, Class<?> arrayComponentType) {
184 // Initialize final field for classLoader. The initialization value of non-null
185 // prevents future JIT optimizations from assuming this final field is null.
186 classLoader = loader;
187 componentType = arrayComponentType;
188 }
189
190 /**
191 * Converts the object to a string. The string representation is the
192 * string "class" or "interface", followed by a space, and then by the
193 * fully qualified name of the class in the format returned by
194 * {@code getName}. If this {@code Class} object represents a
195 * primitive type, this method returns the name of the primitive type. If
196 * this {@code Class} object represents void this method returns
197 * "void". If this {@code Class} object represents an array type,
198 * this method returns "class " followed by {@code getName}.
199 *
200 * @return a string representation of this class object.
201 */
202 public String toString() {
203 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
204 + getName();
205 }
206
207 /**
208 * Returns a string describing this {@code Class}, including
209 * information about modifiers and type parameters.
210 *
211 * The string is formatted as a list of type modifiers, if any,
212 * followed by the kind of type (empty string for primitive types
213 * and {@code class}, {@code enum}, {@code interface},
214 * <code>@</code>{@code interface}, or {@code record} as appropriate), followed
215 * by the type's name, followed by an angle-bracketed
729 * @see java.lang.Void#TYPE
730 * @since 1.1
731 */
732 @HotSpotIntrinsicCandidate
733 public native boolean isPrimitive();
734
735 /**
736 * Returns true if this {@code Class} object represents an annotation
737 * type. Note that if this method returns true, {@link #isInterface()}
738 * would also return true, as all annotation types are also interfaces.
739 *
740 * @return {@code true} if this class object represents an annotation
741 * type; {@code false} otherwise
742 * @since 1.5
743 */
744 public boolean isAnnotation() {
745 return (getModifiers() & ANNOTATION) != 0;
746 }
747
748 /**
749 * Returns {@code true} if this class is a synthetic class;
750 * returns {@code false} otherwise.
751 * @return {@code true} if and only if this class is a synthetic class as
752 * defined by the Java Language Specification.
753 * @jls 13.1 The Form of a Binary
754 * @since 1.5
755 */
756 public boolean isSynthetic() {
757 return (getModifiers() & SYNTHETIC) != 0;
758 }
759
760 /**
761 * Returns the name of the entity (class, interface, array class,
762 * primitive type, or void) represented by this {@code Class} object,
763 * as a {@code String}.
764 *
765 * <p> If this class object represents a reference type that is not an
766 * array type then the binary name of the class is returned, as specified
767 * by
768 * <cite>The Java™ Language Specification</cite>.
769 *
770 * <p> If this class object represents a primitive type or void, then the
771 * name returned is a {@code String} equal to the Java language
772 * keyword corresponding to the primitive type or void.
773 *
774 * <p> If this class object represents a class of arrays, then the internal
775 * form of the name consists of the name of the element type preceded by
776 * one or more '{@code [}' characters representing the depth of the array
777 * nesting. The encoding of element type names is as follows:
778 *
779 * <blockquote><table class="striped">
780 * <caption style="display:none">Element types and encodings</caption>
781 * <thead>
782 * <tr><th scope="col"> Element Type <th scope="col"> Encoding
783 * </thead>
784 * <tbody style="text-align:left">
785 * <tr><th scope="row"> boolean <td style="text-align:center"> Z
786 * <tr><th scope="row"> byte <td style="text-align:center"> B
787 * <tr><th scope="row"> char <td style="text-align:center"> C
788 * <tr><th scope="row"> class or interface
789 * <td style="text-align:center"> L<i>classname</i>;
790 * <tr><th scope="row"> double <td style="text-align:center"> D
791 * <tr><th scope="row"> float <td style="text-align:center"> F
792 * <tr><th scope="row"> int <td style="text-align:center"> I
793 * <tr><th scope="row"> long <td style="text-align:center"> J
794 * <tr><th scope="row"> short <td style="text-align:center"> S
795 * </tbody>
796 * </table></blockquote>
797 *
798 * <p> The class or interface name <i>classname</i> is the binary name of
799 * the class specified above.
800 *
801 * <p> Examples:
802 * <blockquote><pre>
803 * String.class.getName()
804 * returns "java.lang.String"
805 * byte.class.getName()
806 * returns "byte"
807 * (new Object[3]).getClass().getName()
808 * returns "[Ljava.lang.Object;"
809 * (new int[3][4][5][6][7][8][9]).getClass().getName()
810 * returns "[[[[[[[I"
811 * </pre></blockquote>
812 *
813 * <p> If this class object represents a {@linkplain #isHiddenClass() hidden class},
814 * then the name of a hidden class is not a binary name and contains
815 * a ASCII {@code '/'} character.
816 *
817 * @return the name of the class or interface
818 * represented by this object.
819 */
820 public String getName() {
821 String name = this.name;
822 return name != null ? name : initClassName();
823 }
824
825 // Cache the name to reduce the number of calls into the VM.
826 // This field would be set by VM itself during initClassName call.
827 private transient String name;
828 private native String initClassName();
829
830 /**
831 * Returns the class loader for the class. Some implementations may use
832 * null to represent the bootstrap class loader. This method will return
833 * null in such implementations if this class was loaded by the bootstrap
834 * class loader.
835 *
836 * <p>If this object
837 * represents a primitive type or void, null is returned.
838 *
1610 * @return an informative string for the name of this type
1611 * @since 1.8
1612 */
1613 public String getTypeName() {
1614 if (isArray()) {
1615 try {
1616 Class<?> cl = this;
1617 int dimensions = 0;
1618 do {
1619 dimensions++;
1620 cl = cl.getComponentType();
1621 } while (cl.isArray());
1622 return cl.getName() + "[]".repeat(dimensions);
1623 } catch (Throwable e) { /*FALLTHRU*/ }
1624 }
1625 return getName();
1626 }
1627
1628 /**
1629 * Returns the canonical name of the underlying class as
1630 * defined by the Java Language Specification. Returns null if
1631 * the underlying class does not have a canonical name (i.e., if
1632 * it is a local or anonymous class or an array whose component
1633 * type does not have a canonical name).
1634 * @return the canonical name of the underlying class if it exists, and
1635 * {@code null} otherwise.
1636 * @since 1.5
1637 */
1638 public String getCanonicalName() {
1639 ReflectionData<T> rd = reflectionData();
1640 String canonicalName = rd.canonicalName;
1641 if (canonicalName == null) {
1642 rd.canonicalName = canonicalName = getCanonicalName0();
1643 }
1644 return canonicalName == ReflectionData.NULL_SENTINEL? null : canonicalName;
1645 }
1646
1647 private String getCanonicalName0() {
1648 if (isArray()) {
1649 String canonicalName = getComponentType().getCanonicalName();
1650 if (canonicalName != null)
1651 return canonicalName + "[]";
1652 else
1653 return ReflectionData.NULL_SENTINEL;
1654 }
1655 if (isHiddenClass() || isLocalOrAnonymousClass())
1656 return ReflectionData.NULL_SENTINEL;
1657 Class<?> enclosingClass = getEnclosingClass();
1658 if (enclosingClass == null) { // top level class
1659 return getName();
1660 } else {
1661 String enclosingName = enclosingClass.getCanonicalName();
1662 if (enclosingName == null)
1663 return ReflectionData.NULL_SENTINEL;
1664 return enclosingName + "." + getSimpleName();
1665 }
1666 }
1667
1668 /**
1669 * Returns {@code true} if and only if the underlying class
1670 * is an anonymous class. An anonymous class is not a
1671 * {@linkplain #isHiddenClass() hidden class}.
1672 *
1673 * @return {@code true} if and only if this class is an anonymous class.
1674 * @since 1.5
1675 */
1676 public boolean isAnonymousClass() {
1677 return !isArray() && isLocalOrAnonymousClass() &&
1678 getSimpleBinaryName0() == null;
1679 }
1680
1681 /**
1682 * Returns {@code true} if and only if the underlying class
1683 * is a local class.
1684 *
1685 * @return {@code true} if and only if this class is a local class.
1686 * @since 1.5
1687 */
1688 public boolean isLocalClass() {
1689 return isLocalOrAnonymousClass() &&
1690 (isArray() || getSimpleBinaryName0() != null);
1691 }
4220 public Class<?> arrayType() {
4221 return Array.newInstance(this, 0).getClass();
4222 }
4223
4224 /**
4225 * Returns a nominal descriptor for this instance, if one can be
4226 * constructed, or an empty {@link Optional} if one cannot be.
4227 *
4228 * @return An {@link Optional} containing the resulting nominal descriptor,
4229 * or an empty {@link Optional} if one cannot be constructed.
4230 * @since 12
4231 */
4232 @Override
4233 public Optional<ClassDesc> describeConstable() {
4234 return Optional.of(ClassDesc.ofDescriptor(descriptorString()));
4235 }
4236
4237 /**
4238 * Returns {@code true} if and only if the underlying class is a hidden class.
4239 *
4240 * <p> A <em>hidden class</em> is created by calling
4241 * {@link MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)
4242 * Lookup::defineHiddenClass}. A hidden class is non-discoverable
4243 * by name via {@link Class#forName(String) Class::forName},
4244 * {@link ClassLoader#loadClass(String) ClassLoader::loadClass}, and bytecode linkage.
4245 *
4246 * @return {@code true} if and only if this class is a hidden class.
4247 *
4248 * @since 15
4249 * @see MethodHandles.Lookup#defineHiddenClass
4250 */
4251 @HotSpotIntrinsicCandidate
4252 public native boolean isHiddenClass();
4253
4254 }
|
82 import sun.reflect.generics.repository.ClassRepository;
83 import sun.reflect.generics.repository.MethodRepository;
84 import sun.reflect.generics.repository.ConstructorRepository;
85 import sun.reflect.generics.scope.ClassScope;
86 import sun.security.util.SecurityConstants;
87 import sun.reflect.annotation.*;
88 import sun.reflect.misc.ReflectUtil;
89
90 /**
91 * Instances of the class {@code Class} represent classes and
92 * interfaces in a running Java application. An enum type and a record
93 * type are kinds of class; an annotation type is a kind of
94 * interface. Every array also belongs to a class that is reflected as
95 * a {@code Class} object that is shared by all arrays with the same
96 * element type and number of dimensions. The primitive Java types
97 * ({@code boolean}, {@code byte}, {@code char}, {@code short}, {@code
98 * int}, {@code long}, {@code float}, and {@code double}), and the
99 * keyword {@code void} are also represented as {@code Class} objects.
100 *
101 * <p> {@code Class} has no public constructor. Instead a {@code Class}
102 * object is constructed automatically by the Java Virtual Machine when
103 * a class is derived from the bytes of a {@code class} file through
104 * the invocation of one of the following methods:
105 * <ul>
106 * <li> {@link ClassLoader#defineClass(String, byte[], int, int) ClassLoader::defineClass}
107 * <li> {@link java.lang.invoke.MethodHandles.Lookup#defineClass(byte[])
108 * java.lang.invoke.MethodHandles.Lookup::defineClass}
109 * <li> {@link java.lang.invoke.MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)
110 * java.lang.invoke.MethodHandles.Lookup::defineHiddenClass}
111 * </ul>
112 *
113 * <p> The methods of class {@code Class} expose many characteristics of a
114 * class or interface. Most characteristics are derived from the {@code class}
115 * file that the class loader passed to the Java Virtual Machine or
116 * from the {@code class} file passed to {@code Lookup::defineClass}
117 * or {@code Lookup::defineHiddenClass}.
118 * A few characteristics are determined by the class loading environment
119 * at run time, such as the module returned by {@link #getModule() getModule()}.
120 *
121 * <p> The following example uses a {@code Class} object to print the
122 * class name of an object:
123 *
124 * <blockquote><pre>
125 * void printClassName(Object obj) {
126 * System.out.println("The class of " + obj +
127 * " is " + obj.getClass().getName());
128 * }
129 * </pre></blockquote>
130 *
131 * It is also possible to get the {@code Class} object for a named
132 * type (or for void) using a class literal. See Section 15.8.2 of
133 * <cite>The Java™ Language Specification</cite>.
134 * For example:
135 *
136 * <blockquote>
137 * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
138 * </blockquote>
139 *
140 * <p> Some methods of class {@code Class} expose whether the declaration of
141 * a class or interface in Java source code was <em>enclosed</em> within
142 * another declaration. Other methods describe how a class or interface
143 * is situated in a <em>nest</em>. A <a id="nest">nest</a> is a set of
144 * classes and interfaces, in the same run-time package, that
145 * allow mutual access to their {@code private} members.
146 * The classes and interfaces are known as <em>nestmates</em>.
147 * One nestmate acts as the
148 * <em>nest host</em>, and enumerates the other nestmates which
149 * belong to the nest; each of them in turn records it as the nest host.
150 * The classes and interfaces which belong to a nest, including its host, are
151 * determined when
152 * {@code class} files are generated, for example, a Java compiler
153 * will typically record a top-level class as the host of a nest where the
154 * other members are the classes and interfaces whose declarations are
155 * enclosed within the top-level class declaration.
156 *
157 * <p> A class or interface created by the invocation of
158 * {@link java.lang.invoke.MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)
159 * Lookup::defineHiddenClass} is a {@linkplain Class#isHidden() <em>hidden</em>}
160 * class or interface.
161 * All kinds of class, including enum types and record types, may be
162 * hidden classes; all kinds of interface, including annotation types,
163 * may be hidden interfaces.
164 *
165 * The {@linkplain #getName() name of a hidden class or interface} is not a
166 * binary name, which means that a hidden class or interface cannot be
167 * resolved by linkage of constant pool entries and cannot be discovered by
168 * {@link #forName Class::forName} or {@link ClassLoader#loadClass(String, boolean)
169 * ClassLoader::loadClass}.
170 *
171 * A hidden class or interface is never an array class, but may be
172 * the element type of an array. In all other respects, the fact that
173 * a class or interface is hidden has no bearing on the characteristics
174 * exposed by the methods of class {@code Class}.
175 *
176 * @param <T> the type of the class modeled by this {@code Class}
177 * object. For example, the type of {@code String.class} is {@code
178 * Class<String>}. Use {@code Class<?>} if the class being modeled is
179 * unknown.
180 *
181 * @author unascribed
182 * @see java.lang.ClassLoader#defineClass(byte[], int, int)
183 * @since 1.0
184 */
185 public final class Class<T> implements java.io.Serializable,
186 GenericDeclaration,
187 Type,
188 AnnotatedElement,
189 TypeDescriptor.OfField<Class<?>>,
190 Constable {
191 private static final int ANNOTATION= 0x00002000;
192 private static final int ENUM = 0x00004000;
193 private static final int SYNTHETIC = 0x00001000;
194
195 private static native void registerNatives();
196 static {
197 registerNatives();
198 }
199
200 /*
201 * Private constructor. Only the Java Virtual Machine creates Class objects.
202 * This constructor is not used and prevents the default constructor being
203 * generated.
204 */
205 private Class(ClassLoader loader, Class<?> arrayComponentType) {
206 // Initialize final field for classLoader. The initialization value of non-null
207 // prevents future JIT optimizations from assuming this final field is null.
208 classLoader = loader;
209 componentType = arrayComponentType;
210 }
211
212 /**
213 * Converts the object to a string. The string representation is the
214 * string "class" or "interface", followed by a space, and then by the
215 * name of the class in the format returned by {@code getName}.
216 * If this {@code Class} object represents a primitive type,
217 * this method returns the name of the primitive type. If
218 * this {@code Class} object represents void this method returns
219 * "void". If this {@code Class} object represents an array type,
220 * this method returns "class " followed by {@code getName}.
221 *
222 * @return a string representation of this class object.
223 */
224 public String toString() {
225 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
226 + getName();
227 }
228
229 /**
230 * Returns a string describing this {@code Class}, including
231 * information about modifiers and type parameters.
232 *
233 * The string is formatted as a list of type modifiers, if any,
234 * followed by the kind of type (empty string for primitive types
235 * and {@code class}, {@code enum}, {@code interface},
236 * <code>@</code>{@code interface}, or {@code record} as appropriate), followed
237 * by the type's name, followed by an angle-bracketed
751 * @see java.lang.Void#TYPE
752 * @since 1.1
753 */
754 @HotSpotIntrinsicCandidate
755 public native boolean isPrimitive();
756
757 /**
758 * Returns true if this {@code Class} object represents an annotation
759 * type. Note that if this method returns true, {@link #isInterface()}
760 * would also return true, as all annotation types are also interfaces.
761 *
762 * @return {@code true} if this class object represents an annotation
763 * type; {@code false} otherwise
764 * @since 1.5
765 */
766 public boolean isAnnotation() {
767 return (getModifiers() & ANNOTATION) != 0;
768 }
769
770 /**
771 * Returns {@code true} if and only if this class has the synthetic modifier
772 * bit set.
773 *
774 * @return {@code true} if and only if this class has the synthetic modifier bit set
775 * @jls 13.1 The Form of a Binary
776 * @jvms 4.1 The {@code ClassFile} Structure
777 * @since 1.5
778 */
779 public boolean isSynthetic() {
780 return (getModifiers() & SYNTHETIC) != 0;
781 }
782
783 /**
784 * Returns the name of the entity (class, interface, array class,
785 * primitive type, or void) represented by this {@code Class} object,
786 * as a {@code String}.
787 *
788 * <p> If this {@code Class} object represents a class or interface,
789 * not an array class, then
790 * <ul>
791 * <li> If the class or interface is not {@linkplain #isHidden() hidden},
792 * then the <a href="ClassLoader.html#binary-name">binary name</a>
793 * of the class is returned, as specified by
794 * <cite>The Java™ Language Specification</cite>.
795 * <li> If the class or interface is hidden, then the result is a string
796 * of the format: {@code N + '/' + <suffix>}
797 * where {@code N} is the binary name indicated by the {@code class}
798 * file passed to
799 * {@link java.lang.invoke.MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)
800 * java.lang.invoke.MethodHandles.Lookup::defineHiddenClass} and
801 * {@code <suffix>} is an unqualified name that is guaranteed to be
802 * unique during this execution of the JVM.
803 * </ul>
804 *
805 * <p> If this {@code Class} object represents an array class, then
806 * the name consists of the encoded element type (see below) preceded by
807 * one or more '{@code [}' characters representing the depth of the array
808 * nesting. The encoding of element type names is as follows:
809 *
810 * <blockquote><table class="striped">
811 * <caption style="display:none">Element types and encodings</caption>
812 * <thead>
813 * <tr><th scope="col"> Element Type <th scope="col"> Encoding
814 * </thead>
815 * <tbody style="text-align:left">
816 * <tr><th scope="row"> boolean <td style="text-align:center"> Z
817 * <tr><th scope="row"> byte <td style="text-align:center"> B
818 * <tr><th scope="row"> char <td style="text-align:center"> C
819 * <tr><th scope="row"> class or interface
820 * <td style="text-align:center"> L<i>classname</i>;
821 * <tr><th scope="row"> double <td style="text-align:center"> D
822 * <tr><th scope="row"> float <td style="text-align:center"> F
823 * <tr><th scope="row"> int <td style="text-align:center"> I
824 * <tr><th scope="row"> long <td style="text-align:center"> J
825 * <tr><th scope="row"> short <td style="text-align:center"> S
826 * </tbody>
827 * </table></blockquote>
828 *
829 * <p> If this {@code Class} object represents a primitive type or {@code void},
830 * then the result is a {@code String} equal to the Java language
831 * keyword corresponding to the primitive type or {@code void}.
832 *
833 * <p> The class or interface name <i>classname</i> is the binary name of
834 * the class specified above.
835 *
836 * <p> Examples:
837 * <blockquote><pre>
838 * String.class.getName()
839 * returns "java.lang.String"
840 * byte.class.getName()
841 * returns "byte"
842 * (new Object[3]).getClass().getName()
843 * returns "[Ljava.lang.Object;"
844 * (new int[3][4][5][6][7][8][9]).getClass().getName()
845 * returns "[[[[[[[I"
846 * </pre></blockquote>
847 *
848 * @return the name of the class or interface
849 * represented by this {@code Class} object.
850 */
851 public String getName() {
852 String name = this.name;
853 return name != null ? name : initClassName();
854 }
855
856 // Cache the name to reduce the number of calls into the VM.
857 // This field would be set by VM itself during initClassName call.
858 private transient String name;
859 private native String initClassName();
860
861 /**
862 * Returns the class loader for the class. Some implementations may use
863 * null to represent the bootstrap class loader. This method will return
864 * null in such implementations if this class was loaded by the bootstrap
865 * class loader.
866 *
867 * <p>If this object
868 * represents a primitive type or void, null is returned.
869 *
1641 * @return an informative string for the name of this type
1642 * @since 1.8
1643 */
1644 public String getTypeName() {
1645 if (isArray()) {
1646 try {
1647 Class<?> cl = this;
1648 int dimensions = 0;
1649 do {
1650 dimensions++;
1651 cl = cl.getComponentType();
1652 } while (cl.isArray());
1653 return cl.getName() + "[]".repeat(dimensions);
1654 } catch (Throwable e) { /*FALLTHRU*/ }
1655 }
1656 return getName();
1657 }
1658
1659 /**
1660 * Returns the canonical name of the underlying class as
1661 * defined by <cite>The Java™ Language Specification</cite>.
1662 * Returns {@code null} if the underlying class does not have a canonical
1663 * name. Classes without canonical names include:
1664 * <ul>
1665 * <li>a {@linkplain #isLocalClass() local class}
1666 * <li>a {@linkplain #isAnonymousClass() anonymous class}
1667 * <li>a {@linkplain #isHidden() hidden class}
1668 * <li>an array whose component type does not have a canonical name</li>
1669 * </ul>
1670 *
1671 * @return the canonical name of the underlying class if it exists, and
1672 * {@code null} otherwise.
1673 * @since 1.5
1674 */
1675 public String getCanonicalName() {
1676 ReflectionData<T> rd = reflectionData();
1677 String canonicalName = rd.canonicalName;
1678 if (canonicalName == null) {
1679 rd.canonicalName = canonicalName = getCanonicalName0();
1680 }
1681 return canonicalName == ReflectionData.NULL_SENTINEL? null : canonicalName;
1682 }
1683
1684 private String getCanonicalName0() {
1685 if (isArray()) {
1686 String canonicalName = getComponentType().getCanonicalName();
1687 if (canonicalName != null)
1688 return canonicalName + "[]";
1689 else
1690 return ReflectionData.NULL_SENTINEL;
1691 }
1692 if (isHidden() || isLocalOrAnonymousClass())
1693 return ReflectionData.NULL_SENTINEL;
1694 Class<?> enclosingClass = getEnclosingClass();
1695 if (enclosingClass == null) { // top level class
1696 return getName();
1697 } else {
1698 String enclosingName = enclosingClass.getCanonicalName();
1699 if (enclosingName == null)
1700 return ReflectionData.NULL_SENTINEL;
1701 return enclosingName + "." + getSimpleName();
1702 }
1703 }
1704
1705 /**
1706 * Returns {@code true} if and only if the underlying class
1707 * is an anonymous class.
1708 *
1709 * @apiNote
1710 * An anonymous class is not a {@linkplain #isHidden() hidden class}.
1711 *
1712 * @return {@code true} if and only if this class is an anonymous class.
1713 * @since 1.5
1714 */
1715 public boolean isAnonymousClass() {
1716 return !isArray() && isLocalOrAnonymousClass() &&
1717 getSimpleBinaryName0() == null;
1718 }
1719
1720 /**
1721 * Returns {@code true} if and only if the underlying class
1722 * is a local class.
1723 *
1724 * @return {@code true} if and only if this class is a local class.
1725 * @since 1.5
1726 */
1727 public boolean isLocalClass() {
1728 return isLocalOrAnonymousClass() &&
1729 (isArray() || getSimpleBinaryName0() != null);
1730 }
4259 public Class<?> arrayType() {
4260 return Array.newInstance(this, 0).getClass();
4261 }
4262
4263 /**
4264 * Returns a nominal descriptor for this instance, if one can be
4265 * constructed, or an empty {@link Optional} if one cannot be.
4266 *
4267 * @return An {@link Optional} containing the resulting nominal descriptor,
4268 * or an empty {@link Optional} if one cannot be constructed.
4269 * @since 12
4270 */
4271 @Override
4272 public Optional<ClassDesc> describeConstable() {
4273 return Optional.of(ClassDesc.ofDescriptor(descriptorString()));
4274 }
4275
4276 /**
4277 * Returns {@code true} if and only if the underlying class is a hidden class.
4278 *
4279 * @return {@code true} if and only if this class is a hidden class.
4280 *
4281 * @since 15
4282 * @see MethodHandles.Lookup#defineHiddenClass
4283 */
4284 @HotSpotIntrinsicCandidate
4285 public native boolean isHidden();
4286
4287 }
|