< prev index next >

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

Print this page

  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.lang.constant.ClassDesc;
  30 import java.lang.constant.ConstantDescs;
  31 import java.lang.invoke.TypeDescriptor;
  32 import java.lang.invoke.MethodHandles;
  33 import java.lang.ref.SoftReference;
  34 import java.io.IOException;
  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.InvocationTargetException;
  47 import java.lang.reflect.Member;
  48 import java.lang.reflect.Method;
  49 import java.lang.reflect.Modifier;
  50 import java.lang.reflect.Proxy;
  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.vm.annotation.IntrinsicCandidate;
  83 import jdk.internal.vm.annotation.Stable;
  84 
  85 import sun.invoke.util.Wrapper;
  86 import sun.reflect.generics.factory.CoreReflectionFactory;
  87 import sun.reflect.generics.factory.GenericsFactory;
  88 import sun.reflect.generics.repository.ClassRepository;
  89 import sun.reflect.generics.repository.MethodRepository;
  90 import sun.reflect.generics.repository.ConstructorRepository;
  91 import sun.reflect.generics.scope.ClassScope;
  92 import sun.reflect.annotation.*;
  93 
  94 /**

 200  * HelloWorld}, the methods to get the {@linkplain #getName name} and
 201  * {@linkplain #getTypeName type name} return results
 202  * equal to {@code "HelloWorld"}. The {@linkplain #getSimpleName
 203  * simple name} of such an implicitly declared class is {@code "HelloWorld"} and
 204  * the {@linkplain #getCanonicalName canonical name} is {@code "HelloWorld"}.
 205  *
 206  * @param <T> the type of the class modeled by this {@code Class}
 207  * object.  For example, the type of {@code String.class} is {@code
 208  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 209  * unknown.
 210  *
 211  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 212  * @since   1.0
 213  */
 214 public final class Class<T> implements java.io.Serializable,
 215                               GenericDeclaration,
 216                               Type,
 217                               AnnotatedElement,
 218                               TypeDescriptor.OfField<Class<?>>,
 219                               Constable {
 220     private static final int ANNOTATION= 0x00002000;
 221     private static final int ENUM      = 0x00004000;
 222     private static final int SYNTHETIC = 0x00001000;
 223 
 224     private static native void registerNatives();
 225     static {
 226         runtimeSetup();
 227     }
 228 
 229     // Called from JVM when loading an AOT cache
 230     private static void runtimeSetup() {
 231         registerNatives();
 232     }
 233 
 234     /*
 235      * Private constructor. Only the Java Virtual Machine creates Class objects.
 236      * This constructor is not used and prevents the default constructor being
 237      * generated.
 238      */
 239     private Class(ClassLoader loader, Class<?> arrayComponentType, char mods, ProtectionDomain pd, boolean isPrim) {
 240         // Initialize final field for classLoader.  The initialization value of non-null
 241         // prevents future JIT optimizations from assuming this final field is null.
 242         // The following assignments are done directly by the VM without calling this constructor.

 298      *
 299      * @since 1.8
 300      */
 301     public String toGenericString() {
 302         if (isPrimitive()) {
 303             return toString();
 304         } else {
 305             StringBuilder sb = new StringBuilder();
 306             Class<?> component = this;
 307             int arrayDepth = 0;
 308 
 309             if (isArray()) {
 310                 do {
 311                     arrayDepth++;
 312                     component = component.getComponentType();
 313                 } while (component.isArray());
 314                 sb.append(component.getName());
 315             } else {
 316                 // Class modifiers are a superset of interface modifiers
 317                 int modifiers = getModifiers() & Modifier.classModifiers();


 318                 if (modifiers != 0) {
 319                     sb.append(Modifier.toString(modifiers));
 320                     sb.append(' ');
 321                 }
 322 
 323                 // A class cannot be strictfp and sealed/non-sealed so
 324                 // it is sufficient to check for sealed-ness after all
 325                 // modifiers are printed.
 326                 addSealingInfo(modifiers, sb);
 327 
 328                 if (isAnnotation()) {
 329                     sb.append('@');
 330                 }
 331                 if (isInterface()) { // Note: all annotation interfaces are interfaces
 332                     sb.append("interface");
 333                 } else {
 334                     if (isEnum())
 335                         sb.append("enum");
 336                     else if (isRecord())
 337                         sb.append("record");
 338                     else
 339                         sb.append("class");





 340                 }
 341                 sb.append(' ');
 342                 sb.append(getName());
 343             }
 344 
 345             TypeVariable<?>[] typeparms = component.getTypeParameters();
 346             if (typeparms.length > 0) {
 347                 sb.append(Arrays.stream(typeparms)
 348                           .map(Class::typeVarBounds)
 349                           .collect(Collectors.joining(",", "<", ">")));
 350             }
 351 
 352             if (arrayDepth > 0) sb.append("[]".repeat(arrayDepth));
 353 
 354             return sb.toString();
 355         }
 356     }
 357 
 358     private void addSealingInfo(int modifiers, StringBuilder sb) {
 359         // A class can be final XOR sealed XOR non-sealed.

 585      * @throws NullPointerException if the given module or name is {@code null}
 586      *
 587      * @throws LinkageError if the linkage fails
 588      *
 589      * @jls 12.2 Loading of Classes and Interfaces
 590      * @jls 12.3 Linking of Classes and Interfaces
 591      * @since 9
 592      */
 593     public static Class<?> forName(Module module, String name) {
 594         Objects.requireNonNull(module);
 595         Objects.requireNonNull(name);
 596 
 597         ClassLoader cl = module.getClassLoader();
 598         if (cl != null) {
 599             return cl.loadClass(module, name);
 600         } else {
 601             return BootLoader.loadClass(module, name);
 602         }
 603     }
 604 






















































 605     /**
 606      * {@return the {@code Class} object associated with the
 607      * {@linkplain #isPrimitive() primitive type} of the given name}
 608      * If the argument is not the name of a primitive type, {@code
 609      * null} is returned.
 610      *
 611      * @param primitiveName the name of the primitive type to find
 612      *
 613      * @throws NullPointerException if the argument is {@code null}
 614      *
 615      * @jls 4.2 Primitive Types and Values
 616      * @jls 15.8.2 Class Literals
 617      * @since 22
 618      */
 619     public static Class<?> forPrimitiveName(String primitiveName) {
 620         return switch(primitiveName) {
 621         // Integral types
 622         case "int"     -> int.class;
 623         case "long"    -> long.class;
 624         case "short"   -> short.class;

1311         }
1312         return c;
1313     }
1314 
1315     /**
1316      * Returns the Java language modifiers for this class or interface, encoded
1317      * in an integer. The modifiers consist of the Java Virtual Machine's
1318      * constants for {@code public}, {@code protected},
1319      * {@code private}, {@code final}, {@code static},
1320      * {@code abstract} and {@code interface}; they should be decoded
1321      * using the methods of class {@code Modifier}.
1322      *
1323      * <p> If the underlying class is an array class:
1324      * <ul>
1325      * <li> its {@code public}, {@code private} and {@code protected}
1326      *      modifiers are the same as those of its component type
1327      * <li> its {@code abstract} and {@code final} modifiers are always
1328      *      {@code true}
1329      * <li> its interface modifier is always {@code false}, even when
1330      *      the component type is an interface

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

1358      *
1359      * <p> If the underlying class is an array class:
1360      * <ul>
1361      * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1362      *      access flags are the same as those of its component type
1363      * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1364      * <li> its {@code INTERFACE} flag is absent, even when the
1365      *      component type is an interface

1366      * </ul>
1367      * If this {@code Class} object represents a primitive type or
1368      * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1369      * {@code FINAL}.
1370      * For {@code Class} objects representing void, primitive types, and
1371      * arrays, access flags are absent other than as specified above.
1372      *
1373      * @see #getModifiers()
1374      * @jvms 4.1 The ClassFile Structure
1375      * @jvms 4.7.6 The InnerClasses Attribute
1376      * @since 20
1377      */
1378     public Set<AccessFlag> accessFlags() {
1379         // Location.CLASS allows SUPER and AccessFlag.MODULE which
1380         // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1381         // and STATIC, which are not allowed on Location.CLASS.
1382         // Use getClassAccessFlagsRaw to expose SUPER status.
1383         var location = (isMemberClass() || isLocalClass() ||
1384                         isAnonymousClass() || isArray()) ?
1385             AccessFlag.Location.INNER_CLASS :
1386             AccessFlag.Location.CLASS;
1387         return AccessFlag.maskToAccessFlags((location == AccessFlag.Location.CLASS) ?
1388                                             getClassAccessFlagsRaw() :
1389                                             getModifiers(),
1390                                             location);







1391     }
1392 
1393     /**
1394      * Gets the signers of this class.
1395      *
1396      * @return  the signers of this class, or null if there are no signers.  In
1397      *          particular, this method returns null if this {@code Class} object represents
1398      *          a primitive type or void.
1399      * @since   1.1
1400      */

1401     public Object[] getSigners() {
1402         var signers = this.signers;
1403         return signers == null ? null : signers.clone();
1404     }
1405 
1406     /**
1407      * Set the signers of this class.
1408      */
1409     void setSigners(Object[] signers) {
1410         if (!isPrimitive() && !isArray()) {
1411             this.signers = signers;
1412         }
1413     }
1414 
1415     /**
1416      * If this {@code Class} object represents a local or anonymous
1417      * class within a method, returns a {@link
1418      * java.lang.reflect.Method Method} object representing the
1419      * immediately enclosing method of the underlying class. Returns
1420      * {@code null} otherwise.

3768      *
3769      * <p> If this {@code Class} object represents an interface, the return
3770      * value is an array containing objects representing the uses of interface
3771      * types to specify interfaces directly extended by the interface. The
3772      * order of the objects in the array corresponds to the order of the
3773      * interface types used in the 'extends' clause of the declaration of this
3774      * {@code Class} object.
3775      *
3776      * <p> If this {@code Class} object represents a class or interface whose
3777      * declaration does not explicitly indicate any annotated superinterfaces,
3778      * the return value is an array of length 0.
3779      *
3780      * <p> If this {@code Class} object represents either the {@code Object}
3781      * class, an array type, a primitive type, or void, the return value is an
3782      * array of length 0.
3783      *
3784      * @return an array representing the superinterfaces
3785      * @since 1.8
3786      */
3787     public AnnotatedType[] getAnnotatedInterfaces() {
3788          return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
3789     }
3790 
3791     private native Class<?> getNestHost0();
3792 
3793     /**
3794      * Returns the nest host of the <a href=#nest>nest</a> to which the class
3795      * or interface represented by this {@code Class} object belongs.
3796      * Every class and interface belongs to exactly one nest.
3797      *
3798      * If the nest host of this class or interface has previously
3799      * been determined, then this method returns the nest host.
3800      * If the nest host of this class or interface has
3801      * not previously been determined, then this method determines the nest
3802      * host using the algorithm of JVMS 5.4.4, and returns it.
3803      *
3804      * Often, a class or interface belongs to a nest consisting only of itself,
3805      * in which case this method returns {@code this} to indicate that the class
3806      * or interface is the nest host.
3807      *
3808      * <p>If this {@code Class} object represents a primitive type, an array type,

4108      * @since 17
4109      */
4110     public boolean isSealed() {
4111         if (isArray() || isPrimitive()) {
4112             return false;
4113         }
4114         return getPermittedSubclasses() != null;
4115     }
4116 
4117     private native Class<?>[] getPermittedSubclasses0();
4118 
4119     /*
4120      * Return the class's major and minor class file version packed into an int.
4121      * The high order 16 bits contain the class's minor version.  The low order
4122      * 16 bits contain the class's major version.
4123      *
4124      * If the class is an array type then the class file version of its element
4125      * type is returned.  If the class is a primitive type then the latest class
4126      * file major version is returned and zero is returned for the minor version.
4127      */
4128     private int getClassFileVersion() {

4129         Class<?> c = isArray() ? elementType() : this;
4130         return c.getClassFileVersion0();
4131     }
4132 
4133     private native int getClassFileVersion0();
4134 
4135     /*
4136      * Return the access flags as they were in the class's bytecode, including
4137      * the original setting of ACC_SUPER.
4138      *
4139      * If the class is an array type then the access flags of the element type is
4140      * returned.  If the class is a primitive then ACC_ABSTRACT | ACC_FINAL | ACC_PUBLIC.
4141      */
4142     private int getClassAccessFlagsRaw() {
4143         Class<?> c = isArray() ? elementType() : this;
4144         return c.getClassAccessFlagsRaw0();
4145     }
4146 
4147     private native int getClassAccessFlagsRaw0();
4148 }

  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.lang.constant.ClassDesc;
  30 import java.lang.constant.ConstantDescs;
  31 import java.lang.invoke.TypeDescriptor;
  32 import java.lang.invoke.MethodHandles;
  33 import java.lang.ref.SoftReference;
  34 import java.io.IOException;
  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.ClassFileFormatVersion;
  42 import java.lang.reflect.Constructor;
  43 import java.lang.reflect.Executable;
  44 import java.lang.reflect.Field;
  45 import java.lang.reflect.GenericArrayType;
  46 import java.lang.reflect.GenericDeclaration;
  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.Proxy;
  52 import java.lang.reflect.RecordComponent;
  53 import java.lang.reflect.Type;
  54 import java.lang.reflect.TypeVariable;
  55 import java.lang.constant.Constable;
  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.CallerSensitive;
  81 import jdk.internal.reflect.CallerSensitiveAdapter;
  82 import jdk.internal.reflect.ConstantPool;
  83 import jdk.internal.reflect.Reflection;
  84 import jdk.internal.reflect.ReflectionFactory;
  85 import jdk.internal.vm.annotation.IntrinsicCandidate;
  86 import jdk.internal.vm.annotation.Stable;
  87 
  88 import sun.invoke.util.Wrapper;
  89 import sun.reflect.generics.factory.CoreReflectionFactory;
  90 import sun.reflect.generics.factory.GenericsFactory;
  91 import sun.reflect.generics.repository.ClassRepository;
  92 import sun.reflect.generics.repository.MethodRepository;
  93 import sun.reflect.generics.repository.ConstructorRepository;
  94 import sun.reflect.generics.scope.ClassScope;
  95 import sun.reflect.annotation.*;
  96 
  97 /**

 203  * HelloWorld}, the methods to get the {@linkplain #getName name} and
 204  * {@linkplain #getTypeName type name} return results
 205  * equal to {@code "HelloWorld"}. The {@linkplain #getSimpleName
 206  * simple name} of such an implicitly declared class is {@code "HelloWorld"} and
 207  * the {@linkplain #getCanonicalName canonical name} is {@code "HelloWorld"}.
 208  *
 209  * @param <T> the type of the class modeled by this {@code Class}
 210  * object.  For example, the type of {@code String.class} is {@code
 211  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 212  * unknown.
 213  *
 214  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 215  * @since   1.0
 216  */
 217 public final class Class<T> implements java.io.Serializable,
 218                               GenericDeclaration,
 219                               Type,
 220                               AnnotatedElement,
 221                               TypeDescriptor.OfField<Class<?>>,
 222                               Constable {
 223     private static final int ANNOTATION = 0x00002000;
 224     private static final int ENUM       = 0x00004000;
 225     private static final int SYNTHETIC  = 0x00001000;
 226 
 227     private static native void registerNatives();
 228     static {
 229         runtimeSetup();
 230     }
 231 
 232     // Called from JVM when loading an AOT cache
 233     private static void runtimeSetup() {
 234         registerNatives();
 235     }
 236 
 237     /*
 238      * Private constructor. Only the Java Virtual Machine creates Class objects.
 239      * This constructor is not used and prevents the default constructor being
 240      * generated.
 241      */
 242     private Class(ClassLoader loader, Class<?> arrayComponentType, char mods, ProtectionDomain pd, boolean isPrim) {
 243         // Initialize final field for classLoader.  The initialization value of non-null
 244         // prevents future JIT optimizations from assuming this final field is null.
 245         // The following assignments are done directly by the VM without calling this constructor.

 301      *
 302      * @since 1.8
 303      */
 304     public String toGenericString() {
 305         if (isPrimitive()) {
 306             return toString();
 307         } else {
 308             StringBuilder sb = new StringBuilder();
 309             Class<?> component = this;
 310             int arrayDepth = 0;
 311 
 312             if (isArray()) {
 313                 do {
 314                     arrayDepth++;
 315                     component = component.getComponentType();
 316                 } while (component.isArray());
 317                 sb.append(component.getName());
 318             } else {
 319                 // Class modifiers are a superset of interface modifiers
 320                 int modifiers = getModifiers() & Modifier.classModifiers();
 321                 // Modifier.toString() below mis-interprets SYNCHRONIZED, STRICT, and VOLATILE bits
 322                 modifiers &= ~(Modifier.SYNCHRONIZED | Modifier.STRICT | Modifier.VOLATILE);
 323                 if (modifiers != 0) {
 324                     sb.append(Modifier.toString(modifiers));
 325                     sb.append(' ');
 326                 }
 327 
 328                 // A class cannot be strictfp and sealed/non-sealed so
 329                 // it is sufficient to check for sealed-ness after all
 330                 // modifiers are printed.
 331                 addSealingInfo(modifiers, sb);
 332 
 333                 if (isAnnotation()) {
 334                     sb.append('@');
 335                 }
 336                 if (isInterface()) { // Note: all annotation interfaces are interfaces
 337                     sb.append("interface");
 338                 } else {
 339                     if (isEnum())
 340                         sb.append("enum");
 341                     else {
 342                         if (isValue()) {
 343                             sb.append("value ");
 344                         }
 345                         if (isRecord())
 346                             sb.append("record");
 347                         else
 348                             sb.append("class");
 349                     }
 350                 }
 351                 sb.append(' ');
 352                 sb.append(getName());
 353             }
 354 
 355             TypeVariable<?>[] typeparms = component.getTypeParameters();
 356             if (typeparms.length > 0) {
 357                 sb.append(Arrays.stream(typeparms)
 358                           .map(Class::typeVarBounds)
 359                           .collect(Collectors.joining(",", "<", ">")));
 360             }
 361 
 362             if (arrayDepth > 0) sb.append("[]".repeat(arrayDepth));
 363 
 364             return sb.toString();
 365         }
 366     }
 367 
 368     private void addSealingInfo(int modifiers, StringBuilder sb) {
 369         // A class can be final XOR sealed XOR non-sealed.

 595      * @throws NullPointerException if the given module or name is {@code null}
 596      *
 597      * @throws LinkageError if the linkage fails
 598      *
 599      * @jls 12.2 Loading of Classes and Interfaces
 600      * @jls 12.3 Linking of Classes and Interfaces
 601      * @since 9
 602      */
 603     public static Class<?> forName(Module module, String name) {
 604         Objects.requireNonNull(module);
 605         Objects.requireNonNull(name);
 606 
 607         ClassLoader cl = module.getClassLoader();
 608         if (cl != null) {
 609             return cl.loadClass(module, name);
 610         } else {
 611             return BootLoader.loadClass(module, name);
 612         }
 613     }
 614 
 615     /**
 616      * {@return {@code true} if this {@code Class} object represents an identity class,
 617      * otherwise {@code false}}
 618      *
 619      * <ul>
 620      *      <li>
 621      *          If this {@code Class} object represents an array type this method returns {@code true}.
 622      *      <li>
 623      *          If this {@code Class} object represents an interface, a primitive type,
 624      *          or {@code void} this method returns {@code false}.
 625      *      <li>
 626      *          For all other {@code Class} objects, this method returns {@code true} if either
 627      *          preview features are disabled or {@linkplain Modifier#IDENTITY} is set in the
 628      *          {@linkplain #getModifiers() class modifiers}.
 629      * </ul>
 630      * @see AccessFlag#IDENTITY
 631      * @since Valhalla
 632      */
 633     @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
 634     public boolean isIdentity() {
 635         if (isPrimitive()) {
 636             return false;
 637         } else if (PreviewFeatures.isEnabled()) {
 638            return isArray() || Modifier.isIdentity(modifiers);
 639         } else {
 640             return !isInterface();
 641         }
 642     }
 643 
 644     /**
 645      * {@return {@code true} if this {@code Class} object represents a value class,
 646      * otherwise {@code false}}
 647      * <ul>
 648      *      <li>
 649      *          If this {@code Class} object represents an array type this method returns {@code false}.
 650      *      <li>
 651      *          If this {@code Class} object represents an interface, a primitive type,
 652      *          or {@code void} this method returns {@code true} only if preview features are enabled.
 653      *      <li>
 654      *          For all other {@code Class} objects, this method returns {@code true} only if
 655      *          preview features are enabled and {@linkplain Modifier#IDENTITY} is not set in the
 656      *          {@linkplain #getModifiers() class modifiers}.
 657      * </ul>
 658      * @see AccessFlag#IDENTITY
 659      * @since Valhalla
 660      */
 661     @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
 662     public boolean isValue() {
 663         if (!PreviewFeatures.isEnabled()) {
 664             return false;
 665         }
 666         return !isIdentity();
 667     }
 668 
 669     /**
 670      * {@return the {@code Class} object associated with the
 671      * {@linkplain #isPrimitive() primitive type} of the given name}
 672      * If the argument is not the name of a primitive type, {@code
 673      * null} is returned.
 674      *
 675      * @param primitiveName the name of the primitive type to find
 676      *
 677      * @throws NullPointerException if the argument is {@code null}
 678      *
 679      * @jls 4.2 Primitive Types and Values
 680      * @jls 15.8.2 Class Literals
 681      * @since 22
 682      */
 683     public static Class<?> forPrimitiveName(String primitiveName) {
 684         return switch(primitiveName) {
 685         // Integral types
 686         case "int"     -> int.class;
 687         case "long"    -> long.class;
 688         case "short"   -> short.class;

1375         }
1376         return c;
1377     }
1378 
1379     /**
1380      * Returns the Java language modifiers for this class or interface, encoded
1381      * in an integer. The modifiers consist of the Java Virtual Machine's
1382      * constants for {@code public}, {@code protected},
1383      * {@code private}, {@code final}, {@code static},
1384      * {@code abstract} and {@code interface}; they should be decoded
1385      * using the methods of class {@code Modifier}.
1386      *
1387      * <p> If the underlying class is an array class:
1388      * <ul>
1389      * <li> its {@code public}, {@code private} and {@code protected}
1390      *      modifiers are the same as those of its component type
1391      * <li> its {@code abstract} and {@code final} modifiers are always
1392      *      {@code true}
1393      * <li> its interface modifier is always {@code false}, even when
1394      *      the component type is an interface
1395      * <li> its {@code identity} modifier is always true
1396      * </ul>
1397      * If this {@code Class} object represents a primitive type or
1398      * void, its {@code public}, {@code abstract}, and {@code final}
1399      * modifiers are always {@code true}.
1400      * For {@code Class} objects representing void, primitive types, and
1401      * arrays, the values of other modifiers are {@code false} other
1402      * than as specified above.
1403      *
1404      * <p> The modifier encodings are defined in section {@jvms 4.1}
1405      * of <cite>The Java Virtual Machine Specification</cite>.
1406      *
1407      * @return the {@code int} representing the modifiers for this class
1408      * @see     java.lang.reflect.Modifier
1409      * @see #accessFlags()
1410      * @see <a
1411      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1412      * programming language and JVM modeling in core reflection</a>
1413      * @since 1.1
1414      * @jls 8.1.1 Class Modifiers
1415      * @jls 9.1.1 Interface Modifiers
1416      * @jvms 4.1 The {@code ClassFile} Structure
1417      */
1418     public int getModifiers() { return modifiers; }
1419 
1420    /**
1421      * {@return an unmodifiable set of the {@linkplain AccessFlag access
1422      * flags} for this class, possibly empty}
1423      * The {@code AccessFlags} may depend on the class file format version of the class.
1424      *
1425      * <p> If the underlying class is an array class:
1426      * <ul>
1427      * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1428      *      access flags are the same as those of its component type
1429      * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1430      * <li> its {@code INTERFACE} flag is absent, even when the
1431      *      component type is an interface
1432     * <li> its {@code identity} modifier is always true
1433      * </ul>
1434      * If this {@code Class} object represents a primitive type or
1435      * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1436      * {@code FINAL}.
1437      * For {@code Class} objects representing void, primitive types, and
1438      * arrays, access flags are absent other than as specified above.
1439      *
1440      * @see #getModifiers()
1441      * @jvms 4.1 The ClassFile Structure
1442      * @jvms 4.7.6 The InnerClasses Attribute
1443      * @since 20
1444      */
1445     public Set<AccessFlag> accessFlags() {
1446         // Location.CLASS allows SUPER and AccessFlag.MODULE which
1447         // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1448         // and STATIC, which are not allowed on Location.CLASS.
1449         // Use getClassAccessFlagsRaw to expose SUPER status.
1450         var location = (isMemberClass() || isLocalClass() ||
1451                         isAnonymousClass() || isArray()) ?
1452             AccessFlag.Location.INNER_CLASS :
1453             AccessFlag.Location.CLASS;
1454         int accessFlags = (location == AccessFlag.Location.CLASS) ?
1455                 getClassAccessFlagsRaw() : getModifiers();
1456         if (isArray() && PreviewFeatures.isEnabled()) {
1457             accessFlags |= Modifier.IDENTITY;
1458         }
1459         var cffv = ClassFileFormatVersion.fromMajor(getClassFileVersion() & 0xffff);
1460         if (cffv.compareTo(ClassFileFormatVersion.latest()) >= 0) {
1461             // Ignore unspecified (0x0800) access flag for current version
1462             accessFlags &= ~0x0800;
1463         }
1464         return AccessFlag.maskToAccessFlags(accessFlags, location, cffv);
1465     }
1466 
1467    /**
1468      * Gets the signers of this class.
1469      *
1470      * @return  the signers of this class, or null if there are no signers.  In
1471      *          particular, this method returns null if this {@code Class} object represents
1472      *          a primitive type or void.
1473      * @since   1.1
1474      */
1475 
1476     public Object[] getSigners() {
1477         var signers = this.signers;
1478         return signers == null ? null : signers.clone();
1479     }
1480 
1481     /**
1482      * Set the signers of this class.
1483      */
1484     void setSigners(Object[] signers) {
1485         if (!isPrimitive() && !isArray()) {
1486             this.signers = signers;
1487         }
1488     }
1489 
1490     /**
1491      * If this {@code Class} object represents a local or anonymous
1492      * class within a method, returns a {@link
1493      * java.lang.reflect.Method Method} object representing the
1494      * immediately enclosing method of the underlying class. Returns
1495      * {@code null} otherwise.

3843      *
3844      * <p> If this {@code Class} object represents an interface, the return
3845      * value is an array containing objects representing the uses of interface
3846      * types to specify interfaces directly extended by the interface. The
3847      * order of the objects in the array corresponds to the order of the
3848      * interface types used in the 'extends' clause of the declaration of this
3849      * {@code Class} object.
3850      *
3851      * <p> If this {@code Class} object represents a class or interface whose
3852      * declaration does not explicitly indicate any annotated superinterfaces,
3853      * the return value is an array of length 0.
3854      *
3855      * <p> If this {@code Class} object represents either the {@code Object}
3856      * class, an array type, a primitive type, or void, the return value is an
3857      * array of length 0.
3858      *
3859      * @return an array representing the superinterfaces
3860      * @since 1.8
3861      */
3862     public AnnotatedType[] getAnnotatedInterfaces() {
3863         return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
3864     }
3865 
3866     private native Class<?> getNestHost0();
3867 
3868     /**
3869      * Returns the nest host of the <a href=#nest>nest</a> to which the class
3870      * or interface represented by this {@code Class} object belongs.
3871      * Every class and interface belongs to exactly one nest.
3872      *
3873      * If the nest host of this class or interface has previously
3874      * been determined, then this method returns the nest host.
3875      * If the nest host of this class or interface has
3876      * not previously been determined, then this method determines the nest
3877      * host using the algorithm of JVMS 5.4.4, and returns it.
3878      *
3879      * Often, a class or interface belongs to a nest consisting only of itself,
3880      * in which case this method returns {@code this} to indicate that the class
3881      * or interface is the nest host.
3882      *
3883      * <p>If this {@code Class} object represents a primitive type, an array type,

4183      * @since 17
4184      */
4185     public boolean isSealed() {
4186         if (isArray() || isPrimitive()) {
4187             return false;
4188         }
4189         return getPermittedSubclasses() != null;
4190     }
4191 
4192     private native Class<?>[] getPermittedSubclasses0();
4193 
4194     /*
4195      * Return the class's major and minor class file version packed into an int.
4196      * The high order 16 bits contain the class's minor version.  The low order
4197      * 16 bits contain the class's major version.
4198      *
4199      * If the class is an array type then the class file version of its element
4200      * type is returned.  If the class is a primitive type then the latest class
4201      * file major version is returned and zero is returned for the minor version.
4202      */
4203     /* package-private */
4204     int getClassFileVersion() {
4205         Class<?> c = isArray() ? elementType() : this;
4206         return c.getClassFileVersion0();
4207     }
4208 
4209     private native int getClassFileVersion0();
4210 
4211     /*
4212      * Return the access flags as they were in the class's bytecode, including
4213      * the original setting of ACC_SUPER.
4214      *
4215      * If the class is an array type then the access flags of the element type is
4216      * returned.  If the class is a primitive then ACC_ABSTRACT | ACC_FINAL | ACC_PUBLIC.
4217      */
4218     private int getClassAccessFlagsRaw() {
4219         Class<?> c = isArray() ? elementType() : this;
4220         return c.getClassAccessFlagsRaw0();
4221     }
4222 
4223     private native int getClassAccessFlagsRaw0();
4224 }
< prev index next >