< prev index next >

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

Print this page

   1 /*
   2  * Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.lang.constant.ClassDesc;
  30 import java.lang.constant.ConstantDescs;
  31 import java.lang.invoke.TypeDescriptor;
  32 import java.lang.invoke.MethodHandles;
  33 import java.lang.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;

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

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


 321                 if (modifiers != 0) {
 322                     sb.append(Modifier.toString(modifiers));
 323                     sb.append(' ');
 324                 }
 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             }

 615         SecurityManager sm = System.getSecurityManager();
 616         if (sm != null) {
 617             if (caller != null && caller.getModule() != module) {
 618                 // if caller is null, Class.forName is the last java frame on the stack.
 619                 // java.base has all permissions
 620                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
 621             }
 622             PrivilegedAction<ClassLoader> pa = module::getClassLoader;
 623             cl = AccessController.doPrivileged(pa);
 624         } else {
 625             cl = module.getClassLoader();
 626         }
 627 
 628         if (cl != null) {
 629             return cl.loadClass(module, name);
 630         } else {
 631             return BootLoader.loadClass(module, name);
 632         }
 633     }
 634 

































 635     /**
 636      * {@return the {@code Class} object associated with the
 637      * {@linkplain #isPrimitive() primitive type} of the given name}
 638      * If the argument is not the name of a primitive type, {@code
 639      * null} is returned.
 640      *
 641      * @param primitiveName the name of the primitive type to find
 642      *
 643      * @throws NullPointerException if the argument is {@code null}
 644      *
 645      * @jls 4.2 Primitive Types and Values
 646      * @jls 15.8.2 Class Literals
 647      * @since 22
 648      */
 649     public static Class<?> forPrimitiveName(String primitiveName) {
 650         return switch(primitiveName) {
 651         // Integral types
 652         case "int"     -> int.class;
 653         case "long"    -> long.class;
 654         case "short"   -> short.class;

1406      * arrays, the values of other modifiers are {@code false} other
1407      * than as specified above.
1408      *
1409      * <p> The modifier encodings are defined in section {@jvms 4.1}
1410      * of <cite>The Java Virtual Machine Specification</cite>.
1411      *
1412      * @return the {@code int} representing the modifiers for this class
1413      * @see     java.lang.reflect.Modifier
1414      * @see #accessFlags()
1415      * @see <a
1416      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1417      * programming language and JVM modeling in core reflection</a>
1418      * @since 1.1
1419      * @jls 8.1.1 Class Modifiers
1420      * @jls 9.1.1. Interface Modifiers
1421      * @jvms 4.1 The {@code ClassFile} Structure
1422      */
1423     @IntrinsicCandidate
1424     public native int getModifiers();
1425 
1426     /**
1427      * {@return an unmodifiable set of the {@linkplain AccessFlag access
1428      * flags} for this class, possibly empty}

1429      *
1430      * <p> If the underlying class is an array class:
1431      * <ul>
1432      * <li> its {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1433      *      access flags are the same as those of its component type
1434      * <li> its {@code ABSTRACT} and {@code FINAL} flags are present
1435      * <li> its {@code INTERFACE} flag is absent, even when the
1436      *      component type is an interface
1437      * </ul>
1438      * If this {@code Class} object represents a primitive type or
1439      * void, the flags are {@code PUBLIC}, {@code ABSTRACT}, and
1440      * {@code FINAL}.
1441      * For {@code Class} objects representing void, primitive types, and
1442      * arrays, access flags are absent other than as specified above.
1443      *
1444      * @see #getModifiers()
1445      * @jvms 4.1 The ClassFile Structure
1446      * @jvms 4.7.6 The InnerClasses Attribute
1447      * @since 20
1448      */
1449     public Set<AccessFlag> accessFlags() {
1450         // Location.CLASS allows SUPER and AccessFlag.MODULE which
1451         // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1452         // and STATIC, which are not allowed on Location.CLASS.
1453         // Use getClassAccessFlagsRaw to expose SUPER status.
1454         var location = (isMemberClass() || isLocalClass() ||
1455                         isAnonymousClass() || isArray()) ?
1456             AccessFlag.Location.INNER_CLASS :
1457             AccessFlag.Location.CLASS;
1458         return AccessFlag.maskToAccessFlags((location == AccessFlag.Location.CLASS) ?
1459                                             getClassAccessFlagsRaw() :
1460                                             getModifiers(),
1461                                             location);







1462     }
1463 
1464     /**
1465      * Gets the signers of this class.
1466      *
1467      * @return  the signers of this class, or null if there are no signers.  In
1468      *          particular, this method returns null if this {@code Class} object represents
1469      *          a primitive type or void.
1470      * @since   1.1
1471      */
1472     public native Object[] getSigners();
1473 
1474 
1475     /**
1476      * Set the signers of this class.
1477      */
1478     native void setSigners(Object[] signers);
1479 
1480 
1481     /**
1482      * If this {@code Class} object represents a local or anonymous
1483      * class within a method, returns a {@link
1484      * java.lang.reflect.Method Method} object representing the
1485      * immediately enclosing method of the underlying class. Returns
1486      * {@code null} otherwise.
1487      *
1488      * In particular, this method returns {@code null} if the underlying
1489      * class is a local or anonymous class immediately enclosed by a class or
1490      * interface declaration, instance initializer or static initializer.
1491      *
1492      * @return the immediately enclosing method of the underlying class, if
1493      *     that class is a local or anonymous class; otherwise {@code null}.
1494      *
1495      * @throws SecurityException
1496      *         If a security manager, <i>s</i>, is present and any of the
1497      *         following conditions is met:
1498      *
1499      *         <ul>
1500      *

4386      *
4387      * <p> If this {@code Class} object represents an interface, the return
4388      * value is an array containing objects representing the uses of interface
4389      * types to specify interfaces directly extended by the interface. The
4390      * order of the objects in the array corresponds to the order of the
4391      * interface types used in the 'extends' clause of the declaration of this
4392      * {@code Class} object.
4393      *
4394      * <p> If this {@code Class} object represents a class or interface whose
4395      * declaration does not explicitly indicate any annotated superinterfaces,
4396      * the return value is an array of length 0.
4397      *
4398      * <p> If this {@code Class} object represents either the {@code Object}
4399      * class, an array type, a primitive type, or void, the return value is an
4400      * array of length 0.
4401      *
4402      * @return an array representing the superinterfaces
4403      * @since 1.8
4404      */
4405     public AnnotatedType[] getAnnotatedInterfaces() {
4406          return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
4407     }
4408 
4409     private native Class<?> getNestHost0();
4410 
4411     /**
4412      * Returns the nest host of the <a href=#nest>nest</a> to which the class
4413      * or interface represented by this {@code Class} object belongs.
4414      * Every class and interface belongs to exactly one nest.
4415      *
4416      * If the nest host of this class or interface has previously
4417      * been determined, then this method returns the nest host.
4418      * If the nest host of this class or interface has
4419      * not previously been determined, then this method determines the nest
4420      * host using the algorithm of JVMS 5.4.4, and returns it.
4421      *
4422      * Often, a class or interface belongs to a nest consisting only of itself,
4423      * in which case this method returns {@code this} to indicate that the class
4424      * or interface is the nest host.
4425      *
4426      * <p>If this {@code Class} object represents a primitive type, an array type,

4787      * @since 17
4788      */
4789     public boolean isSealed() {
4790         if (isArray() || isPrimitive()) {
4791             return false;
4792         }
4793         return getPermittedSubclasses() != null;
4794     }
4795 
4796     private native Class<?>[] getPermittedSubclasses0();
4797 
4798     /*
4799      * Return the class's major and minor class file version packed into an int.
4800      * The high order 16 bits contain the class's minor version.  The low order
4801      * 16 bits contain the class's major version.
4802      *
4803      * If the class is an array type then the class file version of its element
4804      * type is returned.  If the class is a primitive type then the latest class
4805      * file major version is returned and zero is returned for the minor version.
4806      */
4807     private int getClassFileVersion() {

4808         Class<?> c = isArray() ? elementType() : this;
4809         return c.getClassFileVersion0();
4810     }
4811 
4812     private native int getClassFileVersion0();
4813 
4814     /*
4815      * Return the access flags as they were in the class's bytecode, including
4816      * the original setting of ACC_SUPER.
4817      *
4818      * If the class is an array type then the access flags of the element type is
4819      * returned.  If the class is a primitive then ACC_ABSTRACT | ACC_FINAL | ACC_PUBLIC.
4820      */
4821     private int getClassAccessFlagsRaw() {
4822         Class<?> c = isArray() ? elementType() : this;
4823         return c.getClassAccessFlagsRaw0();
4824     }
4825 
4826     private native int getClassAccessFlagsRaw0();
4827 }

   1 /*
   2  * Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.lang.constant.ClassDesc;
  30 import java.lang.constant.ConstantDescs;
  31 import java.lang.invoke.TypeDescriptor;
  32 import java.lang.invoke.MethodHandles;
  33 import java.lang.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;

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

 302      *
 303      * @since 1.8
 304      */
 305     public String toGenericString() {
 306         if (isPrimitive()) {
 307             return toString();
 308         } else {
 309             StringBuilder sb = new StringBuilder();
 310             Class<?> component = this;
 311             int arrayDepth = 0;
 312 
 313             if (isArray()) {
 314                 do {
 315                     arrayDepth++;
 316                     component = component.getComponentType();
 317                 } while (component.isArray());
 318                 sb.append(component.getName());
 319             } else {
 320                 // Class modifiers are a superset of interface modifiers
 321                 int modifiers = getModifiers() & Modifier.classModifiers();
 322                 // Modifier.toString() below mis-interprets SYNCHRONIZED, STRICT, and VOLATILE bits
 323                 modifiers &= ~(Modifier.SYNCHRONIZED | Modifier.STRICT | Modifier.VOLATILE);
 324                 if (modifiers != 0) {
 325                     sb.append(Modifier.toString(modifiers));
 326                     sb.append(' ');
 327                 }
 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             }

 621         SecurityManager sm = System.getSecurityManager();
 622         if (sm != null) {
 623             if (caller != null && caller.getModule() != module) {
 624                 // if caller is null, Class.forName is the last java frame on the stack.
 625                 // java.base has all permissions
 626                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
 627             }
 628             PrivilegedAction<ClassLoader> pa = module::getClassLoader;
 629             cl = AccessController.doPrivileged(pa);
 630         } else {
 631             cl = module.getClassLoader();
 632         }
 633 
 634         if (cl != null) {
 635             return cl.loadClass(module, name);
 636         } else {
 637             return BootLoader.loadClass(module, name);
 638         }
 639     }
 640 
 641     /**
 642      * {@return {@code true} if this {@code Class} object represents an identity
 643      * class or interface; otherwise {@code false}}
 644      *
 645      * If this {@code Class} object represents an array type, then this method
 646      * returns {@code true}.
 647      * If this {@code Class} object represents a primitive type, or {@code void},
 648      * then this method returns {@code false}.
 649      *
 650      * @since Valhalla
 651      */
 652     @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
 653     public native boolean isIdentity();
 654 
 655     /**
 656      * {@return {@code true} if this {@code Class} object represents a value
 657      * class; otherwise {@code false}}
 658      *
 659      * If this {@code Class} object represents an array type, an interface,
 660      * a primitive type, or {@code void}, then this method returns {@code false}.
 661      *
 662      * @since Valhalla
 663      */
 664     @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
 665     public boolean isValue() {
 666         if (!PreviewFeatures.isEnabled()) {
 667             return false;
 668         }
 669          if (isPrimitive() || isArray() || isInterface())
 670              return false;
 671         return ((getModifiers() & Modifier.IDENTITY) == 0);
 672     }
 673 
 674     /**
 675      * {@return the {@code Class} object associated with the
 676      * {@linkplain #isPrimitive() primitive type} of the given name}
 677      * If the argument is not the name of a primitive type, {@code
 678      * null} is returned.
 679      *
 680      * @param primitiveName the name of the primitive type to find
 681      *
 682      * @throws NullPointerException if the argument is {@code null}
 683      *
 684      * @jls 4.2 Primitive Types and Values
 685      * @jls 15.8.2 Class Literals
 686      * @since 22
 687      */
 688     public static Class<?> forPrimitiveName(String primitiveName) {
 689         return switch(primitiveName) {
 690         // Integral types
 691         case "int"     -> int.class;
 692         case "long"    -> long.class;
 693         case "short"   -> short.class;

1445      * arrays, the values of other modifiers are {@code false} other
1446      * than as specified above.
1447      *
1448      * <p> The modifier encodings are defined in section {@jvms 4.1}
1449      * of <cite>The Java Virtual Machine Specification</cite>.
1450      *
1451      * @return the {@code int} representing the modifiers for this class
1452      * @see     java.lang.reflect.Modifier
1453      * @see #accessFlags()
1454      * @see <a
1455      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1456      * programming language and JVM modeling in core reflection</a>
1457      * @since 1.1
1458      * @jls 8.1.1 Class Modifiers
1459      * @jls 9.1.1. Interface Modifiers
1460      * @jvms 4.1 The {@code ClassFile} Structure
1461      */
1462     @IntrinsicCandidate
1463     public native int getModifiers();
1464 
1465    /**
1466      * {@return an unmodifiable set of the {@linkplain AccessFlag access
1467      * flags} for this class, possibly empty}
1468      * The {@code AccessFlags} may depend on the class file format version of the class.
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         int accessFlags = (location == AccessFlag.Location.CLASS) ?
1499                 getClassAccessFlagsRaw() : getModifiers();
1500         var cffv = ClassFileFormatVersion.fromMajor(getClassFileVersion() & 0xffff);
1501         if (cffv.compareTo(ClassFileFormatVersion.latest()) >= 0) {
1502             // Ignore unspecified (0x0800) access flag for current version
1503             accessFlags &= ~0x0800;
1504         }
1505         if (!PreviewFeatures.isEnabled() && location == AccessFlag.Location.INNER_CLASS) {
1506             accessFlags &= ~Modifier.IDENTITY; // drop ACC_IDENTITY bit in inner class if not in preview
1507         }
1508         return AccessFlag.maskToAccessFlags(accessFlags, location, cffv);
1509     }
1510 
1511    /**
1512      * Gets the signers of this class.
1513      *
1514      * @return  the signers of this class, or null if there are no signers.  In
1515      *          particular, this method returns null if this {@code Class} object represents
1516      *          a primitive type or void.
1517      * @since   1.1
1518      */
1519     public native Object[] getSigners();
1520 

1521     /**
1522      * Set the signers of this class.
1523      */
1524     native void setSigners(Object[] signers);
1525 

1526     /**
1527      * If this {@code Class} object represents a local or anonymous
1528      * class within a method, returns a {@link
1529      * java.lang.reflect.Method Method} object representing the
1530      * immediately enclosing method of the underlying class. Returns
1531      * {@code null} otherwise.
1532      *
1533      * In particular, this method returns {@code null} if the underlying
1534      * class is a local or anonymous class immediately enclosed by a class or
1535      * interface declaration, instance initializer or static initializer.
1536      *
1537      * @return the immediately enclosing method of the underlying class, if
1538      *     that class is a local or anonymous class; otherwise {@code null}.
1539      *
1540      * @throws SecurityException
1541      *         If a security manager, <i>s</i>, is present and any of the
1542      *         following conditions is met:
1543      *
1544      *         <ul>
1545      *

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

4832      * @since 17
4833      */
4834     public boolean isSealed() {
4835         if (isArray() || isPrimitive()) {
4836             return false;
4837         }
4838         return getPermittedSubclasses() != null;
4839     }
4840 
4841     private native Class<?>[] getPermittedSubclasses0();
4842 
4843     /*
4844      * Return the class's major and minor class file version packed into an int.
4845      * The high order 16 bits contain the class's minor version.  The low order
4846      * 16 bits contain the class's major version.
4847      *
4848      * If the class is an array type then the class file version of its element
4849      * type is returned.  If the class is a primitive type then the latest class
4850      * file major version is returned and zero is returned for the minor version.
4851      */
4852     /* package-private */
4853     int getClassFileVersion() {
4854         Class<?> c = isArray() ? elementType() : this;
4855         return c.getClassFileVersion0();
4856     }
4857 
4858     private native int getClassFileVersion0();
4859 
4860     /*
4861      * Return the access flags as they were in the class's bytecode, including
4862      * the original setting of ACC_SUPER.
4863      *
4864      * If the class is an array type then the access flags of the element type is
4865      * returned.  If the class is a primitive then ACC_ABSTRACT | ACC_FINAL | ACC_PUBLIC.
4866      */
4867     private int getClassAccessFlagsRaw() {
4868         Class<?> c = isArray() ? elementType() : this;
4869         return c.getClassAccessFlagsRaw0();
4870     }
4871 
4872     private native int getClassAccessFlagsRaw0();
4873 }
< prev index next >