< prev index next >

src/java.base/share/classes/java/lang/Class.java

Print this page




 122  * <em>nest host</em>, and enumerates the other nestmates which
 123  * belong to the nest; each of them in turn records it as the nest host.
 124  * The classes and interfaces which belong to a nest, including its host, are
 125  * determined when
 126  * {@code class} files are generated, for example, a Java compiler
 127  * will typically record a top-level class as the host of a nest where the
 128  * other members are the classes and interfaces whose declarations are
 129  * enclosed within the top-level class declaration.
 130  *
 131  * <p> The following example uses a {@code Class} object to print the
 132  * class name of an object:
 133  *
 134  * <blockquote><pre>
 135  *     void printClassName(Object obj) {
 136  *         System.out.println("The class of " + obj +
 137  *                            " is " + obj.getClass().getName());
 138  *     }
 139  * </pre></blockquote>
 140  *
 141  * <p> It is also possible to get the {@code Class} object for a named
 142  * type (or for void) using a class literal.  See Section 15.8.2 of
 143  * <cite>The Java&trade; Language Specification</cite>.
 144  * For example:
 145  *
 146  * <blockquote>
 147  *     {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
 148  * </blockquote>
 149  *
 150  * @param <T> the type of the class modeled by this {@code Class}
 151  * object.  For example, the type of {@code String.class} is {@code
 152  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 153  * unknown.
 154  *
 155  * @author  unascribed
 156  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 157  * @since   1.0
 158  */
 159 public final class Class<T> implements java.io.Serializable,
 160                               GenericDeclaration,
 161                               Type,
 162                               AnnotatedElement,


 359      * obtain any of the {@code Class} objects representing primitive
 360      * types or void.
 361      *
 362      * <p> If {@code name} denotes an array class, the component type of
 363      * the array class is loaded but not initialized.
 364      *
 365      * <p> For example, in an instance method the expression:
 366      *
 367      * <blockquote>
 368      *  {@code Class.forName("Foo")}
 369      * </blockquote>
 370      *
 371      * is equivalent to:
 372      *
 373      * <blockquote>
 374      *  {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
 375      * </blockquote>
 376      *
 377      * Note that this method throws errors related to loading, linking
 378      * or initializing as specified in Sections {@jls 12.2}, {@jls
 379      * 12.3}, and {@jls 12.4} of <em>The Java Language
 380      * Specification</em>.
 381      * Note that this method does not check whether the requested class
 382      * is accessible to its caller.
 383      *
 384      * @param name       fully qualified name of the desired class
 385      * @param initialize if {@code true} the class will be initialized (which implies linking).
 386      *                   See Section {@jls 12.4} of <em>The Java Language Specification</em>.
 387      * @param loader     class loader from which the class must be loaded
 388      * @return           class object representing the desired class
 389      *
 390      * @throws    LinkageError if the linkage fails
 391      * @throws    ExceptionInInitializerError if the initialization provoked
 392      *            by this method fails
 393      * @throws    ClassNotFoundException if the class cannot be located by
 394      *            the specified class loader
 395      * @throws    SecurityException
 396      *            if a security manager is present, and the {@code loader} is
 397      *            {@code null}, and the caller's class loader is not
 398      *            {@code null}, and the caller does not have the
 399      *            {@link RuntimePermission}{@code ("getClassLoader")}
 400      *
 401      * @see       java.lang.Class#forName(String)
 402      * @see       java.lang.ClassLoader
 403      *
 404      * @jls 12.2 Loading of Classes and Interfaces
 405      * @jls 12.3 Linking of Classes and Interfaces
 406      * @jls 12.4 Initialization of Classes and Interfaces


 643      * @since 1.1
 644      */
 645     @HotSpotIntrinsicCandidate
 646     public native boolean isInstance(Object obj);
 647 
 648 
 649     /**
 650      * Determines if the class or interface represented by this
 651      * {@code Class} object is either the same as, or is a superclass or
 652      * superinterface of, the class or interface represented by the specified
 653      * {@code Class} parameter. It returns {@code true} if so;
 654      * otherwise it returns {@code false}. If this {@code Class}
 655      * object represents a primitive type, this method returns
 656      * {@code true} if the specified {@code Class} parameter is
 657      * exactly this {@code Class} object; otherwise it returns
 658      * {@code false}.
 659      *
 660      * <p> Specifically, this method tests whether the type represented by the
 661      * specified {@code Class} parameter can be converted to the type
 662      * represented by this {@code Class} object via an identity conversion
 663      * or via a widening reference conversion. See <em>The Java Language
 664      * Specification</em>, sections {@jls 5.1.1} and {@jls 5.1.4},
 665      * for details.
 666      *
 667      * @param     cls the {@code Class} object to be checked
 668      * @return    the {@code boolean} value indicating whether objects of the
 669      *            type {@code cls} can be assigned to objects of this class
 670      * @throws    NullPointerException if the specified Class parameter is
 671      *            null.
 672      * @since     1.1
 673      */
 674     @HotSpotIntrinsicCandidate
 675     public native boolean isAssignableFrom(Class<?> cls);
 676 
 677 
 678     /**
 679      * Determines if the specified {@code Class} object represents an
 680      * interface type.
 681      *
 682      * @return  {@code true} if this object represents an interface;
 683      *          {@code false} otherwise.
 684      */


 728     @HotSpotIntrinsicCandidate
 729     public native boolean isPrimitive();
 730 
 731     /**
 732      * Returns true if this {@code Class} object represents an annotation
 733      * type.  Note that if this method returns true, {@link #isInterface()}
 734      * would also return true, as all annotation types are also interfaces.
 735      *
 736      * @return {@code true} if this class object represents an annotation
 737      *      type; {@code false} otherwise
 738      * @since 1.5
 739      */
 740     public boolean isAnnotation() {
 741         return (getModifiers() & ANNOTATION) != 0;
 742     }
 743 
 744     /**
 745      * Returns {@code true} if this class is a synthetic class;
 746      * returns {@code false} otherwise.
 747      * @return {@code true} if and only if this class is a synthetic class as
 748      *         defined by the Java Language Specification.
 749      * @jls 13.1 The Form of a Binary
 750      * @since 1.5
 751      */
 752     public boolean isSynthetic() {
 753         return (getModifiers() & SYNTHETIC) != 0;
 754     }
 755 
 756     /**
 757      * Returns the  name of the entity (class, interface, array class,
 758      * primitive type, or void) represented by this {@code Class} object,
 759      * as a {@code String}.
 760      *
 761      * <p> If this class object represents a reference type that is not an
 762      * array type then the binary name of the class is returned, as specified
 763      * by
 764      * <cite>The Java&trade; Language Specification</cite>.
 765      *
 766      * <p> If this class object represents a primitive type or void, then the
 767      * name returned is a {@code String} equal to the Java language
 768      * keyword corresponding to the primitive type or void.


 879 
 880     // set by VM
 881     private transient Module module;
 882 
 883     // Initialized in JVM not by private constructor
 884     // This field is filtered from reflection access, i.e. getDeclaredField
 885     // will throw NoSuchFieldException
 886     private final ClassLoader classLoader;
 887 
 888     /**
 889      * Returns an array of {@code TypeVariable} objects that represent the
 890      * type variables declared by the generic declaration represented by this
 891      * {@code GenericDeclaration} object, in declaration order.  Returns an
 892      * array of length 0 if the underlying generic declaration declares no type
 893      * variables.
 894      *
 895      * @return an array of {@code TypeVariable} objects that represent
 896      *     the type variables declared by this generic declaration
 897      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
 898      *     signature of this generic declaration does not conform to
 899      *     the format specified in
 900      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 901      * @since 1.5
 902      */
 903     @SuppressWarnings("unchecked")
 904     public TypeVariable<Class<T>>[] getTypeParameters() {
 905         ClassRepository info = getGenericInfo();
 906         if (info != null)
 907             return (TypeVariable<Class<T>>[])info.getTypeParameters();
 908         else
 909             return (TypeVariable<Class<T>>[])new TypeVariable<?>[0];
 910     }
 911 
 912 
 913     /**
 914      * Returns the {@code Class} representing the direct superclass of the
 915      * entity (class, interface, primitive type or void) represented by
 916      * this {@code Class}.  If this {@code Class} represents either the
 917      * {@code Object} class, an interface, a primitive type, or void, then
 918      * null is returned.  If this object represents an array class then the
 919      * {@code Class} object representing the {@code Object} class is
 920      * returned.


 928     /**
 929      * Returns the {@code Type} representing the direct superclass of
 930      * the entity (class, interface, primitive type or void) represented by
 931      * this {@code Class}.
 932      *
 933      * <p>If the superclass is a parameterized type, the {@code Type}
 934      * object returned must accurately reflect the actual type
 935      * arguments used in the source code. The parameterized type
 936      * representing the superclass is created if it had not been
 937      * created before. See the declaration of {@link
 938      * java.lang.reflect.ParameterizedType ParameterizedType} for the
 939      * semantics of the creation process for parameterized types.  If
 940      * this {@code Class} represents either the {@code Object}
 941      * class, an interface, a primitive type, or void, then null is
 942      * returned.  If this object represents an array class then the
 943      * {@code Class} object representing the {@code Object} class is
 944      * returned.
 945      *
 946      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
 947      *     class signature does not conform to the format specified in

 948      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 949      * @throws TypeNotPresentException if the generic superclass
 950      *     refers to a non-existent type declaration
 951      * @throws java.lang.reflect.MalformedParameterizedTypeException if the
 952      *     generic superclass refers to a parameterized type that cannot be
 953      *     instantiated  for any reason
 954      * @return the direct superclass of the class represented by this object
 955      * @since 1.5
 956      */
 957     public Type getGenericSuperclass() {
 958         ClassRepository info = getGenericInfo();
 959         if (info == null) {
 960             return getSuperclass();
 961         }
 962 
 963         // Historical irregularity:
 964         // Generic signature marks interfaces with superclass = Object
 965         // but this API returns null for interfaces
 966         if (isInterface()) {
 967             return null;


1126      * the declaration of the class represented by this object.
1127      *
1128      * <p>If this object represents an interface, the array contains objects
1129      * representing all interfaces directly extended by the interface.  The
1130      * order of the interface objects in the array corresponds to the order of
1131      * the interface names in the {@code extends} clause of the declaration of
1132      * the interface represented by this object.
1133      *
1134      * <p>If this object represents a class or interface that implements no
1135      * interfaces, the method returns an array of length 0.
1136      *
1137      * <p>If this object represents a primitive type or void, the method
1138      * returns an array of length 0.
1139      *
1140      * <p>If this {@code Class} object represents an array type, the
1141      * interfaces {@code Cloneable} and {@code java.io.Serializable} are
1142      * returned in that order.
1143      *
1144      * @throws java.lang.reflect.GenericSignatureFormatError
1145      *     if the generic class signature does not conform to the format
1146      *     specified in
1147      *     <cite>The Java&trade; Virtual Machine Specification</cite>
1148      * @throws TypeNotPresentException if any of the generic
1149      *     superinterfaces refers to a non-existent type declaration
1150      * @throws java.lang.reflect.MalformedParameterizedTypeException
1151      *     if any of the generic superinterfaces refer to a parameterized
1152      *     type that cannot be instantiated for any reason
1153      * @return an array of interfaces directly implemented by this class
1154      * @since 1.5
1155      */
1156     public Type[] getGenericInterfaces() {
1157         ClassRepository info = getGenericInfo();
1158         return (info == null) ?  getInterfaces() : info.getSuperInterfaces();
1159     }
1160 
1161 
1162     /**
1163      * Returns the {@code Class} representing the component type of an
1164      * array.  If this class does not represent an array class this method
1165      * returns null.
1166      *


1184     /**
1185      * Returns the Java language modifiers for this class or interface, encoded
1186      * in an integer. The modifiers consist of the Java Virtual Machine's
1187      * constants for {@code public}, {@code protected},
1188      * {@code private}, {@code final}, {@code static},
1189      * {@code abstract} and {@code interface}; they should be decoded
1190      * using the methods of class {@code Modifier}.
1191      *
1192      * <p> If the underlying class is an array class, then its
1193      * {@code public}, {@code private} and {@code protected}
1194      * modifiers are the same as those of its component type.  If this
1195      * {@code Class} represents a primitive type or void, its
1196      * {@code public} modifier is always {@code true}, and its
1197      * {@code protected} and {@code private} modifiers are always
1198      * {@code false}. If this object represents an array class, a
1199      * primitive type or void, then its {@code final} modifier is always
1200      * {@code true} and its interface modifier is always
1201      * {@code false}. The values of its other modifiers are not determined
1202      * by this specification.
1203      *
1204      * <p> The modifier encodings are defined in <em>The Java Virtual Machine
1205      * Specification</em>, table 4.1.
1206      *
1207      * @return the {@code int} representing the modifiers for this class
1208      * @see     java.lang.reflect.Modifier
1209      * @since 1.1
1210      */
1211     @HotSpotIntrinsicCandidate
1212     public native int getModifiers();
1213 
1214 
1215     /**
1216      * Gets the signers of this class.
1217      *
1218      * @return  the signers of this class, or null if there are no signers.  In
1219      *          particular, this method returns null if this object represents
1220      *          a primitive type or void.
1221      * @since   1.1
1222      */
1223     public native Object[] getSigners();
1224 
1225 


1594      * @return an informative string for the name of this type
1595      * @since 1.8
1596      */
1597     public String getTypeName() {
1598         if (isArray()) {
1599             try {
1600                 Class<?> cl = this;
1601                 int dimensions = 0;
1602                 do {
1603                     dimensions++;
1604                     cl = cl.getComponentType();
1605                 } while (cl.isArray());
1606                 return cl.getName() + "[]".repeat(dimensions);
1607             } catch (Throwable e) { /*FALLTHRU*/ }
1608         }
1609         return getName();
1610     }
1611 
1612     /**
1613      * Returns the canonical name of the underlying class as
1614      * defined by the Java Language Specification.  Returns null if
1615      * the underlying class does not have a canonical name (i.e., if
1616      * it is a local or anonymous class or an array whose component
1617      * type does not have a canonical name).
1618      * @return the canonical name of the underlying class if it exists, and
1619      * {@code null} otherwise.
1620      * @since 1.5
1621      */
1622     public String getCanonicalName() {
1623         ReflectionData<T> rd = reflectionData();
1624         String canonicalName = rd.canonicalName;
1625         if (canonicalName == null) {
1626             rd.canonicalName = canonicalName = getCanonicalName0();
1627         }
1628         return canonicalName == ReflectionData.NULL_SENTINEL? null : canonicalName;
1629     }
1630 
1631     private String getCanonicalName0() {
1632         if (isArray()) {
1633             String canonicalName = getComponentType().getCanonicalName();
1634             if (canonicalName != null)


1911      * @jls 8.4 Method Declarations
1912      * @since 1.1
1913      */
1914     @CallerSensitive
1915     public Method[] getMethods() throws SecurityException {
1916         SecurityManager sm = System.getSecurityManager();
1917         if (sm != null) {
1918             checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
1919         }
1920         return copyMethods(privateGetPublicMethods());
1921     }
1922 
1923 
1924     /**
1925      * Returns an array containing {@code Constructor} objects reflecting
1926      * all the public constructors of the class represented by this
1927      * {@code Class} object.  An array of length 0 is returned if the
1928      * class has no public constructors, or if the class is an array class, or
1929      * if the class reflects a primitive type or void.
1930      *
1931      * Note that while this method returns an array of {@code

1932      * Constructor<T>} objects (that is an array of constructors from
1933      * this class), the return type of this method is {@code
1934      * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
1935      * might be expected.  This less informative return type is
1936      * necessary since after being returned from this method, the
1937      * array could be modified to hold {@code Constructor} objects for
1938      * different classes, which would violate the type guarantees of
1939      * {@code Constructor<T>[]}.
1940      *
1941      * @return the array of {@code Constructor} objects representing the
1942      *         public constructors of this class
1943      * @throws SecurityException
1944      *         If a security manager, <i>s</i>, is present and
1945      *         the caller's class loader is not the same as or an
1946      *         ancestor of the class loader for the current class and
1947      *         invocation of {@link SecurityManager#checkPackageAccess
1948      *         s.checkPackageAccess()} denies access to the package
1949      *         of this class.
1950      *
1951      * @since 1.1


2384     public Method[] getDeclaredMethods() throws SecurityException {
2385         SecurityManager sm = System.getSecurityManager();
2386         if (sm != null) {
2387             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2388         }
2389         return copyMethods(privateGetDeclaredMethods(false));
2390     }
2391 
2392 
2393     /**
2394      * Returns an array of {@code Constructor} objects reflecting all the
2395      * constructors declared by the class represented by this
2396      * {@code Class} object. These are public, protected, default
2397      * (package) access, and private constructors.  The elements in the array
2398      * returned are not sorted and are not in any particular order.  If the
2399      * class has a default constructor, it is included in the returned array.
2400      * This method returns an array of length 0 if this {@code Class}
2401      * object represents an interface, a primitive type, an array class, or
2402      * void.
2403      *
2404      * <p> See <em>The Java Language Specification</em>, section {@jls 8.2}.
2405      *
2406      * @return  the array of {@code Constructor} objects representing all the
2407      *          declared constructors of this class
2408      * @throws  SecurityException
2409      *          If a security manager, <i>s</i>, is present and any of the
2410      *          following conditions is met:
2411      *
2412      *          <ul>
2413      *
2414      *          <li> the caller's class loader is not the same as the
2415      *          class loader of this class and invocation of
2416      *          {@link SecurityManager#checkPermission
2417      *          s.checkPermission} method with
2418      *          {@code RuntimePermission("accessDeclaredMembers")}
2419      *          denies access to the declared constructors within this class
2420      *
2421      *          <li> the caller's class loader is not the same as or an
2422      *          ancestor of the class loader for the current class and
2423      *          invocation of {@link SecurityManager#checkPackageAccess
2424      *          s.checkPackageAccess()} denies access to the package


3521      * is written into the stream. Future references to the class descriptor
3522      * are written as references to the initial class descriptor instance.
3523      *
3524      * @see java.io.ObjectStreamClass
3525      */
3526     @java.io.Serial
3527     private static final ObjectStreamField[] serialPersistentFields =
3528         new ObjectStreamField[0];
3529 
3530 
3531     /**
3532      * Returns the assertion status that would be assigned to this
3533      * class if it were to be initialized at the time this method is invoked.
3534      * If this class has had its assertion status set, the most recent
3535      * setting will be returned; otherwise, if any package default assertion
3536      * status pertains to this class, the most recent setting for the most
3537      * specific pertinent package default assertion status is returned;
3538      * otherwise, if this class is not a system class (i.e., it has a
3539      * class loader) its class loader's default assertion status is returned;
3540      * otherwise, the system class default assertion status is returned.
3541      * <p>

3542      * Few programmers will have any need for this method; it is provided
3543      * for the benefit of the JRE itself.  (It allows a class to determine at
3544      * the time that it is initialized whether assertions should be enabled.)
3545      * Note that this method is not guaranteed to return the actual
3546      * assertion status that was (or will be) associated with the specified
3547      * class when it was (or will be) initialized.
3548      *
3549      * @return the desired assertion status of the specified class.
3550      * @see    java.lang.ClassLoader#setClassAssertionStatus
3551      * @see    java.lang.ClassLoader#setPackageAssertionStatus
3552      * @see    java.lang.ClassLoader#setDefaultAssertionStatus
3553      * @since  1.4
3554      */
3555     public boolean desiredAssertionStatus() {
3556         ClassLoader loader = getClassLoader0();
3557         // If the loader is null this is a system class, so ask the VM
3558         if (loader == null)
3559             return desiredAssertionStatus0(this);
3560 
3561         // If the classloader has been initialized with the assertion
3562         // directives, ask it. Otherwise, ask the VM.
3563         synchronized(loader.assertionLock) {


4008     /**
4009      * Returns the nest host of the <a href=#nest>nest</a> to which the class
4010      * or interface represented by this {@code Class} object belongs.
4011      * Every class and interface is a member of exactly one nest.
4012      * A class or interface that is not recorded as belonging to a nest
4013      * belongs to the nest consisting only of itself, and is the nest
4014      * host.
4015      *
4016      * <p>Each of the {@code Class} objects representing array types,
4017      * primitive types, and {@code void} returns {@code this} to indicate
4018      * that the represented entity belongs to the nest consisting only of
4019      * itself, and is the nest host.
4020      *
4021      * <p>If there is a {@linkplain LinkageError linkage error} accessing
4022      * the nest host, or if this class or interface is not enumerated as
4023      * a member of the nest by the nest host, then it is considered to belong
4024      * to its own nest and {@code this} is returned as the host.
4025      *
4026      * @apiNote A {@code class} file of version 55.0 or greater may record the
4027      * host of the nest to which it belongs by using the {@code NestHost}
4028      * attribute (JVMS 4.7.28). Alternatively, a {@code class} file of
4029      * version 55.0 or greater may act as a nest host by enumerating the nest's
4030      * other members with the
4031      * {@code NestMembers} attribute (JVMS 4.7.29).
4032      * A {@code class} file of version 54.0 or lower does not use these
4033      * attributes.
4034      *
4035      * @return the nest host of this class or interface
4036      *
4037      * @throws SecurityException
4038      *         If the returned class is not the current class, and
4039      *         if a security manager, <i>s</i>, is present and the caller's
4040      *         class loader is not the same as or an ancestor of the class
4041      *         loader for the returned class and invocation of {@link
4042      *         SecurityManager#checkPackageAccess s.checkPackageAccess()}
4043      *         denies access to the package of the returned class
4044      * @since 11
4045      * @jvms 4.7.28 The {@code NestHost} Attribute
4046      * @jvms 4.7.29 The {@code NestMembers} Attribute
4047      * @jvms 5.4.4 Access Control
4048      */
4049     @CallerSensitive
4050     public Class<?> getNestHost() {
4051         if (isPrimitive() || isArray()) {




 122  * <em>nest host</em>, and enumerates the other nestmates which
 123  * belong to the nest; each of them in turn records it as the nest host.
 124  * The classes and interfaces which belong to a nest, including its host, are
 125  * determined when
 126  * {@code class} files are generated, for example, a Java compiler
 127  * will typically record a top-level class as the host of a nest where the
 128  * other members are the classes and interfaces whose declarations are
 129  * enclosed within the top-level class declaration.
 130  *
 131  * <p> The following example uses a {@code Class} object to print the
 132  * class name of an object:
 133  *
 134  * <blockquote><pre>
 135  *     void printClassName(Object obj) {
 136  *         System.out.println("The class of " + obj +
 137  *                            " is " + obj.getClass().getName());
 138  *     }
 139  * </pre></blockquote>
 140  *
 141  * <p> It is also possible to get the {@code Class} object for a named
 142  * type (or for void) using a class literal.  See Section {@jls 15.8.2} of
 143  * <cite>The Java&trade; Language Specification</cite>.
 144  * For example:
 145  *
 146  * <blockquote>
 147  *     {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
 148  * </blockquote>
 149  *
 150  * @param <T> the type of the class modeled by this {@code Class}
 151  * object.  For example, the type of {@code String.class} is {@code
 152  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 153  * unknown.
 154  *
 155  * @author  unascribed
 156  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 157  * @since   1.0
 158  */
 159 public final class Class<T> implements java.io.Serializable,
 160                               GenericDeclaration,
 161                               Type,
 162                               AnnotatedElement,


 359      * obtain any of the {@code Class} objects representing primitive
 360      * types or void.
 361      *
 362      * <p> If {@code name} denotes an array class, the component type of
 363      * the array class is loaded but not initialized.
 364      *
 365      * <p> For example, in an instance method the expression:
 366      *
 367      * <blockquote>
 368      *  {@code Class.forName("Foo")}
 369      * </blockquote>
 370      *
 371      * is equivalent to:
 372      *
 373      * <blockquote>
 374      *  {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
 375      * </blockquote>
 376      *
 377      * Note that this method throws errors related to loading, linking
 378      * or initializing as specified in Sections {@jls 12.2}, {@jls
 379      * 12.3}, and {@jls 12.4} of <cite>The Java&trade; Language
 380      * Specification</cite>.
 381      * Note that this method does not check whether the requested class
 382      * is accessible to its caller.
 383      *
 384      * @param name       fully qualified name of the desired class
 385      * @param initialize if {@code true} the class will be initialized (which implies linking).
 386      *                   See Section {@jls 12.4} of <cite>The Java&trade; Language Specification</cite>.
 387      * @param loader     class loader from which the class must be loaded
 388      * @return           class object representing the desired class
 389      *
 390      * @throws    LinkageError if the linkage fails
 391      * @throws    ExceptionInInitializerError if the initialization provoked
 392      *            by this method fails
 393      * @throws    ClassNotFoundException if the class cannot be located by
 394      *            the specified class loader
 395      * @throws    SecurityException
 396      *            if a security manager is present, and the {@code loader} is
 397      *            {@code null}, and the caller's class loader is not
 398      *            {@code null}, and the caller does not have the
 399      *            {@link RuntimePermission}{@code ("getClassLoader")}
 400      *
 401      * @see       java.lang.Class#forName(String)
 402      * @see       java.lang.ClassLoader
 403      *
 404      * @jls 12.2 Loading of Classes and Interfaces
 405      * @jls 12.3 Linking of Classes and Interfaces
 406      * @jls 12.4 Initialization of Classes and Interfaces


 643      * @since 1.1
 644      */
 645     @HotSpotIntrinsicCandidate
 646     public native boolean isInstance(Object obj);
 647 
 648 
 649     /**
 650      * Determines if the class or interface represented by this
 651      * {@code Class} object is either the same as, or is a superclass or
 652      * superinterface of, the class or interface represented by the specified
 653      * {@code Class} parameter. It returns {@code true} if so;
 654      * otherwise it returns {@code false}. If this {@code Class}
 655      * object represents a primitive type, this method returns
 656      * {@code true} if the specified {@code Class} parameter is
 657      * exactly this {@code Class} object; otherwise it returns
 658      * {@code false}.
 659      *
 660      * <p> Specifically, this method tests whether the type represented by the
 661      * specified {@code Class} parameter can be converted to the type
 662      * represented by this {@code Class} object via an identity conversion
 663      * or via a widening reference conversion. See <cite>The Java&trade; Language
 664      * Specification</cite>, sections {@jls 5.1.1} and {@jls 5.1.4},
 665      * for details.
 666      *
 667      * @param     cls the {@code Class} object to be checked
 668      * @return    the {@code boolean} value indicating whether objects of the
 669      *            type {@code cls} can be assigned to objects of this class
 670      * @throws    NullPointerException if the specified Class parameter is
 671      *            null.
 672      * @since     1.1
 673      */
 674     @HotSpotIntrinsicCandidate
 675     public native boolean isAssignableFrom(Class<?> cls);
 676 
 677 
 678     /**
 679      * Determines if the specified {@code Class} object represents an
 680      * interface type.
 681      *
 682      * @return  {@code true} if this object represents an interface;
 683      *          {@code false} otherwise.
 684      */


 728     @HotSpotIntrinsicCandidate
 729     public native boolean isPrimitive();
 730 
 731     /**
 732      * Returns true if this {@code Class} object represents an annotation
 733      * type.  Note that if this method returns true, {@link #isInterface()}
 734      * would also return true, as all annotation types are also interfaces.
 735      *
 736      * @return {@code true} if this class object represents an annotation
 737      *      type; {@code false} otherwise
 738      * @since 1.5
 739      */
 740     public boolean isAnnotation() {
 741         return (getModifiers() & ANNOTATION) != 0;
 742     }
 743 
 744     /**
 745      * Returns {@code true} if this class is a synthetic class;
 746      * returns {@code false} otherwise.
 747      * @return {@code true} if and only if this class is a synthetic class as
 748      *         defined by <cite>The Java&trade; Language Specification</cite>.
 749      * @jls 13.1 The Form of a Binary
 750      * @since 1.5
 751      */
 752     public boolean isSynthetic() {
 753         return (getModifiers() & SYNTHETIC) != 0;
 754     }
 755 
 756     /**
 757      * Returns the  name of the entity (class, interface, array class,
 758      * primitive type, or void) represented by this {@code Class} object,
 759      * as a {@code String}.
 760      *
 761      * <p> If this class object represents a reference type that is not an
 762      * array type then the binary name of the class is returned, as specified
 763      * by
 764      * <cite>The Java&trade; Language Specification</cite>.
 765      *
 766      * <p> If this class object represents a primitive type or void, then the
 767      * name returned is a {@code String} equal to the Java language
 768      * keyword corresponding to the primitive type or void.


 879 
 880     // set by VM
 881     private transient Module module;
 882 
 883     // Initialized in JVM not by private constructor
 884     // This field is filtered from reflection access, i.e. getDeclaredField
 885     // will throw NoSuchFieldException
 886     private final ClassLoader classLoader;
 887 
 888     /**
 889      * Returns an array of {@code TypeVariable} objects that represent the
 890      * type variables declared by the generic declaration represented by this
 891      * {@code GenericDeclaration} object, in declaration order.  Returns an
 892      * array of length 0 if the underlying generic declaration declares no type
 893      * variables.
 894      *
 895      * @return an array of {@code TypeVariable} objects that represent
 896      *     the type variables declared by this generic declaration
 897      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
 898      *     signature of this generic declaration does not conform to
 899      *     the format specified in section {@jvms 4.7.9} of the
 900      *     <cite>The Java&trade; Virtual Machine Specification</cite>, 
 901      * @since 1.5
 902      */
 903     @SuppressWarnings("unchecked")
 904     public TypeVariable<Class<T>>[] getTypeParameters() {
 905         ClassRepository info = getGenericInfo();
 906         if (info != null)
 907             return (TypeVariable<Class<T>>[])info.getTypeParameters();
 908         else
 909             return (TypeVariable<Class<T>>[])new TypeVariable<?>[0];
 910     }
 911 
 912 
 913     /**
 914      * Returns the {@code Class} representing the direct superclass of the
 915      * entity (class, interface, primitive type or void) represented by
 916      * this {@code Class}.  If this {@code Class} represents either the
 917      * {@code Object} class, an interface, a primitive type, or void, then
 918      * null is returned.  If this object represents an array class then the
 919      * {@code Class} object representing the {@code Object} class is
 920      * returned.


 928     /**
 929      * Returns the {@code Type} representing the direct superclass of
 930      * the entity (class, interface, primitive type or void) represented by
 931      * this {@code Class}.
 932      *
 933      * <p>If the superclass is a parameterized type, the {@code Type}
 934      * object returned must accurately reflect the actual type
 935      * arguments used in the source code. The parameterized type
 936      * representing the superclass is created if it had not been
 937      * created before. See the declaration of {@link
 938      * java.lang.reflect.ParameterizedType ParameterizedType} for the
 939      * semantics of the creation process for parameterized types.  If
 940      * this {@code Class} represents either the {@code Object}
 941      * class, an interface, a primitive type, or void, then null is
 942      * returned.  If this object represents an array class then the
 943      * {@code Class} object representing the {@code Object} class is
 944      * returned.
 945      *
 946      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
 947      *     class signature does not conform to the format specified in
 948      *     section {@jvms 4.7.9} of the
 949      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 950      * @throws TypeNotPresentException if the generic superclass
 951      *     refers to a non-existent type declaration
 952      * @throws java.lang.reflect.MalformedParameterizedTypeException if the
 953      *     generic superclass refers to a parameterized type that cannot be
 954      *     instantiated  for any reason
 955      * @return the direct superclass of the class represented by this object
 956      * @since 1.5
 957      */
 958     public Type getGenericSuperclass() {
 959         ClassRepository info = getGenericInfo();
 960         if (info == null) {
 961             return getSuperclass();
 962         }
 963 
 964         // Historical irregularity:
 965         // Generic signature marks interfaces with superclass = Object
 966         // but this API returns null for interfaces
 967         if (isInterface()) {
 968             return null;


1127      * the declaration of the class represented by this object.
1128      *
1129      * <p>If this object represents an interface, the array contains objects
1130      * representing all interfaces directly extended by the interface.  The
1131      * order of the interface objects in the array corresponds to the order of
1132      * the interface names in the {@code extends} clause of the declaration of
1133      * the interface represented by this object.
1134      *
1135      * <p>If this object represents a class or interface that implements no
1136      * interfaces, the method returns an array of length 0.
1137      *
1138      * <p>If this object represents a primitive type or void, the method
1139      * returns an array of length 0.
1140      *
1141      * <p>If this {@code Class} object represents an array type, the
1142      * interfaces {@code Cloneable} and {@code java.io.Serializable} are
1143      * returned in that order.
1144      *
1145      * @throws java.lang.reflect.GenericSignatureFormatError
1146      *     if the generic class signature does not conform to the format
1147      *     specified in section {@jvms 4.7.9} of the
1148      *     <cite>The Java&trade; Virtual Machine Specification</cite>
1149      * @throws TypeNotPresentException if any of the generic
1150      *     superinterfaces refers to a non-existent type declaration
1151      * @throws java.lang.reflect.MalformedParameterizedTypeException
1152      *     if any of the generic superinterfaces refer to a parameterized
1153      *     type that cannot be instantiated for any reason
1154      * @return an array of interfaces directly implemented by this class
1155      * @since 1.5
1156      */
1157     public Type[] getGenericInterfaces() {
1158         ClassRepository info = getGenericInfo();
1159         return (info == null) ?  getInterfaces() : info.getSuperInterfaces();
1160     }
1161 
1162 
1163     /**
1164      * Returns the {@code Class} representing the component type of an
1165      * array.  If this class does not represent an array class this method
1166      * returns null.
1167      *


1185     /**
1186      * Returns the Java language modifiers for this class or interface, encoded
1187      * in an integer. The modifiers consist of the Java Virtual Machine's
1188      * constants for {@code public}, {@code protected},
1189      * {@code private}, {@code final}, {@code static},
1190      * {@code abstract} and {@code interface}; they should be decoded
1191      * using the methods of class {@code Modifier}.
1192      *
1193      * <p> If the underlying class is an array class, then its
1194      * {@code public}, {@code private} and {@code protected}
1195      * modifiers are the same as those of its component type.  If this
1196      * {@code Class} represents a primitive type or void, its
1197      * {@code public} modifier is always {@code true}, and its
1198      * {@code protected} and {@code private} modifiers are always
1199      * {@code false}. If this object represents an array class, a
1200      * primitive type or void, then its {@code final} modifier is always
1201      * {@code true} and its interface modifier is always
1202      * {@code false}. The values of its other modifiers are not determined
1203      * by this specification.
1204      *
1205      * <p> The modifier encodings are defined in section {@jvms 4.1} of the 
1206      * <cite>The Java&trade; Virtual Machine Specification</cite>.
1207      *
1208      * @return the {@code int} representing the modifiers for this class
1209      * @see     java.lang.reflect.Modifier
1210      * @since 1.1
1211      */
1212     @HotSpotIntrinsicCandidate
1213     public native int getModifiers();
1214 
1215 
1216     /**
1217      * Gets the signers of this class.
1218      *
1219      * @return  the signers of this class, or null if there are no signers.  In
1220      *          particular, this method returns null if this object represents
1221      *          a primitive type or void.
1222      * @since   1.1
1223      */
1224     public native Object[] getSigners();
1225 
1226 


1595      * @return an informative string for the name of this type
1596      * @since 1.8
1597      */
1598     public String getTypeName() {
1599         if (isArray()) {
1600             try {
1601                 Class<?> cl = this;
1602                 int dimensions = 0;
1603                 do {
1604                     dimensions++;
1605                     cl = cl.getComponentType();
1606                 } while (cl.isArray());
1607                 return cl.getName() + "[]".repeat(dimensions);
1608             } catch (Throwable e) { /*FALLTHRU*/ }
1609         }
1610         return getName();
1611     }
1612 
1613     /**
1614      * Returns the canonical name of the underlying class as
1615      * defined by <cite>The Java&trade; Language Specification</cite>, section {@jls 6.7}.  Returns null if
1616      * the underlying class does not have a canonical name (i.e., if
1617      * it is a local or anonymous class or an array whose component
1618      * type does not have a canonical name).
1619      * @return the canonical name of the underlying class if it exists, and
1620      * {@code null} otherwise.
1621      * @since 1.5
1622      */
1623     public String getCanonicalName() {
1624         ReflectionData<T> rd = reflectionData();
1625         String canonicalName = rd.canonicalName;
1626         if (canonicalName == null) {
1627             rd.canonicalName = canonicalName = getCanonicalName0();
1628         }
1629         return canonicalName == ReflectionData.NULL_SENTINEL? null : canonicalName;
1630     }
1631 
1632     private String getCanonicalName0() {
1633         if (isArray()) {
1634             String canonicalName = getComponentType().getCanonicalName();
1635             if (canonicalName != null)


1912      * @jls 8.4 Method Declarations
1913      * @since 1.1
1914      */
1915     @CallerSensitive
1916     public Method[] getMethods() throws SecurityException {
1917         SecurityManager sm = System.getSecurityManager();
1918         if (sm != null) {
1919             checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
1920         }
1921         return copyMethods(privateGetPublicMethods());
1922     }
1923 
1924 
1925     /**
1926      * Returns an array containing {@code Constructor} objects reflecting
1927      * all the public constructors of the class represented by this
1928      * {@code Class} object.  An array of length 0 is returned if the
1929      * class has no public constructors, or if the class is an array class, or
1930      * if the class reflects a primitive type or void.
1931      *
1932      * @apiNote
1933      * While this method returns an array of {@code
1934      * Constructor<T>} objects (that is an array of constructors from
1935      * this class), the return type of this method is {@code
1936      * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
1937      * might be expected.  This less informative return type is
1938      * necessary since after being returned from this method, the
1939      * array could be modified to hold {@code Constructor} objects for
1940      * different classes, which would violate the type guarantees of
1941      * {@code Constructor<T>[]}.
1942      *
1943      * @return the array of {@code Constructor} objects representing the
1944      *         public constructors of this class
1945      * @throws SecurityException
1946      *         If a security manager, <i>s</i>, is present and
1947      *         the caller's class loader is not the same as or an
1948      *         ancestor of the class loader for the current class and
1949      *         invocation of {@link SecurityManager#checkPackageAccess
1950      *         s.checkPackageAccess()} denies access to the package
1951      *         of this class.
1952      *
1953      * @since 1.1


2386     public Method[] getDeclaredMethods() throws SecurityException {
2387         SecurityManager sm = System.getSecurityManager();
2388         if (sm != null) {
2389             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2390         }
2391         return copyMethods(privateGetDeclaredMethods(false));
2392     }
2393 
2394 
2395     /**
2396      * Returns an array of {@code Constructor} objects reflecting all the
2397      * constructors declared by the class represented by this
2398      * {@code Class} object. These are public, protected, default
2399      * (package) access, and private constructors.  The elements in the array
2400      * returned are not sorted and are not in any particular order.  If the
2401      * class has a default constructor, it is included in the returned array.
2402      * This method returns an array of length 0 if this {@code Class}
2403      * object represents an interface, a primitive type, an array class, or
2404      * void.
2405      *
2406      * <p> See <cite>The Java&trade; Language Specification</cite>, section {@jls 8.2}.
2407      *
2408      * @return  the array of {@code Constructor} objects representing all the
2409      *          declared constructors of this class
2410      * @throws  SecurityException
2411      *          If a security manager, <i>s</i>, is present and any of the
2412      *          following conditions is met:
2413      *
2414      *          <ul>
2415      *
2416      *          <li> the caller's class loader is not the same as the
2417      *          class loader of this class and invocation of
2418      *          {@link SecurityManager#checkPermission
2419      *          s.checkPermission} method with
2420      *          {@code RuntimePermission("accessDeclaredMembers")}
2421      *          denies access to the declared constructors within this class
2422      *
2423      *          <li> the caller's class loader is not the same as or an
2424      *          ancestor of the class loader for the current class and
2425      *          invocation of {@link SecurityManager#checkPackageAccess
2426      *          s.checkPackageAccess()} denies access to the package


3523      * is written into the stream. Future references to the class descriptor
3524      * are written as references to the initial class descriptor instance.
3525      *
3526      * @see java.io.ObjectStreamClass
3527      */
3528     @java.io.Serial
3529     private static final ObjectStreamField[] serialPersistentFields =
3530         new ObjectStreamField[0];
3531 
3532 
3533     /**
3534      * Returns the assertion status that would be assigned to this
3535      * class if it were to be initialized at the time this method is invoked.
3536      * If this class has had its assertion status set, the most recent
3537      * setting will be returned; otherwise, if any package default assertion
3538      * status pertains to this class, the most recent setting for the most
3539      * specific pertinent package default assertion status is returned;
3540      * otherwise, if this class is not a system class (i.e., it has a
3541      * class loader) its class loader's default assertion status is returned;
3542      * otherwise, the system class default assertion status is returned.
3543      * 
3544      * @apiNote
3545      * Few programmers will have any need for this method; it is provided
3546      * for the benefit of the JDK itself.  (It allows a class to determine at
3547      * the time that it is initialized whether assertions should be enabled.)
3548      * Note that this method is not guaranteed to return the actual
3549      * assertion status that was (or will be) associated with the specified
3550      * class when it was (or will be) initialized.
3551      *
3552      * @return the desired assertion status of the specified class.
3553      * @see    java.lang.ClassLoader#setClassAssertionStatus
3554      * @see    java.lang.ClassLoader#setPackageAssertionStatus
3555      * @see    java.lang.ClassLoader#setDefaultAssertionStatus
3556      * @since  1.4
3557      */
3558     public boolean desiredAssertionStatus() {
3559         ClassLoader loader = getClassLoader0();
3560         // If the loader is null this is a system class, so ask the VM
3561         if (loader == null)
3562             return desiredAssertionStatus0(this);
3563 
3564         // If the classloader has been initialized with the assertion
3565         // directives, ask it. Otherwise, ask the VM.
3566         synchronized(loader.assertionLock) {


4011     /**
4012      * Returns the nest host of the <a href=#nest>nest</a> to which the class
4013      * or interface represented by this {@code Class} object belongs.
4014      * Every class and interface is a member of exactly one nest.
4015      * A class or interface that is not recorded as belonging to a nest
4016      * belongs to the nest consisting only of itself, and is the nest
4017      * host.
4018      *
4019      * <p>Each of the {@code Class} objects representing array types,
4020      * primitive types, and {@code void} returns {@code this} to indicate
4021      * that the represented entity belongs to the nest consisting only of
4022      * itself, and is the nest host.
4023      *
4024      * <p>If there is a {@linkplain LinkageError linkage error} accessing
4025      * the nest host, or if this class or interface is not enumerated as
4026      * a member of the nest by the nest host, then it is considered to belong
4027      * to its own nest and {@code this} is returned as the host.
4028      *
4029      * @apiNote A {@code class} file of version 55.0 or greater may record the
4030      * host of the nest to which it belongs by using the {@code NestHost}
4031      * attribute (JVMS {@jvms 4.7.28}). Alternatively, a {@code class} file of
4032      * version 55.0 or greater may act as a nest host by enumerating the nest's
4033      * other members with the
4034      * {@code NestMembers} attribute (JVMS {@jvms 4.7.29}).
4035      * A {@code class} file of version 54.0 or lower does not use these
4036      * attributes.
4037      *
4038      * @return the nest host of this class or interface
4039      *
4040      * @throws SecurityException
4041      *         If the returned class is not the current class, and
4042      *         if a security manager, <i>s</i>, is present and the caller's
4043      *         class loader is not the same as or an ancestor of the class
4044      *         loader for the returned class and invocation of {@link
4045      *         SecurityManager#checkPackageAccess s.checkPackageAccess()}
4046      *         denies access to the package of the returned class
4047      * @since 11
4048      * @jvms 4.7.28 The {@code NestHost} Attribute
4049      * @jvms 4.7.29 The {@code NestMembers} Attribute
4050      * @jvms 5.4.4 Access Control
4051      */
4052     @CallerSensitive
4053     public Class<?> getNestHost() {
4054         if (isPrimitive() || isArray()) {


< prev index next >