< 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.PrivilegedAction;
  59 import java.util.ArrayList;
  60 import java.util.Arrays;
  61 import java.util.Collection;

 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  * @jls 15.8.2 Class Literals
 217  */
 218 public final class Class<T> implements java.io.Serializable,
 219                               GenericDeclaration,
 220                               Type,
 221                               AnnotatedElement,
 222                               TypeDescriptor.OfField<Class<?>>,
 223                               Constable {
 224     private static final int ANNOTATION= 0x00002000;
 225     private static final int ENUM      = 0x00004000;
 226     private static final int SYNTHETIC = 0x00001000;
 227 
 228     private static native void registerNatives();
 229     static {
 230         registerNatives();
 231     }
 232 
 233     /*
 234      * Private constructor. Only the Java Virtual Machine creates Class objects.
 235      * This constructor is not used and prevents the default constructor being
 236      * generated.
 237      */
 238     private Class(ClassLoader loader, Class<?> arrayComponentType) {
 239         // Initialize final field for classLoader.  The initialization value of non-null
 240         // prevents future JIT optimizations from assuming this final field is null.
 241         classLoader = loader;
 242         componentType = arrayComponentType;
 243     }
 244 
 245     /**
 246      * Converts the object to a string. The string representation is the

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


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



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

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

































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

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

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

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

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







1502     }
1503 
1504     /**
1505      * Gets the signers of this class.
1506      *
1507      * @return  the signers of this class, or null if there are no signers.  In
1508      *          particular, this method returns null if this {@code Class} object represents
1509      *          a primitive type or void.
1510      * @since   1.1
1511      */
1512     public native Object[] getSigners();
1513 
1514 
1515     /**
1516      * Set the signers of this class.
1517      */
1518     native void setSigners(Object[] signers);
1519 
1520 
1521     /**
1522      * If this {@code Class} object represents a local or anonymous
1523      * class within a method, returns a {@link
1524      * java.lang.reflect.Method Method} object representing the
1525      * immediately enclosing method of the underlying class. Returns
1526      * {@code null} otherwise.
1527      *
1528      * In particular, this method returns {@code null} if the underlying
1529      * class is a local or anonymous class immediately enclosed by a class or
1530      * interface declaration, instance initializer or static initializer.
1531      *
1532      * @return the immediately enclosing method of the underlying class, if
1533      *     that class is a local or anonymous class; otherwise {@code null}.
1534      *
1535      * @throws SecurityException
1536      *         If a security manager, <i>s</i>, is present and any of the
1537      *         following conditions is met:
1538      *
1539      *         <ul>
1540      *

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

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

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

  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.PrivilegedAction;
  60 import java.util.ArrayList;
  61 import java.util.Arrays;
  62 import java.util.Collection;

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

 294      *
 295      * @since 1.8
 296      */
 297     public String toGenericString() {
 298         if (isPrimitive()) {
 299             return toString();
 300         } else {
 301             StringBuilder sb = new StringBuilder();
 302             Class<?> component = this;
 303             int arrayDepth = 0;
 304 
 305             if (isArray()) {
 306                 do {
 307                     arrayDepth++;
 308                     component = component.getComponentType();
 309                 } while (component.isArray());
 310                 sb.append(component.getName());
 311             } else {
 312                 // Class modifiers are a superset of interface modifiers
 313                 int modifiers = getModifiers() & Modifier.classModifiers();
 314                 // Modifier.toString() below mis-interprets SYNCHRONIZED, STRICT, and VOLATILE bits
 315                 modifiers &= ~(Modifier.SYNCHRONIZED | Modifier.STRICT | Modifier.VOLATILE);
 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 (isValue()) {
 330                     sb.append("value ");
 331                 }
 332                 if (isInterface()) { // Note: all annotation interfaces are interfaces
 333                     sb.append("interface");
 334                 } else {
 335                     if (isEnum())
 336                         sb.append("enum");
 337                     else if (isRecord())
 338                         sb.append("record");
 339                     else
 340                         sb.append("class");
 341                 }
 342                 sb.append(' ');
 343                 sb.append(getName());
 344             }
 345 
 346             TypeVariable<?>[] typeparms = component.getTypeParameters();
 347             if (typeparms.length > 0) {
 348                 sb.append(Arrays.stream(typeparms)
 349                           .map(Class::typeVarBounds)
 350                           .collect(Collectors.joining(",", "<", ">")));
 351             }

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

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

1563     /**
1564      * Set the signers of this class.
1565      */
1566     native void setSigners(Object[] signers);
1567 

1568     /**
1569      * If this {@code Class} object represents a local or anonymous
1570      * class within a method, returns a {@link
1571      * java.lang.reflect.Method Method} object representing the
1572      * immediately enclosing method of the underlying class. Returns
1573      * {@code null} otherwise.
1574      *
1575      * In particular, this method returns {@code null} if the underlying
1576      * class is a local or anonymous class immediately enclosed by a class or
1577      * interface declaration, instance initializer or static initializer.
1578      *
1579      * @return the immediately enclosing method of the underlying class, if
1580      *     that class is a local or anonymous class; otherwise {@code null}.
1581      *
1582      * @throws SecurityException
1583      *         If a security manager, <i>s</i>, is present and any of the
1584      *         following conditions is met:
1585      *
1586      *         <ul>
1587      *

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

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