< prev index next >

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

Print this page

  35 import java.io.InputStream;
  36 import java.io.ObjectStreamField;
  37 import java.lang.reflect.AnnotatedElement;
  38 import java.lang.reflect.AnnotatedType;
  39 import java.lang.reflect.AccessFlag;
  40 import java.lang.reflect.Array;
  41 import java.lang.reflect.Constructor;
  42 import java.lang.reflect.Executable;
  43 import java.lang.reflect.Field;
  44 import java.lang.reflect.GenericArrayType;
  45 import java.lang.reflect.GenericDeclaration;
  46 import java.lang.reflect.GenericSignatureFormatError;
  47 import java.lang.reflect.InvocationTargetException;
  48 import java.lang.reflect.Member;
  49 import java.lang.reflect.Method;
  50 import java.lang.reflect.Modifier;
  51 import java.lang.reflect.RecordComponent;
  52 import java.lang.reflect.Type;
  53 import java.lang.reflect.TypeVariable;
  54 import java.lang.constant.Constable;

  55 import java.net.URL;
  56 import java.security.AllPermission;
  57 import java.security.Permissions;
  58 import java.security.ProtectionDomain;
  59 import java.util.ArrayList;
  60 import java.util.Arrays;
  61 import java.util.Collection;
  62 import java.util.HashMap;
  63 import java.util.LinkedHashMap;
  64 import java.util.LinkedHashSet;
  65 import java.util.List;
  66 import java.util.Map;
  67 import java.util.Objects;
  68 import java.util.Optional;
  69 import java.util.Set;
  70 import java.util.stream.Collectors;
  71 
  72 import jdk.internal.constant.ConstantUtils;

  73 import jdk.internal.loader.BootLoader;
  74 import jdk.internal.loader.BuiltinClassLoader;

  75 import jdk.internal.misc.Unsafe;
  76 import jdk.internal.module.Resources;

  77 import jdk.internal.reflect.CallerSensitive;
  78 import jdk.internal.reflect.CallerSensitiveAdapter;
  79 import jdk.internal.reflect.ConstantPool;

  80 import jdk.internal.reflect.Reflection;
  81 import jdk.internal.reflect.ReflectionFactory;
  82 import jdk.internal.util.ModifiedUtf;
  83 import jdk.internal.vm.annotation.AOTRuntimeSetup;
  84 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
  85 import jdk.internal.vm.annotation.IntrinsicCandidate;
  86 import jdk.internal.vm.annotation.Stable;
  87 
  88 import sun.invoke.util.BytecodeDescriptor;
  89 import sun.invoke.util.Wrapper;
  90 import sun.reflect.generics.factory.CoreReflectionFactory;
  91 import sun.reflect.generics.factory.GenericsFactory;
  92 import sun.reflect.generics.repository.ClassRepository;
  93 import sun.reflect.generics.scope.ClassScope;
  94 import sun.reflect.annotation.*;
  95 
  96 /**
  97  * Instances of the class {@code Class} represent classes and
  98  * interfaces in a running Java application. An enum class and a record
  99  * class are kinds of class; an annotation interface is a kind of

 207  * {@linkplain #getTypeName type name} return results
 208  * equal to {@code "HelloWorld"}. The {@linkplain #getSimpleName
 209  * simple name} of such an implicitly declared class is {@code "HelloWorld"} and
 210  * the {@linkplain #getCanonicalName canonical name} is {@code "HelloWorld"}.
 211  *
 212  * @param <T> the type of the class modeled by this {@code Class}
 213  * object.  For example, the type of {@code String.class} is {@code
 214  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 215  * unknown.
 216  *
 217  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 218  * @since   1.0
 219  */
 220 @AOTSafeClassInitializer
 221 public final class Class<T> implements java.io.Serializable,
 222                               GenericDeclaration,
 223                               Type,
 224                               AnnotatedElement,
 225                               TypeDescriptor.OfField<Class<?>>,
 226                               Constable {
 227     private static final int ANNOTATION = 0x00002000;
 228     private static final int ENUM       = 0x00004000;
 229     private static final int SYNTHETIC  = 0x00001000;
 230 
 231     private static native void registerNatives();
 232     static {
 233         runtimeSetup();
 234     }
 235 
 236     /// No significant static final fields; [#resetArchivedStates()] handles
 237     /// prevents storing [#reflectionFactory] into AOT image.
 238     @AOTRuntimeSetup
 239     private static void runtimeSetup() {
 240         registerNatives();
 241     }
 242 
 243     /*
 244      * Private constructor. Only the Java Virtual Machine creates Class objects.
 245      * This constructor is not used and prevents the default constructor being
 246      * generated.
 247      */
 248     private Class(ClassLoader loader, Class<?> arrayComponentType, char mods, ProtectionDomain pd, boolean isPrim, char flags) {
 249         // Initialize final field for classLoader.  The initialization value of non-null

 327                 Reflection.appendAccessControlModifiers(sb, modifiers);
 328                 if (Modifier.isAbstract(modifiers))
 329                     sb.append("abstract "); // Intentionally printed for interfaces
 330                 if (Modifier.isStatic(modifiers))
 331                     sb.append("static ");
 332                 if (Modifier.isFinal(modifiers))
 333                     sb.append("final ");
 334 
 335                 addSealingInfo(modifiers, sb);
 336 
 337                 // Note: class strictfp modifier is not recoverable from a class file
 338 
 339                 if (isAnnotation()) {
 340                     sb.append('@');
 341                 }
 342                 if (isInterface()) { // Note: all annotation interfaces are interfaces
 343                     sb.append("interface");
 344                 } else {
 345                     if (isEnum())
 346                         sb.append("enum");
 347                     else if (isRecord())
 348                         sb.append("record");
 349                     else
 350                         sb.append("class");






 351                 }
 352                 sb.append(' ');
 353                 sb.append(getName());
 354             }
 355 
 356             TypeVariable<?>[] typeparms = component.getTypeParameters();
 357             if (typeparms.length > 0) {
 358                 sb.append(Arrays.stream(typeparms)
 359                           .map(Class::typeVarBounds)
 360                           .collect(Collectors.joining(",", "<", ">")));
 361             }
 362 
 363             if (arrayDepth > 0) sb.append("[]".repeat(arrayDepth));
 364 
 365             return sb.toString();
 366         }
 367     }
 368 
 369     private void addSealingInfo(int modifiers, StringBuilder sb) {
 370         // A class can be final XOR sealed XOR non-sealed.

 599      *
 600      * @jls 12.2 Loading of Classes and Interfaces
 601      * @jls 12.3 Linking of Classes and Interfaces
 602      * @since 9
 603      */
 604     public static Class<?> forName(Module module, String name) {
 605         Objects.requireNonNull(module);
 606         Objects.requireNonNull(name);
 607         if (!ModifiedUtf.isValidLengthInConstantPool(name)) {
 608             return null;
 609         }
 610 
 611         ClassLoader cl = module.getClassLoader();
 612         if (cl != null) {
 613             return cl.loadClass(module, name);
 614         } else {
 615             return BootLoader.loadClass(module, name);
 616         }
 617     }
 618 






















 619     /**
 620      * {@return the {@code Class} object associated with the
 621      * {@linkplain #isPrimitive() primitive type} of the given name}
 622      * If the argument is not the name of a primitive type, {@code
 623      * null} is returned.
 624      *
 625      * @param primitiveName the name of the primitive type to find
 626      *
 627      * @jls 4.2 Primitive Types and Values
 628      * @jls 15.8.2 Class Literals
 629      * @since 22
 630      */
 631     public static Class<?> forPrimitiveName(String primitiveName) {
 632         return switch(primitiveName) {
 633         // Integral types
 634         case "int"     -> int.class;
 635         case "long"    -> long.class;
 636         case "short"   -> short.class;
 637         case "char"    -> char.class;
 638         case "byte"    -> byte.class;

 854      * @see     java.lang.Float#TYPE
 855      * @see     java.lang.Double#TYPE
 856      * @see     java.lang.Void#TYPE
 857      * @since 1.1
 858      * @jls 15.8.2 Class Literals
 859      */
 860     public boolean isPrimitive() {
 861         return primitive;
 862     }
 863 
 864     /**
 865      * Returns true if this {@code Class} object represents an annotation
 866      * interface.  Note that if this method returns true, {@link #isInterface()}
 867      * would also return true, as all annotation interfaces are also interfaces.
 868      *
 869      * @return {@code true} if this {@code Class} object represents an annotation
 870      *      interface; {@code false} otherwise
 871      * @since 1.5
 872      */
 873     public boolean isAnnotation() {
 874         return (getModifiers() & ANNOTATION) != 0;
 875     }
 876 
 877     /**
 878      *{@return {@code true} if and only if this class has the synthetic modifier
 879      * bit set}
 880      *
 881      * @jls 13.1 The Form of a Binary
 882      * @jvms 4.1 The {@code ClassFile} Structure
 883      * @see <a
 884      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
 885      * programming language and JVM modeling in core reflection</a>
 886      * @since 1.5
 887      */
 888     public boolean isSynthetic() {
 889         return (getModifiers() & SYNTHETIC) != 0;
 890     }
 891 
 892     /**
 893      * Returns the  name of the entity (class, interface, array class,
 894      * primitive type, or void) represented by this {@code Class} object.
 895      *
 896      * <p> If this {@code Class} object represents a class or interface,
 897      * not an array class, then:
 898      * <ul>
 899      * <li> If the class or interface is not {@linkplain #isHidden() hidden},
 900      *      then the {@linkplain ClassLoader##binary-name binary name}
 901      *      of the class or interface is returned.
 902      * <li> If the class or interface is hidden, then the result is a string
 903      *      of the form: {@code N + '/' + <suffix>}
 904      *      where {@code N} is the {@linkplain ClassLoader##binary-name binary name}
 905      *      indicated by the {@code class} file passed to
 906      *      {@link java.lang.invoke.MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)
 907      *      Lookup::defineHiddenClass}, and {@code <suffix>} is an unqualified name.
 908      * </ul>
 909      *

1002      *
1003      * @since 9
1004      */
1005     public Module getModule() {
1006         return module;
1007     }
1008 
1009     // set by VM
1010     @Stable
1011     private transient Module module;
1012 
1013     // Initialized in JVM not by private constructor
1014     // This field is filtered from reflection access, i.e. getDeclaredField
1015     // will throw NoSuchFieldException
1016     private final ClassLoader classLoader;
1017 
1018     private transient Object classData; // Set by VM
1019     private transient Object[] signers; // Read by VM, mutable
1020     private final transient char modifiers;  // Set by the VM
1021     private final transient char classFileAccessFlags;  // Set by the VM
1022     private final transient boolean primitive;  // Set by the VM if the Class is a primitive type.
1023 
1024     // package-private
1025     Object getClassData() {
1026         return classData;
1027     }
1028 
1029     /**
1030      * Returns an array of {@code TypeVariable} objects that represent the
1031      * type variables declared by the generic declaration represented by this
1032      * {@code GenericDeclaration} object, in declaration order.  Returns an
1033      * array of length 0 if the underlying generic declaration declares no type
1034      * variables.
1035      *
1036      * @return an array of {@code TypeVariable} objects that represent
1037      *     the type variables declared by this generic declaration
1038      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
1039      *     signature of this generic declaration does not conform to
1040      *     the format specified in section {@jvms 4.7.9} of
1041      *     <cite>The Java Virtual Machine Specification</cite>
1042      * @since 1.5

1322         }
1323         return c;
1324     }
1325 
1326     /**
1327      * Returns the Java language modifiers for this class or interface, encoded
1328      * in an integer. The modifiers consist of the Java Virtual Machine's
1329      * constants for {@code public}, {@code protected},
1330      * {@code private}, {@code final}, {@code static},
1331      * {@code abstract} and {@code interface}; they should be decoded
1332      * using the methods of class {@code Modifier}.
1333      *
1334      * <p> If the underlying class is an array class:
1335      * <ul>
1336      * <li> its {@code public}, {@code private} and {@code protected}
1337      *      modifiers are the same as those of its component type
1338      * <li> its {@code abstract} and {@code final} modifiers are always
1339      *      {@code true}
1340      * <li> its interface modifier is always {@code false}, even when
1341      *      the component type is an interface


1342      * </ul>
1343      * If this {@code Class} object represents a primitive type or
1344      * void, its {@code public}, {@code abstract}, and {@code final}
1345      * modifiers are always {@code true}.
1346      * For {@code Class} objects representing void, primitive types, and
1347      * arrays, the values of other modifiers are {@code false} other
1348      * than as specified above.
1349      *
1350      * <p> The modifier encodings are defined in section {@jvms 4.1}
1351      * of <cite>The Java Virtual Machine Specification</cite>.
1352      *
1353      * @return the {@code int} representing the modifiers for this class
1354      * @see     java.lang.reflect.Modifier
1355      * @see #accessFlags()
1356      * @see <a
1357      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1358      * programming language and JVM modeling in core reflection</a>
1359      * @since 1.1
1360      * @jls 8.1.1 Class Modifiers
1361      * @jls 9.1.1 Interface Modifiers
1362      * @jvms 4.1 The {@code ClassFile} Structure
1363      */
1364     public int getModifiers() { return modifiers; }
1365 
1366     /**
1367      * {@return an unmodifiable set of the {@linkplain AccessFlag access
1368      * flags} for this class, possibly empty}

1369      *
1370      * <p> If the underlying class is an array class:
1371      * <ul>
1372      * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1373      *      access flags are the same as those of its component type
1374      * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1375      * <li> its {@code INTERFACE} flag is absent, even when the
1376      *      component type is an interface


1377      * </ul>
1378      * If this {@code Class} object represents a primitive type or
1379      * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1380      * {@code FINAL}.
1381      * For {@code Class} objects representing void, primitive types, and
1382      * arrays, access flags are absent other than as specified above.
1383      *
1384      * @see #getModifiers()
1385      * @jvms 4.1 The ClassFile Structure
1386      * @jvms 4.7.6 The InnerClasses Attribute
1387      * @since 20
1388      */
1389     public Set<AccessFlag> accessFlags() {
1390         // Location.CLASS allows SUPER and AccessFlag.MODULE which
1391         // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1392         // and STATIC, which are not allowed on Location.CLASS.
1393         // Use getClassFileAccessFlags to expose SUPER status.
1394         // Arrays need to use PRIVATE/PROTECTED from its component modifiers.
1395         var location = (isMemberClass() || isLocalClass() ||
1396                         isAnonymousClass() || isArray()) ?
1397             AccessFlag.Location.INNER_CLASS :
1398             AccessFlag.Location.CLASS;
1399         return getReflectionFactory().parseAccessFlags((location == AccessFlag.Location.CLASS) ?
1400                         getClassFileAccessFlags() : getModifiers(), location, this);



1401     }
1402 
1403     /**
1404      * Gets the signers of this class.
1405      *
1406      * @return  the signers of this class, or null if there are no signers.  In
1407      *          particular, this method returns null if this {@code Class} object represents
1408      *          a primitive type or void.
1409      * @since   1.1
1410      */
1411     public Object[] getSigners() {
1412         var signers = this.signers;
1413         return signers == null ? null : signers.clone();
1414     }
1415 
1416     /**
1417      * Set the signers of this class.
1418      */
1419     void setSigners(Object[] signers) {
1420         if (!isPrimitive() && !isArray()) {

3345      * source code.
3346      *
3347      * Note that {@link java.lang.Enum} is not itself an enum class.
3348      *
3349      * Also note that if an enum constant is declared with a class body,
3350      * the class of that enum constant object is an anonymous class
3351      * and <em>not</em> the class of the declaring enum class. The
3352      * {@link Enum#getDeclaringClass} method of an enum constant can
3353      * be used to get the class of the enum class declaring the
3354      * constant.
3355      *
3356      * @return true if and only if this class was declared as an enum in the
3357      *     source code
3358      * @since 1.5
3359      * @jls 8.9.1 Enum Constants
3360      */
3361     public boolean isEnum() {
3362         // An enum must both directly extend java.lang.Enum and have
3363         // the ENUM bit set; classes for specialized enum constants
3364         // don't do the former.
3365         return (this.getModifiers() & ENUM) != 0 &&
3366         this.getSuperclass() == java.lang.Enum.class;
3367     }
3368 
3369     /**
3370      * Returns {@code true} if and only if this class is a record class.
3371      *
3372      * <p> The {@linkplain #getSuperclass() direct superclass} of a record
3373      * class is {@code java.lang.Record}. A record class is {@linkplain
3374      * Modifier#FINAL final}. A record class has (possibly zero) record
3375      * components; {@link #getRecordComponents()} returns a non-null but
3376      * possibly empty value for a record.
3377      *
3378      * <p> Note that class {@link Record} is not a record class and thus
3379      * invoking this method on class {@code Record} returns {@code false}.
3380      *
3381      * @return true if and only if this class is a record class, otherwise false
3382      * @jls 8.10 Record Classes
3383      * @since 16
3384      */
3385     public boolean isRecord() {

  35 import java.io.InputStream;
  36 import java.io.ObjectStreamField;
  37 import java.lang.reflect.AnnotatedElement;
  38 import java.lang.reflect.AnnotatedType;
  39 import java.lang.reflect.AccessFlag;
  40 import java.lang.reflect.Array;
  41 import java.lang.reflect.Constructor;
  42 import java.lang.reflect.Executable;
  43 import java.lang.reflect.Field;
  44 import java.lang.reflect.GenericArrayType;
  45 import java.lang.reflect.GenericDeclaration;
  46 import java.lang.reflect.GenericSignatureFormatError;
  47 import java.lang.reflect.InvocationTargetException;
  48 import java.lang.reflect.Member;
  49 import java.lang.reflect.Method;
  50 import java.lang.reflect.Modifier;
  51 import java.lang.reflect.RecordComponent;
  52 import java.lang.reflect.Type;
  53 import java.lang.reflect.TypeVariable;
  54 import java.lang.constant.Constable;
  55 import java.lang.classfile.ClassFile;
  56 import java.net.URL;
  57 import java.security.AllPermission;
  58 import java.security.Permissions;
  59 import java.security.ProtectionDomain;
  60 import java.util.ArrayList;
  61 import java.util.Arrays;
  62 import java.util.Collection;
  63 import java.util.HashMap;
  64 import java.util.LinkedHashMap;
  65 import java.util.LinkedHashSet;
  66 import java.util.List;
  67 import java.util.Map;
  68 import java.util.Objects;
  69 import java.util.Optional;
  70 import java.util.Set;
  71 import java.util.stream.Collectors;
  72 
  73 import jdk.internal.constant.ConstantUtils;
  74 import jdk.internal.javac.PreviewFeature;
  75 import jdk.internal.loader.BootLoader;
  76 import jdk.internal.loader.BuiltinClassLoader;
  77 import jdk.internal.misc.PreviewFeatures;
  78 import jdk.internal.misc.Unsafe;
  79 import jdk.internal.module.Resources;
  80 import jdk.internal.reflect.AccessFlagSet;
  81 import jdk.internal.reflect.CallerSensitive;
  82 import jdk.internal.reflect.CallerSensitiveAdapter;
  83 import jdk.internal.reflect.ConstantPool;
  84 import jdk.internal.reflect.PreviewAccessFlags;
  85 import jdk.internal.reflect.Reflection;
  86 import jdk.internal.reflect.ReflectionFactory;
  87 import jdk.internal.util.ModifiedUtf;
  88 import jdk.internal.vm.annotation.AOTRuntimeSetup;
  89 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
  90 import jdk.internal.vm.annotation.IntrinsicCandidate;
  91 import jdk.internal.vm.annotation.Stable;
  92 
  93 import sun.invoke.util.BytecodeDescriptor;
  94 import sun.invoke.util.Wrapper;
  95 import sun.reflect.generics.factory.CoreReflectionFactory;
  96 import sun.reflect.generics.factory.GenericsFactory;
  97 import sun.reflect.generics.repository.ClassRepository;
  98 import sun.reflect.generics.scope.ClassScope;
  99 import sun.reflect.annotation.*;
 100 
 101 /**
 102  * Instances of the class {@code Class} represent classes and
 103  * interfaces in a running Java application. An enum class and a record
 104  * class are kinds of class; an annotation interface is a kind of

 212  * {@linkplain #getTypeName type name} return results
 213  * equal to {@code "HelloWorld"}. The {@linkplain #getSimpleName
 214  * simple name} of such an implicitly declared class is {@code "HelloWorld"} and
 215  * the {@linkplain #getCanonicalName canonical name} is {@code "HelloWorld"}.
 216  *
 217  * @param <T> the type of the class modeled by this {@code Class}
 218  * object.  For example, the type of {@code String.class} is {@code
 219  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 220  * unknown.
 221  *
 222  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 223  * @since   1.0
 224  */
 225 @AOTSafeClassInitializer
 226 public final class Class<T> implements java.io.Serializable,
 227                               GenericDeclaration,
 228                               Type,
 229                               AnnotatedElement,
 230                               TypeDescriptor.OfField<Class<?>>,
 231                               Constable {



 232 
 233     private static native void registerNatives();
 234     static {
 235         runtimeSetup();
 236     }
 237 
 238     /// No significant static final fields; [#resetArchivedStates()] handles
 239     /// prevents storing [#reflectionFactory] into AOT image.
 240     @AOTRuntimeSetup
 241     private static void runtimeSetup() {
 242         registerNatives();
 243     }
 244 
 245     /*
 246      * Private constructor. Only the Java Virtual Machine creates Class objects.
 247      * This constructor is not used and prevents the default constructor being
 248      * generated.
 249      */
 250     private Class(ClassLoader loader, Class<?> arrayComponentType, char mods, ProtectionDomain pd, boolean isPrim, char flags) {
 251         // Initialize final field for classLoader.  The initialization value of non-null

 329                 Reflection.appendAccessControlModifiers(sb, modifiers);
 330                 if (Modifier.isAbstract(modifiers))
 331                     sb.append("abstract "); // Intentionally printed for interfaces
 332                 if (Modifier.isStatic(modifiers))
 333                     sb.append("static ");
 334                 if (Modifier.isFinal(modifiers))
 335                     sb.append("final ");
 336 
 337                 addSealingInfo(modifiers, sb);
 338 
 339                 // Note: class strictfp modifier is not recoverable from a class file
 340 
 341                 if (isAnnotation()) {
 342                     sb.append('@');
 343                 }
 344                 if (isInterface()) { // Note: all annotation interfaces are interfaces
 345                     sb.append("interface");
 346                 } else {
 347                     if (isEnum())
 348                         sb.append("enum");
 349                     else {
 350                         if (isValue()) {
 351                             sb.append("value ");
 352                         }
 353                         if (isRecord()) {
 354                             sb.append("record");
 355                         } else {
 356                             sb.append("class");
 357                         }
 358                     }
 359                 }
 360                 sb.append(' ');
 361                 sb.append(getName());
 362             }
 363 
 364             TypeVariable<?>[] typeparms = component.getTypeParameters();
 365             if (typeparms.length > 0) {
 366                 sb.append(Arrays.stream(typeparms)
 367                           .map(Class::typeVarBounds)
 368                           .collect(Collectors.joining(",", "<", ">")));
 369             }
 370 
 371             if (arrayDepth > 0) sb.append("[]".repeat(arrayDepth));
 372 
 373             return sb.toString();
 374         }
 375     }
 376 
 377     private void addSealingInfo(int modifiers, StringBuilder sb) {
 378         // A class can be final XOR sealed XOR non-sealed.

 607      *
 608      * @jls 12.2 Loading of Classes and Interfaces
 609      * @jls 12.3 Linking of Classes and Interfaces
 610      * @since 9
 611      */
 612     public static Class<?> forName(Module module, String name) {
 613         Objects.requireNonNull(module);
 614         Objects.requireNonNull(name);
 615         if (!ModifiedUtf.isValidLengthInConstantPool(name)) {
 616             return null;
 617         }
 618 
 619         ClassLoader cl = module.getClassLoader();
 620         if (cl != null) {
 621             return cl.loadClass(module, name);
 622         } else {
 623             return BootLoader.loadClass(module, name);
 624         }
 625     }
 626 
 627     /**
 628      * {@return {@code true} if this {@code Class} object represents a value class,
 629      * otherwise {@code false}}
 630      *
 631      * <p>A value class is declared with the {@code value} modifier. If this
 632      * {@code Class} object represents an interface, array type, primitive type,
 633      * or {@code void}, the result is {@code false}.
 634      *
 635      * @jls value-objects-8.1.1.5 {@code value} Classes
 636      * @see AccessFlag#IDENTITY
 637      * @since Valhalla
 638      */
 639     @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
 640     public boolean isValue() {
 641         if (!PreviewFeatures.isEnabled()) {
 642             return false;
 643         } else {
 644             int mask = ClassFile.ACC_IDENTITY | ClassFile.ACC_INTERFACE;
 645             return !primitive && (getModifiers() & mask) == 0;
 646         }
 647     }
 648 
 649     /**
 650      * {@return the {@code Class} object associated with the
 651      * {@linkplain #isPrimitive() primitive type} of the given name}
 652      * If the argument is not the name of a primitive type, {@code
 653      * null} is returned.
 654      *
 655      * @param primitiveName the name of the primitive type to find
 656      *
 657      * @jls 4.2 Primitive Types and Values
 658      * @jls 15.8.2 Class Literals
 659      * @since 22
 660      */
 661     public static Class<?> forPrimitiveName(String primitiveName) {
 662         return switch(primitiveName) {
 663         // Integral types
 664         case "int"     -> int.class;
 665         case "long"    -> long.class;
 666         case "short"   -> short.class;
 667         case "char"    -> char.class;
 668         case "byte"    -> byte.class;

 884      * @see     java.lang.Float#TYPE
 885      * @see     java.lang.Double#TYPE
 886      * @see     java.lang.Void#TYPE
 887      * @since 1.1
 888      * @jls 15.8.2 Class Literals
 889      */
 890     public boolean isPrimitive() {
 891         return primitive;
 892     }
 893 
 894     /**
 895      * Returns true if this {@code Class} object represents an annotation
 896      * interface.  Note that if this method returns true, {@link #isInterface()}
 897      * would also return true, as all annotation interfaces are also interfaces.
 898      *
 899      * @return {@code true} if this {@code Class} object represents an annotation
 900      *      interface; {@code false} otherwise
 901      * @since 1.5
 902      */
 903     public boolean isAnnotation() {
 904         return (getModifiers() & ClassFile.ACC_ANNOTATION) != 0;
 905     }
 906 
 907     /**
 908      *{@return {@code true} if and only if this class has the synthetic modifier
 909      * bit set}
 910      *
 911      * @jls 13.1 The Form of a Binary
 912      * @jvms 4.1 The {@code ClassFile} Structure
 913      * @see <a
 914      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
 915      * programming language and JVM modeling in core reflection</a>
 916      * @since 1.5
 917      */
 918     public boolean isSynthetic() {
 919         return (getModifiers() & ClassFile.ACC_SYNTHETIC) != 0;
 920     }
 921 
 922     /**
 923      * Returns the  name of the entity (class, interface, array class,
 924      * primitive type, or void) represented by this {@code Class} object.
 925      *
 926      * <p> If this {@code Class} object represents a class or interface,
 927      * not an array class, then:
 928      * <ul>
 929      * <li> If the class or interface is not {@linkplain #isHidden() hidden},
 930      *      then the {@linkplain ClassLoader##binary-name binary name}
 931      *      of the class or interface is returned.
 932      * <li> If the class or interface is hidden, then the result is a string
 933      *      of the form: {@code N + '/' + <suffix>}
 934      *      where {@code N} is the {@linkplain ClassLoader##binary-name binary name}
 935      *      indicated by the {@code class} file passed to
 936      *      {@link java.lang.invoke.MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)
 937      *      Lookup::defineHiddenClass}, and {@code <suffix>} is an unqualified name.
 938      * </ul>
 939      *

1032      *
1033      * @since 9
1034      */
1035     public Module getModule() {
1036         return module;
1037     }
1038 
1039     // set by VM
1040     @Stable
1041     private transient Module module;
1042 
1043     // Initialized in JVM not by private constructor
1044     // This field is filtered from reflection access, i.e. getDeclaredField
1045     // will throw NoSuchFieldException
1046     private final ClassLoader classLoader;
1047 
1048     private transient Object classData; // Set by VM
1049     private transient Object[] signers; // Read by VM, mutable
1050     private final transient char modifiers;  // Set by the VM
1051     private final transient char classFileAccessFlags;  // Set by the VM
1052     private final transient boolean primitive;  // Set by the VM if the Class is a primitive type
1053 
1054     // package-private
1055     Object getClassData() {
1056         return classData;
1057     }
1058 
1059     /**
1060      * Returns an array of {@code TypeVariable} objects that represent the
1061      * type variables declared by the generic declaration represented by this
1062      * {@code GenericDeclaration} object, in declaration order.  Returns an
1063      * array of length 0 if the underlying generic declaration declares no type
1064      * variables.
1065      *
1066      * @return an array of {@code TypeVariable} objects that represent
1067      *     the type variables declared by this generic declaration
1068      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
1069      *     signature of this generic declaration does not conform to
1070      *     the format specified in section {@jvms 4.7.9} of
1071      *     <cite>The Java Virtual Machine Specification</cite>
1072      * @since 1.5

1352         }
1353         return c;
1354     }
1355 
1356     /**
1357      * Returns the Java language modifiers for this class or interface, encoded
1358      * in an integer. The modifiers consist of the Java Virtual Machine's
1359      * constants for {@code public}, {@code protected},
1360      * {@code private}, {@code final}, {@code static},
1361      * {@code abstract} and {@code interface}; they should be decoded
1362      * using the methods of class {@code Modifier}.
1363      *
1364      * <p> If the underlying class is an array class:
1365      * <ul>
1366      * <li> its {@code public}, {@code private} and {@code protected}
1367      *      modifiers are the same as those of its component type
1368      * <li> its {@code abstract} and {@code final} modifiers are always
1369      *      {@code true}
1370      * <li> its interface modifier is always {@code false}, even when
1371      *      the component type is an interface
1372      * <li> when preview features are enabled, its {@linkplain
1373      *      AccessFlag#IDENTITY identity} modifier is always true
1374      * </ul>
1375      * If this {@code Class} object represents a primitive type or
1376      * void, its {@code public}, {@code abstract}, and {@code final}
1377      * modifiers are always {@code true}.
1378      * For {@code Class} objects representing void, primitive types, and
1379      * arrays, the values of other modifiers are {@code false} other
1380      * than as specified above.
1381      *
1382      * <p> The modifier encodings are defined in section {@jvms 4.1}
1383      * of <cite>The Java Virtual Machine Specification</cite>.
1384      *
1385      * @return the {@code int} representing the modifiers for this class
1386      * @see     java.lang.reflect.Modifier
1387      * @see #accessFlags()
1388      * @see <a
1389      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1390      * programming language and JVM modeling in core reflection</a>
1391      * @since 1.1
1392      * @jls 8.1.1 Class Modifiers
1393      * @jls 9.1.1 Interface Modifiers
1394      * @jvms 4.1 The {@code ClassFile} Structure
1395      */
1396     public int getModifiers() { return modifiers; }
1397 
1398     /**
1399      * {@return an unmodifiable set of the {@linkplain AccessFlag access
1400      * flags} for this class, possibly empty}
1401      * The {@code AccessFlags} may depend on the class file format version of the class.
1402      *
1403      * <p> If the underlying class is an array class:
1404      * <ul>
1405      * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1406      *      access flags are the same as those of its component type
1407      * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1408      * <li> its {@code INTERFACE} flag is absent, even when the
1409      *      component type is an interface
1410      * <li> when preview features are enabled, its {@code IDENTITY} flag
1411     *       is present
1412      * </ul>
1413      * If this {@code Class} object represents a primitive type or
1414      * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1415      * {@code FINAL}.
1416      * For {@code Class} objects representing void, primitive types, and
1417      * arrays, access flags are absent other than as specified above.
1418      *
1419      * @see #getModifiers()
1420      * @jvms 4.1 The ClassFile Structure
1421      * @jvms 4.7.6 The InnerClasses Attribute
1422      * @since 20
1423      */
1424     public Set<AccessFlag> accessFlags() {
1425         if (!PreviewFeatures.isEnabled()) {
1426             // INNER_CLASS_FLAGS exclusively defines PRIVATE, PROTECTED, and STATIC.
1427             // CLASS_FLAGS exclusively defines SUPER and MODULE.
1428             // Nested classes and interfaces need to report PRIVATE/PROTECTED/STATIC.
1429             // Arrays need to report PRIVATE/PROTECTED.
1430             // Top-level classes need to report SUPER, using getClassFileAccessFlags.
1431             // Module descriptors do not have Class objects so nothing reports MODULE.
1432             return (isArray() || getEnclosingClass() != null)
1433                     ? AccessFlagSet.ofValidated(AccessFlagSet.INNER_CLASS_FLAGS, getModifiers())
1434                     : AccessFlagSet.ofValidated(AccessFlagSet.CLASS_FLAGS, getClassFileAccessFlags());
1435         }
1436         // CLASS_FLAGS exclusively defines MODULE, but module descriptors are
1437         // never represented with Class objects, so INNER_CLASS_FLAGS works
1438         return AccessFlagSet.ofValidated(PreviewAccessFlags.INNER_CLASS_PREVIEW_FLAGS, getModifiers());
1439     }
1440 
1441     /**
1442      * Gets the signers of this class.
1443      *
1444      * @return  the signers of this class, or null if there are no signers.  In
1445      *          particular, this method returns null if this {@code Class} object represents
1446      *          a primitive type or void.
1447      * @since   1.1
1448      */
1449     public Object[] getSigners() {
1450         var signers = this.signers;
1451         return signers == null ? null : signers.clone();
1452     }
1453 
1454     /**
1455      * Set the signers of this class.
1456      */
1457     void setSigners(Object[] signers) {
1458         if (!isPrimitive() && !isArray()) {

3383      * source code.
3384      *
3385      * Note that {@link java.lang.Enum} is not itself an enum class.
3386      *
3387      * Also note that if an enum constant is declared with a class body,
3388      * the class of that enum constant object is an anonymous class
3389      * and <em>not</em> the class of the declaring enum class. The
3390      * {@link Enum#getDeclaringClass} method of an enum constant can
3391      * be used to get the class of the enum class declaring the
3392      * constant.
3393      *
3394      * @return true if and only if this class was declared as an enum in the
3395      *     source code
3396      * @since 1.5
3397      * @jls 8.9.1 Enum Constants
3398      */
3399     public boolean isEnum() {
3400         // An enum must both directly extend java.lang.Enum and have
3401         // the ENUM bit set; classes for specialized enum constants
3402         // don't do the former.
3403         return (this.getModifiers() & ClassFile.ACC_ENUM) != 0 &&
3404         this.getSuperclass() == java.lang.Enum.class;
3405     }
3406 
3407     /**
3408      * Returns {@code true} if and only if this class is a record class.
3409      *
3410      * <p> The {@linkplain #getSuperclass() direct superclass} of a record
3411      * class is {@code java.lang.Record}. A record class is {@linkplain
3412      * Modifier#FINAL final}. A record class has (possibly zero) record
3413      * components; {@link #getRecordComponents()} returns a non-null but
3414      * possibly empty value for a record.
3415      *
3416      * <p> Note that class {@link Record} is not a record class and thus
3417      * invoking this method on class {@code Record} returns {@code false}.
3418      *
3419      * @return true if and only if this class is a record class, otherwise false
3420      * @jls 8.10 Record Classes
3421      * @since 16
3422      */
3423     public boolean isRecord() {
< prev index next >