< prev index next >

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

Print this page

  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.module.ModuleReader;
  34 import java.lang.ref.SoftReference;
  35 import java.io.IOException;
  36 import java.io.InputStream;
  37 import java.io.ObjectStreamField;
  38 import java.lang.reflect.AnnotatedElement;
  39 import java.lang.reflect.AnnotatedType;
  40 import java.lang.reflect.AccessFlag;
  41 import java.lang.reflect.Array;

  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.AccessController;
  58 import java.security.Permissions;
  59 import java.security.PrivilegedAction;
  60 import java.security.ProtectionDomain;
  61 import java.util.ArrayList;

 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  * @jls 15.8.2 Class Literals
 220  */
 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         registerNatives();
 234     }
 235 
 236     /*
 237      * Private constructor. Only the Java Virtual Machine creates Class objects.
 238      * This constructor is not used and prevents the default constructor being
 239      * generated.
 240      */
 241     private Class(ClassLoader loader, Class<?> arrayComponentType) {
 242         // Initialize final field for classLoader.  The initialization value of non-null
 243         // prevents future JIT optimizations from assuming this final field is null.
 244         classLoader = loader;
 245         componentType = arrayComponentType;
 246     }
 247 
 248     /**
 249      * Converts the object to a string. The string representation is the

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


 316                 if (modifiers != 0) {
 317                     sb.append(Modifier.toString(modifiers));
 318                     sb.append(' ');
 319                 }
 320 
 321                 // A class cannot be strictfp and sealed/non-sealed so
 322                 // it is sufficient to check for sealed-ness after all
 323                 // modifiers are printed.
 324                 addSealingInfo(modifiers, sb);
 325 
 326                 if (isAnnotation()) {
 327                     sb.append('@');
 328                 }



 329                 if (isInterface()) { // Note: all annotation interfaces are interfaces
 330                     sb.append("interface");
 331                 } else {
 332                     if (isEnum())
 333                         sb.append("enum");
 334                     else if (isRecord())
 335                         sb.append("record");
 336                     else
 337                         sb.append("class");
 338                 }
 339                 sb.append(' ');
 340                 sb.append(getName());
 341             }
 342 
 343             TypeVariable<?>[] typeparms = component.getTypeParameters();
 344             if (typeparms.length > 0) {
 345                 sb.append(Arrays.stream(typeparms)
 346                           .map(Class::typeVarBounds)
 347                           .collect(Collectors.joining(",", "<", ">")));
 348             }

 658         SecurityManager sm = System.getSecurityManager();
 659         if (sm != null) {
 660             if (caller != null && caller.getModule() != module) {
 661                 // if caller is null, Class.forName is the last java frame on the stack.
 662                 // java.base has all permissions
 663                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
 664             }
 665             PrivilegedAction<ClassLoader> pa = module::getClassLoader;
 666             cl = AccessController.doPrivileged(pa);
 667         } else {
 668             cl = module.getClassLoader();
 669         }
 670 
 671         if (cl != null) {
 672             return cl.loadClass(module, name);
 673         } else {
 674             return BootLoader.loadClass(module, name);
 675         }
 676     }
 677 

































 678     /**
 679      * {@return the {@code Class} object associated with the
 680      * {@linkplain #isPrimitive() primitive type} of the given name}
 681      * If the argument is not the name of a primitive type, {@code
 682      * null} is returned.
 683      *
 684      * @param primitiveName the name of the primitive type to find
 685      *
 686      * @throws NullPointerException if the argument is {@code null}
 687      *
 688      * @jls 4.2 Primitive Types and Values
 689      * @jls 15.8.2 Class Literals
 690      * @since 22
 691      */
 692     public static Class<?> forPrimitiveName(String primitiveName) {
 693         return switch(primitiveName) {
 694         // Integral types
 695         case "int"     -> int.class;
 696         case "long"    -> long.class;
 697         case "short"   -> short.class;

1424         }
1425         return c;
1426     }
1427 
1428     /**
1429      * Returns the Java language modifiers for this class or interface, encoded
1430      * in an integer. The modifiers consist of the Java Virtual Machine's
1431      * constants for {@code public}, {@code protected},
1432      * {@code private}, {@code final}, {@code static},
1433      * {@code abstract} and {@code interface}; they should be decoded
1434      * using the methods of class {@code Modifier}.
1435      *
1436      * <p> If the underlying class is an array class:
1437      * <ul>
1438      * <li> its {@code public}, {@code private} and {@code protected}
1439      *      modifiers are the same as those of its component type
1440      * <li> its {@code abstract} and {@code final} modifiers are always
1441      *      {@code true}
1442      * <li> its interface modifier is always {@code false}, even when
1443      *      the component type is an interface

1444      * </ul>
1445      * If this {@code Class} object represents a primitive type or
1446      * void, its {@code public}, {@code abstract}, and {@code final}
1447      * modifiers are always {@code true}.
1448      * For {@code Class} objects representing void, primitive types, and
1449      * arrays, the values of other modifiers are {@code false} other
1450      * than as specified above.
1451      *
1452      * <p> The modifier encodings are defined in section {@jvms 4.1}
1453      * of <cite>The Java Virtual Machine Specification</cite>.
1454      *
1455      * @return the {@code int} representing the modifiers for this class
1456      * @see     java.lang.reflect.Modifier
1457      * @see #accessFlags()
1458      * @see <a
1459      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1460      * programming language and JVM modeling in core reflection</a>
1461      * @since 1.1
1462      * @jls 8.1.1 Class Modifiers
1463      * @jls 9.1.1. Interface Modifiers
1464      * @jvms 4.1 The {@code ClassFile} Structure
1465      */
1466     @IntrinsicCandidate
1467     public native int getModifiers();
1468 
1469     /**
1470      * {@return an unmodifiable set of the {@linkplain AccessFlag access
1471      * flags} for this class, possibly empty}

1472      *
1473      * <p> If the underlying class is an array class:
1474      * <ul>
1475      * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1476      *      access flags are the same as those of its component type
1477      * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1478      * <li> its {@code INTERFACE} flag is absent, even when the
1479      *      component type is an interface

1480      * </ul>
1481      * If this {@code Class} object represents a primitive type or
1482      * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1483      * {@code FINAL}.
1484      * For {@code Class} objects representing void, primitive types, and
1485      * arrays, access flags are absent other than as specified above.
1486      *
1487      * @see #getModifiers()
1488      * @jvms 4.1 The ClassFile Structure
1489      * @jvms 4.7.6 The InnerClasses Attribute
1490      * @since 20
1491      */
1492     public Set<AccessFlag> accessFlags() {
1493         // Location.CLASS allows SUPER and AccessFlag.MODULE which
1494         // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1495         // and STATIC, which are not allowed on Location.CLASS.
1496         // Use getClassAccessFlagsRaw to expose SUPER status.
1497         var location = (isMemberClass() || isLocalClass() ||
1498                         isAnonymousClass() || isArray()) ?
1499             AccessFlag.Location.INNER_CLASS :
1500             AccessFlag.Location.CLASS;
1501         return AccessFlag.maskToAccessFlags((location == AccessFlag.Location.CLASS) ?
1502                                             getClassAccessFlagsRaw() :
1503                                             getModifiers(),
1504                                             location);







1505     }
1506 
1507     /**
1508      * Gets the signers of this class.
1509      *
1510      * @return  the signers of this class, or null if there are no signers.  In
1511      *          particular, this method returns null if this {@code Class} object represents
1512      *          a primitive type or void.
1513      * @since   1.1
1514      */

1515     public Object[] getSigners() {
1516         var signers = this.signers;
1517         return signers == null ? null : signers.clone();
1518     }
1519 
1520     /**
1521      * Set the signers of this class.
1522      */
1523     void setSigners(Object[] signers) {
1524         if (!isPrimitive() && !isArray()) {
1525             this.signers = signers;
1526         }
1527     }
1528 
1529     /**
1530      * If this {@code Class} object represents a local or anonymous
1531      * class within a method, returns a {@link
1532      * java.lang.reflect.Method Method} object representing the
1533      * immediately enclosing method of the underlying class. Returns
1534      * {@code null} otherwise.

4421      *
4422      * <p> If this {@code Class} object represents an interface, the return
4423      * value is an array containing objects representing the uses of interface
4424      * types to specify interfaces directly extended by the interface. The
4425      * order of the objects in the array corresponds to the order of the
4426      * interface types used in the 'extends' clause of the declaration of this
4427      * {@code Class} object.
4428      *
4429      * <p> If this {@code Class} object represents a class or interface whose
4430      * declaration does not explicitly indicate any annotated superinterfaces,
4431      * the return value is an array of length 0.
4432      *
4433      * <p> If this {@code Class} object represents either the {@code Object}
4434      * class, an array type, a primitive type, or void, the return value is an
4435      * array of length 0.
4436      *
4437      * @return an array representing the superinterfaces
4438      * @since 1.8
4439      */
4440     public AnnotatedType[] getAnnotatedInterfaces() {
4441          return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
4442     }
4443 
4444     private native Class<?> getNestHost0();
4445 
4446     /**
4447      * Returns the nest host of the <a href=#nest>nest</a> to which the class
4448      * or interface represented by this {@code Class} object belongs.
4449      * Every class and interface belongs to exactly one nest.
4450      *
4451      * If the nest host of this class or interface has previously
4452      * been determined, then this method returns the nest host.
4453      * If the nest host of this class or interface has
4454      * not previously been determined, then this method determines the nest
4455      * host using the algorithm of JVMS 5.4.4, and returns it.
4456      *
4457      * Often, a class or interface belongs to a nest consisting only of itself,
4458      * in which case this method returns {@code this} to indicate that the class
4459      * or interface is the nest host.
4460      *
4461      * <p>If this {@code Class} object represents a primitive type, an array type,

4819      * @since 17
4820      */
4821     public boolean isSealed() {
4822         if (isArray() || isPrimitive()) {
4823             return false;
4824         }
4825         return getPermittedSubclasses() != null;
4826     }
4827 
4828     private native Class<?>[] getPermittedSubclasses0();
4829 
4830     /*
4831      * Return the class's major and minor class file version packed into an int.
4832      * The high order 16 bits contain the class's minor version.  The low order
4833      * 16 bits contain the class's major version.
4834      *
4835      * If the class is an array type then the class file version of its element
4836      * type is returned.  If the class is a primitive type then the latest class
4837      * file major version is returned and zero is returned for the minor version.
4838      */
4839     private int getClassFileVersion() {

4840         Class<?> c = isArray() ? elementType() : this;
4841         return c.getClassFileVersion0();
4842     }
4843 
4844     private native int getClassFileVersion0();
4845 
4846     /*
4847      * Return the access flags as they were in the class's bytecode, including
4848      * the original setting of ACC_SUPER.
4849      *
4850      * If the class is an array type then the access flags of the element type is
4851      * returned.  If the class is a primitive then ACC_ABSTRACT | ACC_FINAL | ACC_PUBLIC.
4852      */
4853     private int getClassAccessFlagsRaw() {
4854         Class<?> c = isArray() ? elementType() : this;
4855         return c.getClassAccessFlagsRaw0();
4856     }
4857 
4858     private native int getClassAccessFlagsRaw0();
4859 }

  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.module.ModuleReader;
  34 import java.lang.ref.SoftReference;
  35 import java.io.IOException;
  36 import java.io.InputStream;
  37 import java.io.ObjectStreamField;
  38 import java.lang.reflect.AnnotatedElement;
  39 import java.lang.reflect.AnnotatedType;
  40 import java.lang.reflect.AccessFlag;
  41 import java.lang.reflect.Array;
  42 import java.lang.reflect.ClassFileFormatVersion;
  43 import java.lang.reflect.Constructor;
  44 import java.lang.reflect.Executable;
  45 import java.lang.reflect.Field;
  46 import java.lang.reflect.GenericArrayType;
  47 import java.lang.reflect.GenericDeclaration;
  48 import java.lang.reflect.InvocationTargetException;
  49 import java.lang.reflect.Member;
  50 import java.lang.reflect.Method;
  51 import java.lang.reflect.Modifier;
  52 import java.lang.reflect.Proxy;
  53 import java.lang.reflect.RecordComponent;
  54 import java.lang.reflect.Type;
  55 import java.lang.reflect.TypeVariable;
  56 import java.lang.constant.Constable;
  57 import java.net.URL;
  58 import java.security.AccessController;
  59 import java.security.Permissions;
  60 import java.security.PrivilegedAction;
  61 import java.security.ProtectionDomain;
  62 import java.util.ArrayList;

 208  * {@linkplain #getTypeName type name} return results
 209  * equal to {@code "HelloWorld"}. The {@linkplain #getSimpleName
 210  * simple name} of such an implicitly declared class is {@code "HelloWorld"} and
 211  * the {@linkplain #getCanonicalName canonical name} is {@code "HelloWorld"}.
 212  *
 213  * @param <T> the type of the class modeled by this {@code Class}
 214  * object.  For example, the type of {@code String.class} is {@code
 215  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 216  * unknown.
 217  *
 218  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 219  * @since   1.0
 220  * @jls 15.8.2 Class Literals
 221  */
 222 public final class Class<T> implements java.io.Serializable,
 223                               GenericDeclaration,
 224                               Type,
 225                               AnnotatedElement,
 226                               TypeDescriptor.OfField<Class<?>>,
 227                               Constable {
 228     private static final int ANNOTATION = 0x00002000;
 229     private static final int ENUM       = 0x00004000;
 230     private static final int SYNTHETIC  = 0x00001000;
 231 
 232     private static native void registerNatives();
 233     static {
 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) {
 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         classLoader = loader;
 246         componentType = arrayComponentType;
 247     }
 248 
 249     /**
 250      * Converts the object to a string. The string representation is the

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

 664         SecurityManager sm = System.getSecurityManager();
 665         if (sm != null) {
 666             if (caller != null && caller.getModule() != module) {
 667                 // if caller is null, Class.forName is the last java frame on the stack.
 668                 // java.base has all permissions
 669                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
 670             }
 671             PrivilegedAction<ClassLoader> pa = module::getClassLoader;
 672             cl = AccessController.doPrivileged(pa);
 673         } else {
 674             cl = module.getClassLoader();
 675         }
 676 
 677         if (cl != null) {
 678             return cl.loadClass(module, name);
 679         } else {
 680             return BootLoader.loadClass(module, name);
 681         }
 682     }
 683 
 684     /**
 685      * {@return {@code true} if this {@code Class} object represents an identity
 686      * class or interface; otherwise {@code false}}
 687      *
 688      * If this {@code Class} object represents an array type, then this method
 689      * returns {@code true}.
 690      * If this {@code Class} object represents a primitive type, or {@code void},
 691      * then this method returns {@code false}.
 692      *
 693      * @since Valhalla
 694      */
 695     @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
 696     public native boolean isIdentity();
 697 
 698     /**
 699      * {@return {@code true} if this {@code Class} object represents a value
 700      * class; otherwise {@code false}}
 701      *
 702      * If this {@code Class} object represents an array type, an interface,
 703      * a primitive type, or {@code void}, then this method returns {@code false}.
 704      *
 705      * @since Valhalla
 706      */
 707     @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
 708     public boolean isValue() {
 709         if (!PreviewFeatures.isEnabled()) {
 710             return false;
 711         }
 712          if (isPrimitive() || isArray() || isInterface())
 713              return false;
 714         return ((getModifiers() & Modifier.IDENTITY) == 0);
 715     }
 716 
 717     /**
 718      * {@return the {@code Class} object associated with the
 719      * {@linkplain #isPrimitive() primitive type} of the given name}
 720      * If the argument is not the name of a primitive type, {@code
 721      * null} is returned.
 722      *
 723      * @param primitiveName the name of the primitive type to find
 724      *
 725      * @throws NullPointerException if the argument is {@code null}
 726      *
 727      * @jls 4.2 Primitive Types and Values
 728      * @jls 15.8.2 Class Literals
 729      * @since 22
 730      */
 731     public static Class<?> forPrimitiveName(String primitiveName) {
 732         return switch(primitiveName) {
 733         // Integral types
 734         case "int"     -> int.class;
 735         case "long"    -> long.class;
 736         case "short"   -> short.class;

1463         }
1464         return c;
1465     }
1466 
1467     /**
1468      * Returns the Java language modifiers for this class or interface, encoded
1469      * in an integer. The modifiers consist of the Java Virtual Machine's
1470      * constants for {@code public}, {@code protected},
1471      * {@code private}, {@code final}, {@code static},
1472      * {@code abstract} and {@code interface}; they should be decoded
1473      * using the methods of class {@code Modifier}.
1474      *
1475      * <p> If the underlying class is an array class:
1476      * <ul>
1477      * <li> its {@code public}, {@code private} and {@code protected}
1478      *      modifiers are the same as those of its component type
1479      * <li> its {@code abstract} and {@code final} modifiers are always
1480      *      {@code true}
1481      * <li> its interface modifier is always {@code false}, even when
1482      *      the component type is an interface
1483      * <li> its {@code identity} modifier is always true
1484      * </ul>
1485      * If this {@code Class} object represents a primitive type or
1486      * void, its {@code public}, {@code abstract}, and {@code final}
1487      * modifiers are always {@code true}.
1488      * For {@code Class} objects representing void, primitive types, and
1489      * arrays, the values of other modifiers are {@code false} other
1490      * than as specified above.
1491      *
1492      * <p> The modifier encodings are defined in section {@jvms 4.1}
1493      * of <cite>The Java Virtual Machine Specification</cite>.
1494      *
1495      * @return the {@code int} representing the modifiers for this class
1496      * @see     java.lang.reflect.Modifier
1497      * @see #accessFlags()
1498      * @see <a
1499      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1500      * programming language and JVM modeling in core reflection</a>
1501      * @since 1.1
1502      * @jls 8.1.1 Class Modifiers
1503      * @jls 9.1.1. Interface Modifiers
1504      * @jvms 4.1 The {@code ClassFile} Structure
1505      */
1506     @IntrinsicCandidate
1507     public native int getModifiers();
1508 
1509    /**
1510      * {@return an unmodifiable set of the {@linkplain AccessFlag access
1511      * flags} for this class, possibly empty}
1512      * The {@code AccessFlags} may depend on the class file format version of the class.
1513      *
1514      * <p> If the underlying class is an array class:
1515      * <ul>
1516      * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1517      *      access flags are the same as those of its component type
1518      * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1519      * <li> its {@code INTERFACE} flag is absent, even when the
1520      *      component type is an interface
1521     * <li> its {@code identity} modifier is always true
1522      * </ul>
1523      * If this {@code Class} object represents a primitive type or
1524      * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1525      * {@code FINAL}.
1526      * For {@code Class} objects representing void, primitive types, and
1527      * arrays, access flags are absent other than as specified above.
1528      *
1529      * @see #getModifiers()
1530      * @jvms 4.1 The ClassFile Structure
1531      * @jvms 4.7.6 The InnerClasses Attribute
1532      * @since 20
1533      */
1534     public Set<AccessFlag> accessFlags() {
1535         // Location.CLASS allows SUPER and AccessFlag.MODULE which
1536         // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1537         // and STATIC, which are not allowed on Location.CLASS.
1538         // Use getClassAccessFlagsRaw to expose SUPER status.
1539         var location = (isMemberClass() || isLocalClass() ||
1540                         isAnonymousClass() || isArray()) ?
1541             AccessFlag.Location.INNER_CLASS :
1542             AccessFlag.Location.CLASS;
1543         int accessFlags = (location == AccessFlag.Location.CLASS) ?
1544                 getClassAccessFlagsRaw() : getModifiers();
1545         if (isArray() && PreviewFeatures.isEnabled()) {
1546             accessFlags |= Modifier.IDENTITY;
1547         }
1548         var cffv = ClassFileFormatVersion.fromMajor(getClassFileVersion() & 0xffff);
1549         if (cffv.compareTo(ClassFileFormatVersion.latest()) >= 0) {
1550             // Ignore unspecified (0x0800) access flag for current version
1551             accessFlags &= ~0x0800;
1552         }
1553         return AccessFlag.maskToAccessFlags(accessFlags, location, cffv);
1554     }
1555 
1556    /**
1557      * Gets the signers of this class.
1558      *
1559      * @return  the signers of this class, or null if there are no signers.  In
1560      *          particular, this method returns null if this {@code Class} object represents
1561      *          a primitive type or void.
1562      * @since   1.1
1563      */
1564 
1565     public Object[] getSigners() {
1566         var signers = this.signers;
1567         return signers == null ? null : signers.clone();
1568     }
1569 
1570     /**
1571      * Set the signers of this class.
1572      */
1573     void setSigners(Object[] signers) {
1574         if (!isPrimitive() && !isArray()) {
1575             this.signers = signers;
1576         }
1577     }
1578 
1579     /**
1580      * If this {@code Class} object represents a local or anonymous
1581      * class within a method, returns a {@link
1582      * java.lang.reflect.Method Method} object representing the
1583      * immediately enclosing method of the underlying class. Returns
1584      * {@code null} otherwise.

4471      *
4472      * <p> If this {@code Class} object represents an interface, the return
4473      * value is an array containing objects representing the uses of interface
4474      * types to specify interfaces directly extended by the interface. The
4475      * order of the objects in the array corresponds to the order of the
4476      * interface types used in the 'extends' clause of the declaration of this
4477      * {@code Class} object.
4478      *
4479      * <p> If this {@code Class} object represents a class or interface whose
4480      * declaration does not explicitly indicate any annotated superinterfaces,
4481      * the return value is an array of length 0.
4482      *
4483      * <p> If this {@code Class} object represents either the {@code Object}
4484      * class, an array type, a primitive type, or void, the return value is an
4485      * array of length 0.
4486      *
4487      * @return an array representing the superinterfaces
4488      * @since 1.8
4489      */
4490     public AnnotatedType[] getAnnotatedInterfaces() {
4491         return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
4492     }
4493 
4494     private native Class<?> getNestHost0();
4495 
4496     /**
4497      * Returns the nest host of the <a href=#nest>nest</a> to which the class
4498      * or interface represented by this {@code Class} object belongs.
4499      * Every class and interface belongs to exactly one nest.
4500      *
4501      * If the nest host of this class or interface has previously
4502      * been determined, then this method returns the nest host.
4503      * If the nest host of this class or interface has
4504      * not previously been determined, then this method determines the nest
4505      * host using the algorithm of JVMS 5.4.4, and returns it.
4506      *
4507      * Often, a class or interface belongs to a nest consisting only of itself,
4508      * in which case this method returns {@code this} to indicate that the class
4509      * or interface is the nest host.
4510      *
4511      * <p>If this {@code Class} object represents a primitive type, an array type,

4869      * @since 17
4870      */
4871     public boolean isSealed() {
4872         if (isArray() || isPrimitive()) {
4873             return false;
4874         }
4875         return getPermittedSubclasses() != null;
4876     }
4877 
4878     private native Class<?>[] getPermittedSubclasses0();
4879 
4880     /*
4881      * Return the class's major and minor class file version packed into an int.
4882      * The high order 16 bits contain the class's minor version.  The low order
4883      * 16 bits contain the class's major version.
4884      *
4885      * If the class is an array type then the class file version of its element
4886      * type is returned.  If the class is a primitive type then the latest class
4887      * file major version is returned and zero is returned for the minor version.
4888      */
4889     /* package-private */
4890     int getClassFileVersion() {
4891         Class<?> c = isArray() ? elementType() : this;
4892         return c.getClassFileVersion0();
4893     }
4894 
4895     private native int getClassFileVersion0();
4896 
4897     /*
4898      * Return the access flags as they were in the class's bytecode, including
4899      * the original setting of ACC_SUPER.
4900      *
4901      * If the class is an array type then the access flags of the element type is
4902      * returned.  If the class is a primitive then ACC_ABSTRACT | ACC_FINAL | ACC_PUBLIC.
4903      */
4904     private int getClassAccessFlagsRaw() {
4905         Class<?> c = isArray() ? elementType() : this;
4906         return c.getClassAccessFlagsRaw0();
4907     }
4908 
4909     private native int getClassAccessFlagsRaw0();
4910 }
< prev index next >