< 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;

 208  * HelloWorld}, the methods to get the {@linkplain #getName name} and
 209  * {@linkplain #getTypeName type name} return results
 210  * equal to {@code "HelloWorld"}. The {@linkplain #getSimpleName
 211  * simple name} of such an implicitly declared class is {@code "HelloWorld"} and
 212  * the {@linkplain #getCanonicalName canonical name} is {@code "HelloWorld"}.
 213  *
 214  * @param <T> the type of the class modeled by this {@code Class}
 215  * object.  For example, the type of {@code String.class} is {@code
 216  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 217  * unknown.
 218  *
 219  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 220  * @since   1.0
 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                 if (modifiers != 0) {
 318                     sb.append(Modifier.toString(modifiers));
 319                     sb.append(' ');
 320                 }
 321 
 322                 // A class cannot be strictfp and sealed/non-sealed so
 323                 // it is sufficient to check for sealed-ness after all
 324                 // modifiers are printed.
 325                 addSealingInfo(modifiers, sb);
 326 
 327                 if (isAnnotation()) {
 328                     sb.append('@');
 329                 }



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

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

































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

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

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

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

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







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

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

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

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

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

  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;

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

 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                 // Modifier.toString() below mis-interprets SYNCHRONIZED, STRICT, and VOLATILE bits
 319                 modifiers &= ~(Modifier.SYNCHRONIZED | Modifier.STRICT | Modifier.VOLATILE);
 320                 if (modifiers != 0) {
 321                     sb.append(Modifier.toString(modifiers));
 322                     sb.append(' ');
 323                 }
 324 
 325                 // A class cannot be strictfp and sealed/non-sealed so
 326                 // it is sufficient to check for sealed-ness after all
 327                 // modifiers are printed.
 328                 addSealingInfo(modifiers, sb);
 329 
 330                 if (isAnnotation()) {
 331                     sb.append('@');
 332                 }
 333                 if (isValue()) {
 334                     sb.append("value ");
 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 if (isRecord())
 342                         sb.append("record");
 343                     else
 344                         sb.append("class");
 345                 }
 346                 sb.append(' ');
 347                 sb.append(getName());
 348             }
 349 
 350             TypeVariable<?>[] typeparms = component.getTypeParameters();
 351             if (typeparms.length > 0) {
 352                 sb.append(Arrays.stream(typeparms)
 353                           .map(Class::typeVarBounds)
 354                           .collect(Collectors.joining(",", "<", ">")));
 355             }

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

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

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

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