< prev index next >

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

Print this page

  61 import java.util.HashMap;
  62 import java.util.HashSet;
  63 import java.util.LinkedHashMap;
  64 import java.util.LinkedHashSet;
  65 import java.util.List;
  66 import java.util.Map;
  67 import java.util.Objects;
  68 import java.util.Optional;
  69 import java.util.Set;
  70 import java.util.stream.Collectors;
  71 
  72 import jdk.internal.loader.BootLoader;
  73 import jdk.internal.loader.BuiltinClassLoader;
  74 import jdk.internal.misc.Unsafe;
  75 import jdk.internal.module.Resources;
  76 import jdk.internal.reflect.CallerSensitive;
  77 import jdk.internal.reflect.CallerSensitiveAdapter;
  78 import jdk.internal.reflect.ConstantPool;
  79 import jdk.internal.reflect.Reflection;
  80 import jdk.internal.reflect.ReflectionFactory;

  81 import jdk.internal.vm.annotation.ForceInline;
  82 import jdk.internal.vm.annotation.IntrinsicCandidate;
  83 import sun.invoke.util.Wrapper;
  84 import sun.reflect.generics.factory.CoreReflectionFactory;
  85 import sun.reflect.generics.factory.GenericsFactory;
  86 import sun.reflect.generics.repository.ClassRepository;
  87 import sun.reflect.generics.repository.MethodRepository;
  88 import sun.reflect.generics.repository.ConstructorRepository;
  89 import sun.reflect.generics.scope.ClassScope;
  90 import sun.security.util.SecurityConstants;
  91 import sun.reflect.annotation.*;
  92 import sun.reflect.misc.ReflectUtil;
  93 
  94 /**
  95  * Instances of the class {@code Class} represent classes and
  96  * interfaces in a running Java application. An enum class and a record
  97  * class are kinds of class; an annotation interface is a kind of
  98  * interface. Every array also belongs to a class that is reflected as
  99  * a {@code Class} object that is shared by all arrays with the same
 100  * element type and number of dimensions.  The primitive Java types

 183  * A hidden class or interface is never an array class, but may be
 184  * the element type of an array. In all other respects, the fact that
 185  * a class or interface is hidden has no bearing on the characteristics
 186  * exposed by the methods of class {@code Class}.
 187  *
 188  * @param <T> the type of the class modeled by this {@code Class}
 189  * object.  For example, the type of {@code String.class} is {@code
 190  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 191  * unknown.
 192  *
 193  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 194  * @since   1.0
 195  * @jls 15.8.2 Class Literals
 196  */
 197 public final class Class<T> implements java.io.Serializable,
 198                               GenericDeclaration,
 199                               Type,
 200                               AnnotatedElement,
 201                               TypeDescriptor.OfField<Class<?>>,
 202                               Constable {
 203     private static final int ANNOTATION= 0x00002000;
 204     private static final int ENUM      = 0x00004000;
 205     private static final int SYNTHETIC = 0x00001000;
 206 
 207     private static native void registerNatives();
 208     static {
 209         registerNatives();
 210     }
 211 
 212     /*
 213      * Private constructor. Only the Java Virtual Machine creates Class objects.
 214      * This constructor is not used and prevents the default constructor being
 215      * generated.
 216      */
 217     private Class(ClassLoader loader, Class<?> arrayComponentType) {
 218         // Initialize final field for classLoader.  The initialization value of non-null
 219         // prevents future JIT optimizations from assuming this final field is null.
 220         classLoader = loader;
 221         componentType = arrayComponentType;
 222     }
 223 
 224     /**
 225      * Converts the object to a string. The string representation is the
 226      * string "class" or "interface", followed by a space, and then by the
 227      * name of the class in the format returned by {@code getName}.
 228      * If this {@code Class} object represents a primitive type,
 229      * this method returns the name of the primitive type.  If
 230      * this {@code Class} object represents void this method returns
 231      * "void". If this {@code Class} object represents an array type,
 232      * this method returns "class " followed by {@code getName}.
 233      *
 234      * @return a string representation of this {@code Class} object.
 235      */
 236     public String toString() {
 237         String kind = isInterface() ? "interface " : isPrimitive() ? "" : "class ";
 238         return kind.concat(getName());














 239     }
 240 
 241     /**
 242      * Returns a string describing this {@code Class}, including
 243      * information about modifiers and type parameters.
 244      *
 245      * The string is formatted as a list of type modifiers, if any,
 246      * followed by the kind of type (empty string for primitive types
 247      * and {@code class}, {@code enum}, {@code interface},
 248      * {@code @interface}, or {@code record} as appropriate), followed
 249      * by the type's name, followed by an angle-bracketed
 250      * comma-separated list of the type's type parameters, if any,
 251      * including informative bounds on the type parameters, if any.
 252      *
 253      * A space is used to separate modifiers from one another and to
 254      * separate any modifiers from the kind of type. The modifiers
 255      * occur in canonical order. If there are no type parameters, the
 256      * type parameter list is elided.
 257      *
 258      * For an array type, the string starts with the type name,

 271      *
 272      * @since 1.8
 273      */
 274     public String toGenericString() {
 275         if (isPrimitive()) {
 276             return toString();
 277         } else {
 278             StringBuilder sb = new StringBuilder();
 279             Class<?> component = this;
 280             int arrayDepth = 0;
 281 
 282             if (isArray()) {
 283                 do {
 284                     arrayDepth++;
 285                     component = component.getComponentType();
 286                 } while (component.isArray());
 287                 sb.append(component.getName());
 288             } else {
 289                 // Class modifiers are a superset of interface modifiers
 290                 int modifiers = getModifiers() & Modifier.classModifiers();


 291                 if (modifiers != 0) {
 292                     sb.append(Modifier.toString(modifiers));
 293                     sb.append(' ');
 294                 }
 295 
 296                 if (isAnnotation()) {
 297                     sb.append('@');
 298                 }



 299                 if (isInterface()) { // Note: all annotation interfaces are interfaces
 300                     sb.append("interface");
 301                 } else {
 302                     if (isEnum())
 303                         sb.append("enum");
 304                     else if (isRecord())
 305                         sb.append("record");
 306                     else
 307                         sb.append("class");
 308                 }
 309                 sb.append(' ');
 310                 sb.append(getName());
 311             }
 312 
 313             TypeVariable<?>[] typeparms = component.getTypeParameters();
 314             if (typeparms.length > 0) {
 315                 sb.append(Arrays.stream(typeparms)
 316                           .map(Class::typeVarBounds)
 317                           .collect(Collectors.joining(",", "<", ">")));
 318             }

 481             throws ClassNotFoundException
 482     {
 483         @SuppressWarnings("removal")
 484         SecurityManager sm = System.getSecurityManager();
 485         if (sm != null) {
 486             // Reflective call to get caller class is only needed if a security manager
 487             // is present.  Avoid the overhead of making this call otherwise.
 488             if (loader == null) {
 489                 ClassLoader ccl = ClassLoader.getClassLoader(caller);
 490                 if (ccl != null) {
 491                     sm.checkPermission(
 492                             SecurityConstants.GET_CLASSLOADER_PERMISSION);
 493                 }
 494             }
 495         }
 496         return forName0(name, initialize, loader, caller);
 497     }
 498 
 499     /** Called after security check for system loader access checks have been made. */
 500     private static native Class<?> forName0(String name, boolean initialize,
 501                                             ClassLoader loader,
 502                                             Class<?> caller)
 503         throws ClassNotFoundException;
 504 
 505 
 506     /**
 507      * Returns the {@code Class} with the given <a href="ClassLoader.html#binary-name">
 508      * binary name</a> in the given module.
 509      *
 510      * <p> This method attempts to locate and load the class or interface.
 511      * It does not link the class, and does not run the class initializer.
 512      * If the class is not found, this method returns {@code null}. </p>
 513      *
 514      * <p> If the class loader of the given module defines other modules and
 515      * the given name is a class defined in a different module, this method
 516      * returns {@code null} after the class is loaded. </p>
 517      *
 518      * <p> This method does not check whether the requested class is
 519      * accessible to its caller. </p>
 520      *
 521      * @apiNote
 522      * This method returns {@code null} on failure rather than

 571         SecurityManager sm = System.getSecurityManager();
 572         if (sm != null) {
 573             if (caller != null && caller.getModule() != module) {
 574                 // if caller is null, Class.forName is the last java frame on the stack.
 575                 // java.base has all permissions
 576                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
 577             }
 578             PrivilegedAction<ClassLoader> pa = module::getClassLoader;
 579             cl = AccessController.doPrivileged(pa);
 580         } else {
 581             cl = module.getClassLoader();
 582         }
 583 
 584         if (cl != null) {
 585             return cl.loadClass(module, name);
 586         } else {
 587             return BootLoader.loadClass(module, name);
 588         }
 589     }
 590 
































































































































 591     /**
 592      * Creates a new instance of the class represented by this {@code Class}
 593      * object.  The class is instantiated as if by a {@code new}
 594      * expression with an empty argument list.  The class is initialized if it
 595      * has not already been initialized.
 596      *
 597      * @deprecated This method propagates any exception thrown by the
 598      * nullary constructor, including a checked exception.  Use of
 599      * this method effectively bypasses the compile-time exception
 600      * checking that would otherwise be performed by the compiler.
 601      * The {@link
 602      * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
 603      * Constructor.newInstance} method avoids this problem by wrapping
 604      * any exception thrown by the constructor in a (checked) {@link
 605      * java.lang.reflect.InvocationTargetException}.
 606      *
 607      * <p>The call
 608      *
 609      * <pre>{@code
 610      * clazz.newInstance()

 715      * class or any superclass of the specified {@code Object} argument
 716      * implements this interface; it returns {@code false} otherwise. If
 717      * this {@code Class} object represents a primitive type, this method
 718      * returns {@code false}.
 719      *
 720      * @param   obj the object to check
 721      * @return  true if {@code obj} is an instance of this class
 722      *
 723      * @since 1.1
 724      */
 725     @IntrinsicCandidate
 726     public native boolean isInstance(Object obj);
 727 
 728 
 729     /**
 730      * Determines if the class or interface represented by this
 731      * {@code Class} object is either the same as, or is a superclass or
 732      * superinterface of, the class or interface represented by the specified
 733      * {@code Class} parameter. It returns {@code true} if so;
 734      * otherwise it returns {@code false}. If this {@code Class}




 735      * object represents a primitive type, this method returns
 736      * {@code true} if the specified {@code Class} parameter is
 737      * exactly this {@code Class} object; otherwise it returns
 738      * {@code false}.
 739      *
 740      * <p> Specifically, this method tests whether the type represented by the
 741      * specified {@code Class} parameter can be converted to the type
 742      * represented by this {@code Class} object via an identity conversion
 743      * or via a widening reference conversion. See <cite>The Java Language
 744      * Specification</cite>, sections {@jls 5.1.1} and {@jls 5.1.4},
 745      * for details.
 746      *
 747      * @param     cls the {@code Class} object to be checked
 748      * @return    the {@code boolean} value indicating whether objects of the
 749      *            type {@code cls} can be assigned to objects of this class
 750      * @throws    NullPointerException if the specified Class parameter is
 751      *            null.
 752      * @since     1.1
 753      */
 754     @IntrinsicCandidate
 755     public native boolean isAssignableFrom(Class<?> cls);
 756 
 757 
 758     /**
 759      * Determines if this {@code Class} object represents an
 760      * interface type.
 761      *
 762      * @return  {@code true} if this {@code Class} object represents an interface;
 763      *          {@code false} otherwise.
 764      */
 765     @IntrinsicCandidate

1278      * If this class does not represent an array class, then this method returns
1279      * {@code null}.
1280      */
1281     private Class<?> elementType() {
1282         if (!isArray()) return null;
1283 
1284         Class<?> c = this;
1285         while (c.isArray()) {
1286             c = c.getComponentType();
1287         }
1288         return c;
1289     }
1290 
1291     /**
1292      * Returns the Java language modifiers for this class or interface, encoded
1293      * in an integer. The modifiers consist of the Java Virtual Machine's
1294      * constants for {@code public}, {@code protected},
1295      * {@code private}, {@code final}, {@code static},
1296      * {@code abstract} and {@code interface}; they should be decoded
1297      * using the methods of class {@code Modifier}.


1298      *
1299      * <p> If the underlying class is an array class, then its
1300      * {@code public}, {@code private} and {@code protected}
1301      * modifiers are the same as those of its component type.  If this
1302      * {@code Class} object represents a primitive type or void, its
1303      * {@code public} modifier is always {@code true}, and its
1304      * {@code protected} and {@code private} modifiers are always
1305      * {@code false}. If this {@code Class} object represents an array class, a
1306      * primitive type or void, then its {@code final} modifier is always
1307      * {@code true} and its interface modifier is always
1308      * {@code false}. The values of its other modifiers are not determined
1309      * by this specification.
1310      *
1311      * <p> The modifier encodings are defined in section {@jvms 4.1}
1312      * of <cite>The Java Virtual Machine Specification</cite>.
1313      *
1314      * @return the {@code int} representing the modifiers for this class
1315      * @see     java.lang.reflect.Modifier
1316      * @see #accessFlags()
1317      * @see <a
1318      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1319      * programming language and JVM modeling in core reflection</a>
1320      * @since 1.1
1321      * @jls 8.1.1 Class Modifiers
1322      * @jls 9.1.1. Interface Modifiers
1323      */
1324     @IntrinsicCandidate
1325     public native int getModifiers();
1326 
1327     /**
1328      * {@return an unmodifiable set of the {@linkplain AccessFlag access
1329      * flags} for this class, possibly empty}
1330      *
1331      * <p> If the underlying class is an array class, then its
1332      * {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1333      * access flags are the same as those of its component type.  If this
1334      * {@code Class} object represents a primitive type or void, the
1335      * {@code PUBLIC} access flag is present, and the
1336      * {@code PROTECTED} and {@code PRIVATE} access flags are always
1337      * absent. If this {@code Class} object represents an array class, a
1338      * primitive type or void, then the {@code FINAL} access flag is always
1339      * present and the interface access flag is always
1340      * absent. The values of its other access flags are not determined
1341      * by this specification.
1342      *
1343      * @see #getModifiers()
1344      * @jvms 4.1 The ClassFile Structure
1345      * @jvms 4.7.6 The InnerClasses Attribute
1346      * @since 20
1347      */
1348     public Set<AccessFlag> accessFlags() {
1349         // Location.CLASS allows SUPER and AccessFlag.MODULE which
1350         // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1351         // and STATIC, which are not allowed on Location.CLASS.
1352         // Use getClassAccessFlagsRaw to expose SUPER status.
1353         var location = (isMemberClass() || isLocalClass() ||
1354                         isAnonymousClass() || isArray()) ?
1355             AccessFlag.Location.INNER_CLASS :
1356             AccessFlag.Location.CLASS;
1357         return AccessFlag.maskToAccessFlags((location == AccessFlag.Location.CLASS) ?
1358                                             getClassAccessFlagsRaw() :
1359                                             getModifiers(),
1360                                             location);
1361     }
1362 
1363     /**
1364      * Gets the signers of this class.
1365      *
1366      * @return  the signers of this class, or null if there are no signers.  In
1367      *          particular, this method returns null if this {@code Class} object represents
1368      *          a primitive type or void.
1369      * @since   1.1
1370      */
1371     public native Object[] getSigners();
1372 
1373 
1374     /**
1375      * Set the signers of this class.
1376      */
1377     native void setSigners(Object[] signers);
1378 
1379 
1380     /**
1381      * If this {@code Class} object represents a local or anonymous
1382      * class within a method, returns a {@link
1383      * java.lang.reflect.Method Method} object representing the
1384      * immediately enclosing method of the underlying class. Returns
1385      * {@code null} otherwise.
1386      *
1387      * In particular, this method returns {@code null} if the underlying
1388      * class is a local or anonymous class immediately enclosed by a class or
1389      * interface declaration, instance initializer or static initializer.
1390      *
1391      * @return the immediately enclosing method of the underlying class, if
1392      *     that class is a local or anonymous class; otherwise {@code null}.
1393      *
1394      * @throws SecurityException
1395      *         If a security manager, <i>s</i>, is present and any of the
1396      *         following conditions is met:
1397      *
1398      *         <ul>
1399      *

1741             simpleName = simpleName.substring(simpleName.lastIndexOf('.') + 1); // strip the package name
1742         }
1743         return simpleName;
1744     }
1745 
1746     /**
1747      * Return an informative string for the name of this class or interface.
1748      *
1749      * @return an informative string for the name of this class or interface
1750      * @since 1.8
1751      */
1752     public String getTypeName() {
1753         if (isArray()) {
1754             try {
1755                 Class<?> cl = this;
1756                 int dimensions = 0;
1757                 do {
1758                     dimensions++;
1759                     cl = cl.getComponentType();
1760                 } while (cl.isArray());
1761                 return cl.getName().concat("[]".repeat(dimensions));
1762             } catch (Throwable e) { /*FALLTHRU*/ }
1763         }
1764         return getName();





1765     }
1766 
1767     /**
1768      * Returns the canonical name of the underlying class as
1769      * defined by <cite>The Java Language Specification</cite>.
1770      * Returns {@code null} if the underlying class does not have a canonical
1771      * name. Classes without canonical names include:
1772      * <ul>
1773      * <li>a {@linkplain #isLocalClass() local class}
1774      * <li>a {@linkplain #isAnonymousClass() anonymous class}
1775      * <li>a {@linkplain #isHidden() hidden class}
1776      * <li>an array whose component type does not have a canonical name</li>
1777      * </ul>
1778      *
1779      * The canonical name for a primitive class is the keyword for the
1780      * corresponding primitive type ({@code byte}, {@code short},
1781      * {@code char}, {@code int}, and so on).
1782      *
1783      * <p>An array type has a canonical name if and only if its
1784      * component type has a canonical name. When an array type has a

3962         }
3963         return directory;
3964     }
3965     private transient volatile Map<String, T> enumConstantDirectory;
3966 
3967     /**
3968      * Casts an object to the class or interface represented
3969      * by this {@code Class} object.
3970      *
3971      * @param obj the object to be cast
3972      * @return the object after casting, or null if obj is null
3973      *
3974      * @throws ClassCastException if the object is not
3975      * null and is not assignable to the type T.
3976      *
3977      * @since 1.5
3978      */
3979     @SuppressWarnings("unchecked")
3980     @IntrinsicCandidate
3981     public T cast(Object obj) {



3982         if (obj != null && !isInstance(obj))
3983             throw new ClassCastException(cannotCastMsg(obj));
3984         return (T) obj;
3985     }
3986 
3987     private String cannotCastMsg(Object obj) {
3988         return "Cannot cast " + obj.getClass().getName() + " to " + getName();
3989     }
3990 
3991     /**
3992      * Casts this {@code Class} object to represent a subclass of the class
3993      * represented by the specified class object.  Checks that the cast
3994      * is valid, and throws a {@code ClassCastException} if it is not.  If
3995      * this method succeeds, it always returns a reference to this {@code Class} object.
3996      *
3997      * <p>This method is useful when a client needs to "narrow" the type of
3998      * a {@code Class} object to pass it to an API that restricts the
3999      * {@code Class} objects that it is willing to accept.  A cast would
4000      * generate a compile-time warning, as the correctness of the cast
4001      * could not be checked at runtime (because generic types are implemented

4257      *
4258      * <p> If this {@code Class} object represents an interface, the return
4259      * value is an array containing objects representing the uses of interface
4260      * types to specify interfaces directly extended by the interface. The
4261      * order of the objects in the array corresponds to the order of the
4262      * interface types used in the 'extends' clause of the declaration of this
4263      * {@code Class} object.
4264      *
4265      * <p> If this {@code Class} object represents a class or interface whose
4266      * declaration does not explicitly indicate any annotated superinterfaces,
4267      * the return value is an array of length 0.
4268      *
4269      * <p> If this {@code Class} object represents either the {@code Object}
4270      * class, an array type, a primitive type, or void, the return value is an
4271      * array of length 0.
4272      *
4273      * @return an array representing the superinterfaces
4274      * @since 1.8
4275      */
4276     public AnnotatedType[] getAnnotatedInterfaces() {
4277          return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
4278     }
4279 
4280     private native Class<?> getNestHost0();
4281 
4282     /**
4283      * Returns the nest host of the <a href=#nest>nest</a> to which the class
4284      * or interface represented by this {@code Class} object belongs.
4285      * Every class and interface belongs to exactly one nest.
4286      *
4287      * If the nest host of this class or interface has previously
4288      * been determined, then this method returns the nest host.
4289      * If the nest host of this class or interface has
4290      * not previously been determined, then this method determines the nest
4291      * host using the algorithm of JVMS 5.4.4, and returns it.
4292      *
4293      * Often, a class or interface belongs to a nest consisting only of itself,
4294      * in which case this method returns {@code this} to indicate that the class
4295      * or interface is the nest host.
4296      *
4297      * <p>If this {@code Class} object represents a primitive type, an array type,

4472      * {@code void}, then the result is a field descriptor string which
4473      * is a one-letter code corresponding to a primitive type or {@code void}
4474      * ({@code "B", "C", "D", "F", "I", "J", "S", "Z", "V"}) (JVMS {@jvms 4.3.2}).
4475      *
4476      * @apiNote
4477      * This is not a strict inverse of {@link #forName};
4478      * distinct classes which share a common name but have different class loaders
4479      * will have identical descriptor strings.
4480      *
4481      * @return the descriptor string for this {@code Class} object
4482      * @jvms 4.3.2 Field Descriptors
4483      * @since 12
4484      */
4485     @Override
4486     public String descriptorString() {
4487         if (isPrimitive())
4488             return Wrapper.forPrimitiveType(this).basicTypeString();
4489 
4490         if (isArray()) {
4491             return "[" + componentType.descriptorString();
4492         } else if (isHidden()) {


4493             String name = getName();
4494             int index = name.indexOf('/');
4495             return new StringBuilder(name.length() + 2)
4496                     .append('L')
4497                     .append(name.substring(0, index).replace('.', '/'))
4498                     .append('.')
4499                     .append(name, index + 1, name.length())
4500                     .append(';')
4501                     .toString();
4502         } else {
4503             String name = getName().replace('.', '/');
4504             return new StringBuilder(name.length() + 2)
4505                     .append('L')
4506                     .append(name)
4507                     .append(';')
4508                     .toString();
4509         }
4510     }
4511 
4512     /**
4513      * Returns the component type of this {@code Class}, if it describes
4514      * an array type, or {@code null} otherwise.
4515      *
4516      * @implSpec
4517      * Equivalent to {@link Class#getComponentType()}.
4518      *
4519      * @return a {@code Class} describing the component type, or {@code null}
4520      * if this {@code Class} does not describe an array type
4521      * @since 12
4522      */
4523     @Override
4524     public Class<?> componentType() {
4525         return isArray() ? componentType : null;

  61 import java.util.HashMap;
  62 import java.util.HashSet;
  63 import java.util.LinkedHashMap;
  64 import java.util.LinkedHashSet;
  65 import java.util.List;
  66 import java.util.Map;
  67 import java.util.Objects;
  68 import java.util.Optional;
  69 import java.util.Set;
  70 import java.util.stream.Collectors;
  71 
  72 import jdk.internal.loader.BootLoader;
  73 import jdk.internal.loader.BuiltinClassLoader;
  74 import jdk.internal.misc.Unsafe;
  75 import jdk.internal.module.Resources;
  76 import jdk.internal.reflect.CallerSensitive;
  77 import jdk.internal.reflect.CallerSensitiveAdapter;
  78 import jdk.internal.reflect.ConstantPool;
  79 import jdk.internal.reflect.Reflection;
  80 import jdk.internal.reflect.ReflectionFactory;
  81 import jdk.internal.value.PrimitiveClass;
  82 import jdk.internal.vm.annotation.ForceInline;
  83 import jdk.internal.vm.annotation.IntrinsicCandidate;
  84 import sun.invoke.util.Wrapper;
  85 import sun.reflect.generics.factory.CoreReflectionFactory;
  86 import sun.reflect.generics.factory.GenericsFactory;
  87 import sun.reflect.generics.repository.ClassRepository;
  88 import sun.reflect.generics.repository.MethodRepository;
  89 import sun.reflect.generics.repository.ConstructorRepository;
  90 import sun.reflect.generics.scope.ClassScope;
  91 import sun.security.util.SecurityConstants;
  92 import sun.reflect.annotation.*;
  93 import sun.reflect.misc.ReflectUtil;
  94 
  95 /**
  96  * Instances of the class {@code Class} represent classes and
  97  * interfaces in a running Java application. An enum class and a record
  98  * class are kinds of class; an annotation interface is a kind of
  99  * interface. Every array also belongs to a class that is reflected as
 100  * a {@code Class} object that is shared by all arrays with the same
 101  * element type and number of dimensions.  The primitive Java types

 184  * A hidden class or interface is never an array class, but may be
 185  * the element type of an array. In all other respects, the fact that
 186  * a class or interface is hidden has no bearing on the characteristics
 187  * exposed by the methods of class {@code Class}.
 188  *
 189  * @param <T> the type of the class modeled by this {@code Class}
 190  * object.  For example, the type of {@code String.class} is {@code
 191  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 192  * unknown.
 193  *
 194  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 195  * @since   1.0
 196  * @jls 15.8.2 Class Literals
 197  */
 198 public final class Class<T> implements java.io.Serializable,
 199                               GenericDeclaration,
 200                               Type,
 201                               AnnotatedElement,
 202                               TypeDescriptor.OfField<Class<?>>,
 203                               Constable {
 204     private static final int ANNOTATION = 0x00002000;
 205     private static final int ENUM       = 0x00004000;
 206     private static final int SYNTHETIC  = 0x00001000;
 207 
 208     private static native void registerNatives();
 209     static {
 210         registerNatives();
 211     }
 212 
 213     /*
 214      * Private constructor. Only the Java Virtual Machine creates Class objects.
 215      * This constructor is not used and prevents the default constructor being
 216      * generated.
 217      */
 218     private Class(ClassLoader loader, Class<?> arrayComponentType) {
 219         // Initialize final field for classLoader.  The initialization value of non-null
 220         // prevents future JIT optimizations from assuming this final field is null.
 221         classLoader = loader;
 222         componentType = arrayComponentType;
 223     }
 224 
 225     /**
 226      * Converts the object to a string. The string representation is the
 227      * string "class" or "interface", followed by a space, and then by the
 228      * name of the class in the format returned by {@code getName}.
 229      * If this {@code Class} object represents a primitive type,
 230      * this method returns the name of the primitive type.  If
 231      * this {@code Class} object represents void this method returns
 232      * "void". If this {@code Class} object represents an array type,
 233      * this method returns "class " followed by {@code getName}.
 234      *
 235      * @return a string representation of this {@code Class} object.
 236      */
 237     public String toString() {
 238         String s = getName();
 239         if (isPrimitive()) {
 240             return s;
 241         }
 242         // Avoid invokedynamic based String concat, might be not available
 243         // Prepend type of class
 244         s = (isInterface() ? "interface " : "class ").concat(s);
 245         if (isValue()) {
 246             // prepend value class type
 247             s = (isPrimitiveClass() ? "primitive " : "value ").concat(s);
 248             if (isPrimitiveClass() && isPrimaryType()) {
 249                 // Append .ref
 250                 s = s.concat(".ref");
 251             }
 252         }
 253         return s;
 254     }
 255 
 256     /**
 257      * Returns a string describing this {@code Class}, including
 258      * information about modifiers and type parameters.
 259      *
 260      * The string is formatted as a list of type modifiers, if any,
 261      * followed by the kind of type (empty string for primitive types
 262      * and {@code class}, {@code enum}, {@code interface},
 263      * {@code @interface}, or {@code record} as appropriate), followed
 264      * by the type's name, followed by an angle-bracketed
 265      * comma-separated list of the type's type parameters, if any,
 266      * including informative bounds on the type parameters, if any.
 267      *
 268      * A space is used to separate modifiers from one another and to
 269      * separate any modifiers from the kind of type. The modifiers
 270      * occur in canonical order. If there are no type parameters, the
 271      * type parameter list is elided.
 272      *
 273      * For an array type, the string starts with the type name,

 286      *
 287      * @since 1.8
 288      */
 289     public String toGenericString() {
 290         if (isPrimitive()) {
 291             return toString();
 292         } else {
 293             StringBuilder sb = new StringBuilder();
 294             Class<?> component = this;
 295             int arrayDepth = 0;
 296 
 297             if (isArray()) {
 298                 do {
 299                     arrayDepth++;
 300                     component = component.getComponentType();
 301                 } while (component.isArray());
 302                 sb.append(component.getName());
 303             } else {
 304                 // Class modifiers are a superset of interface modifiers
 305                 int modifiers = getModifiers() & Modifier.classModifiers();
 306                 // Modifier.toString() below mis-interprets SYNCHRONIZED, STRICT, and VOLATILE bits
 307                 modifiers &= ~(Modifier.SYNCHRONIZED | Modifier.STRICT | Modifier.VOLATILE);
 308                 if (modifiers != 0) {
 309                     sb.append(Modifier.toString(modifiers));
 310                     sb.append(' ');
 311                 }
 312 
 313                 if (isAnnotation()) {
 314                     sb.append('@');
 315                 }
 316                 if (isValue()) {
 317                     sb.append(isPrimitiveClass() ? "primitive " : "value ");
 318                 }
 319                 if (isInterface()) { // Note: all annotation interfaces are interfaces
 320                     sb.append("interface");
 321                 } else {
 322                     if (isEnum())
 323                         sb.append("enum");
 324                     else if (isRecord())
 325                         sb.append("record");
 326                     else
 327                         sb.append("class");
 328                 }
 329                 sb.append(' ');
 330                 sb.append(getName());
 331             }
 332 
 333             TypeVariable<?>[] typeparms = component.getTypeParameters();
 334             if (typeparms.length > 0) {
 335                 sb.append(Arrays.stream(typeparms)
 336                           .map(Class::typeVarBounds)
 337                           .collect(Collectors.joining(",", "<", ">")));
 338             }

 501             throws ClassNotFoundException
 502     {
 503         @SuppressWarnings("removal")
 504         SecurityManager sm = System.getSecurityManager();
 505         if (sm != null) {
 506             // Reflective call to get caller class is only needed if a security manager
 507             // is present.  Avoid the overhead of making this call otherwise.
 508             if (loader == null) {
 509                 ClassLoader ccl = ClassLoader.getClassLoader(caller);
 510                 if (ccl != null) {
 511                     sm.checkPermission(
 512                             SecurityConstants.GET_CLASSLOADER_PERMISSION);
 513                 }
 514             }
 515         }
 516         return forName0(name, initialize, loader, caller);
 517     }
 518 
 519     /** Called after security check for system loader access checks have been made. */
 520     private static native Class<?> forName0(String name, boolean initialize,
 521                                     ClassLoader loader,
 522                                     Class<?> caller)
 523         throws ClassNotFoundException;
 524 
 525 
 526     /**
 527      * Returns the {@code Class} with the given <a href="ClassLoader.html#binary-name">
 528      * binary name</a> in the given module.
 529      *
 530      * <p> This method attempts to locate and load the class or interface.
 531      * It does not link the class, and does not run the class initializer.
 532      * If the class is not found, this method returns {@code null}. </p>
 533      *
 534      * <p> If the class loader of the given module defines other modules and
 535      * the given name is a class defined in a different module, this method
 536      * returns {@code null} after the class is loaded. </p>
 537      *
 538      * <p> This method does not check whether the requested class is
 539      * accessible to its caller. </p>
 540      *
 541      * @apiNote
 542      * This method returns {@code null} on failure rather than

 591         SecurityManager sm = System.getSecurityManager();
 592         if (sm != null) {
 593             if (caller != null && caller.getModule() != module) {
 594                 // if caller is null, Class.forName is the last java frame on the stack.
 595                 // java.base has all permissions
 596                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
 597             }
 598             PrivilegedAction<ClassLoader> pa = module::getClassLoader;
 599             cl = AccessController.doPrivileged(pa);
 600         } else {
 601             cl = module.getClassLoader();
 602         }
 603 
 604         if (cl != null) {
 605             return cl.loadClass(module, name);
 606         } else {
 607             return BootLoader.loadClass(module, name);
 608         }
 609     }
 610 
 611     // set by VM if this class is an exotic type such as primitive class
 612     // otherwise, these two fields are null
 613     private transient Class<T> primaryType;
 614     private transient Class<T> secondaryType;
 615 
 616     /**
 617      * Returns {@code true} if this class is a primitive class.
 618      * <p>
 619      * Each primitive class has a {@linkplain #isPrimaryType() primary type}
 620      * representing the <em>primitive reference type</em> and a
 621      * {@linkplain #isPrimitiveValueType() secondary type} representing
 622      * the <em>primitive value type</em>.  The primitive reference type
 623      * and primitive value type can be obtained by calling the
 624      * {@link #asPrimaryType()} and {@link #asValueType} method
 625      * of a primitive class respectively.
 626      * <p>
 627      * A primitive class is a {@linkplain #isValue() value class}.
 628      *
 629      * @return {@code true} if this class is a primitive class, otherwise {@code false}
 630      * @see #isValue()
 631      * @see #asPrimaryType()
 632      * @see #asValueType()
 633      * @since Valhalla
 634      */
 635     /* package */ boolean isPrimitiveClass() {
 636         return (this.getModifiers() & PrimitiveClass.PRIMITIVE_CLASS) != 0;
 637     }
 638 
 639     /**
 640      * {@return {@code true} if this class is an identity class, otherwise {@code false}}
 641      *
 642      * @since Valhalla
 643      */
 644     public boolean isIdentity() {
 645         return (this.getModifiers() & Modifier.IDENTITY) != 0;
 646     }
 647 
 648     /**
 649      * {@return {@code true} if this class is a value class, otherwise {@code false}}
 650      *
 651      * @since Valhalla
 652      */
 653     public boolean isValue() {
 654         return (this.getModifiers() & Modifier.VALUE) != 0;
 655     }
 656 
 657     /**
 658      * Returns a {@code Class} object representing the primary type
 659      * of this class or interface.
 660      * <p>
 661      * If this {@code Class} object represents a primitive type or an array type,
 662      * then this method returns this class.
 663      * <p>
 664      * If this {@code Class} object represents a {@linkplain #isPrimitiveClass()
 665      * primitive class}, then this method returns the <em>primitive reference type</em>
 666      * type of this primitive class.
 667      * <p>
 668      * Otherwise, this {@code Class} object represents a non-primitive class or interface
 669      * and this method returns this class.
 670      *
 671      * @return the {@code Class} representing the primary type of
 672      *         this class or interface
 673      * @since Valhalla
 674      */
 675     @IntrinsicCandidate
 676     /* package */ Class<?> asPrimaryType() {
 677         return isPrimitiveClass() ? primaryType : this;
 678     }
 679 
 680     /**
 681      * Returns a {@code Class} object representing the <em>primitive value type</em>
 682      * of this class if this class is a {@linkplain #isPrimitiveClass() primitive class}.
 683      *
 684      * @apiNote Alternatively, this method returns null if this class is not
 685      *          a primitive class rather than throwing UOE.
 686      *
 687      * @return the {@code Class} representing the {@linkplain #isPrimitiveValueType()
 688      * primitive value type} of this class if this class is a primitive class
 689      * @throws UnsupportedOperationException if this class or interface
 690      *         is not a primitive class
 691      * @since Valhalla
 692      */
 693     @IntrinsicCandidate
 694     /* package */ Class<?> asValueType() {
 695         if (isPrimitiveClass())
 696             return secondaryType;
 697 
 698         throw new UnsupportedOperationException(this.getName().concat(" is not a primitive class"));
 699     }
 700 
 701     /**
 702      * Returns {@code true} if this {@code Class} object represents the primary type
 703      * of this class or interface.
 704      * <p>
 705      * If this {@code Class} object represents a primitive type or an array type,
 706      * then this method returns {@code true}.
 707      * <p>
 708      * If this {@code Class} object represents a {@linkplain #isPrimitiveClass()
 709      * primitive}, then this method returns {@code true} if this {@code Class}
 710      * object represents a primitive reference type, or returns {@code false}
 711      * if this {@code Class} object represents a primitive value type.
 712      * <p>
 713      * If this {@code Class} object represents a non-primitive class or interface,
 714      * then this method returns {@code true}.
 715      *
 716      * @return {@code true} if this {@code Class} object represents
 717      * the primary type of this class or interface
 718      * @since Valhalla
 719      */
 720     /* package */ boolean isPrimaryType() {
 721         if (isPrimitiveClass()) {
 722             return this == primaryType;
 723         }
 724         return true;
 725     }
 726 
 727     /**
 728      * Returns {@code true} if this {@code Class} object represents
 729      * a {@linkplain #isPrimitiveClass() primitive} value type.
 730      *
 731      * @return {@code true} if this {@code Class} object represents
 732      * the value type of a primitive class
 733      * @since Valhalla
 734      */
 735     /* package */ boolean isPrimitiveValueType() {
 736         return isPrimitiveClass() && this == secondaryType;
 737     }
 738 
 739     /**
 740      * Creates a new instance of the class represented by this {@code Class}
 741      * object.  The class is instantiated as if by a {@code new}
 742      * expression with an empty argument list.  The class is initialized if it
 743      * has not already been initialized.
 744      *
 745      * @deprecated This method propagates any exception thrown by the
 746      * nullary constructor, including a checked exception.  Use of
 747      * this method effectively bypasses the compile-time exception
 748      * checking that would otherwise be performed by the compiler.
 749      * The {@link
 750      * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
 751      * Constructor.newInstance} method avoids this problem by wrapping
 752      * any exception thrown by the constructor in a (checked) {@link
 753      * java.lang.reflect.InvocationTargetException}.
 754      *
 755      * <p>The call
 756      *
 757      * <pre>{@code
 758      * clazz.newInstance()

 863      * class or any superclass of the specified {@code Object} argument
 864      * implements this interface; it returns {@code false} otherwise. If
 865      * this {@code Class} object represents a primitive type, this method
 866      * returns {@code false}.
 867      *
 868      * @param   obj the object to check
 869      * @return  true if {@code obj} is an instance of this class
 870      *
 871      * @since 1.1
 872      */
 873     @IntrinsicCandidate
 874     public native boolean isInstance(Object obj);
 875 
 876 
 877     /**
 878      * Determines if the class or interface represented by this
 879      * {@code Class} object is either the same as, or is a superclass or
 880      * superinterface of, the class or interface represented by the specified
 881      * {@code Class} parameter. It returns {@code true} if so;
 882      * otherwise it returns {@code false}. If this {@code Class}
 883      * object represents the {@linkplain #isPrimaryType() reference type}
 884      * of a {@linkplain #isPrimitiveClass() primitive class}, this method
 885      * return {@code true} if the specified {@code Class} parameter represents
 886      * the same primitive class. If this {@code Class}
 887      * object represents a primitive type, this method returns
 888      * {@code true} if the specified {@code Class} parameter is
 889      * exactly this {@code Class} object; otherwise it returns
 890      * {@code false}.
 891      *
 892      * <p> Specifically, this method tests whether the type represented by the
 893      * specified {@code Class} parameter can be converted to the type
 894      * represented by this {@code Class} object via an identity conversion
 895      * or via a widening reference conversion or via a primitive widening
 896      * conversion. See <cite>The Java Language Specification</cite>,
 897      * sections {@jls 5.1.1} and {@jls 5.1.4}, for details.
 898      *
 899      * @param     cls the {@code Class} object to be checked
 900      * @return    the {@code boolean} value indicating whether objects of the
 901      *            type {@code cls} can be assigned to objects of this class
 902      * @throws    NullPointerException if the specified Class parameter is
 903      *            null.
 904      * @since     1.1
 905      */
 906     @IntrinsicCandidate
 907     public native boolean isAssignableFrom(Class<?> cls);
 908 
 909 
 910     /**
 911      * Determines if this {@code Class} object represents an
 912      * interface type.
 913      *
 914      * @return  {@code true} if this {@code Class} object represents an interface;
 915      *          {@code false} otherwise.
 916      */
 917     @IntrinsicCandidate

1430      * If this class does not represent an array class, then this method returns
1431      * {@code null}.
1432      */
1433     private Class<?> elementType() {
1434         if (!isArray()) return null;
1435 
1436         Class<?> c = this;
1437         while (c.isArray()) {
1438             c = c.getComponentType();
1439         }
1440         return c;
1441     }
1442 
1443     /**
1444      * Returns the Java language modifiers for this class or interface, encoded
1445      * in an integer. The modifiers consist of the Java Virtual Machine's
1446      * constants for {@code public}, {@code protected},
1447      * {@code private}, {@code final}, {@code static},
1448      * {@code abstract} and {@code interface}; they should be decoded
1449      * using the methods of class {@code Modifier}.
1450      * The modifiers also include the Java Virtual Machine's constants for
1451      * {@code identity class} and {@code value class}.
1452      *
1453      * <p> If the underlying class is an array class, then its
1454      * {@code public}, {@code private} and {@code protected}
1455      * modifiers are the same as those of its component type.  If this
1456      * {@code Class} object represents a primitive type or void, its
1457      * {@code public} modifier is always {@code true}, and its
1458      * {@code protected} and {@code private} modifiers are always
1459      * {@code false}. If this {@code Class} object represents an array class, a
1460      * primitive type or void, then its {@code final} modifier is always
1461      * {@code true} and its interface modifier is always
1462      * {@code false}. The values of its other modifiers are not determined
1463      * by this specification.
1464      *
1465      * <p> The modifier encodings are defined in section {@jvms 4.1}
1466      * of <cite>The Java Virtual Machine Specification</cite>.
1467      *
1468      * @return the {@code int} representing the modifiers for this class
1469      * @see     java.lang.reflect.Modifier
1470      * @see #accessFlags()
1471      * @see <a
1472      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
1473      * programming language and JVM modeling in core reflection</a>
1474      * @since 1.1
1475      * @jls 8.1.1 Class Modifiers
1476      * @jls 9.1.1. Interface Modifiers
1477      */
1478     @IntrinsicCandidate
1479     public native int getModifiers();
1480 
1481    /**
1482      * {@return an unmodifiable set of the {@linkplain AccessFlag access
1483      * flags} for this class, possibly empty}
1484      *
1485      * <p> If the underlying class is an array class, then its
1486      * {@code PUBLIC}, {@code PRIVATE} and {@code PROTECTED}
1487      * access flags are the same as those of its component type.  If this
1488      * {@code Class} object represents a primitive type or void, the
1489      * {@code PUBLIC} access flag is present, and the
1490      * {@code PROTECTED} and {@code PRIVATE} access flags are always
1491      * absent. If this {@code Class} object represents an array class, a
1492      * primitive type or void, then the {@code FINAL} access flag is always
1493      * present and the interface access flag is always
1494      * absent. The values of its other access flags are not determined
1495      * by this specification.
1496      *
1497      * @see #getModifiers()
1498      * @jvms 4.1 The ClassFile Structure
1499      * @jvms 4.7.6 The InnerClasses Attribute
1500      * @since 20
1501      */
1502     public Set<AccessFlag> accessFlags() {
1503         // Location.CLASS allows SUPER and AccessFlag.MODULE which
1504         // INNER_CLASS forbids. INNER_CLASS allows PRIVATE, PROTECTED,
1505         // and STATIC, which are not allowed on Location.CLASS.
1506         // Use getClassAccessFlagsRaw to expose SUPER status.
1507         var location = (isMemberClass() || isLocalClass() ||
1508                         isAnonymousClass() || isArray()) ?
1509             AccessFlag.Location.INNER_CLASS :
1510             AccessFlag.Location.CLASS;
1511         return AccessFlag.maskToAccessFlags((location == AccessFlag.Location.CLASS) ?
1512                                             getClassAccessFlagsRaw() :
1513                                             getModifiers(),
1514                                             location);
1515     }
1516 
1517    /**
1518      * Gets the signers of this class.
1519      *
1520      * @return  the signers of this class, or null if there are no signers.  In
1521      *          particular, this method returns null if this {@code Class} object represents
1522      *          a primitive type or void.
1523      * @since   1.1
1524      */
1525     public native Object[] getSigners();
1526 

1527     /**
1528      * Set the signers of this class.
1529      */
1530     native void setSigners(Object[] signers);
1531 

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

1893             simpleName = simpleName.substring(simpleName.lastIndexOf('.') + 1); // strip the package name
1894         }
1895         return simpleName;
1896     }
1897 
1898     /**
1899      * Return an informative string for the name of this class or interface.
1900      *
1901      * @return an informative string for the name of this class or interface
1902      * @since 1.8
1903      */
1904     public String getTypeName() {
1905         if (isArray()) {
1906             try {
1907                 Class<?> cl = this;
1908                 int dimensions = 0;
1909                 do {
1910                     dimensions++;
1911                     cl = cl.getComponentType();
1912                 } while (cl.isArray());
1913                 return cl.getTypeName().concat("[]".repeat(dimensions));
1914             } catch (Throwable e) { /*FALLTHRU*/ }
1915         }
1916         if (isPrimitiveClass()) {
1917             // TODO: null-default
1918             return isPrimaryType() ? getName().concat(".ref") : getName();
1919         } else {
1920             return getName();
1921         }
1922     }
1923 
1924     /**
1925      * Returns the canonical name of the underlying class as
1926      * defined by <cite>The Java Language Specification</cite>.
1927      * Returns {@code null} if the underlying class does not have a canonical
1928      * name. Classes without canonical names include:
1929      * <ul>
1930      * <li>a {@linkplain #isLocalClass() local class}
1931      * <li>a {@linkplain #isAnonymousClass() anonymous class}
1932      * <li>a {@linkplain #isHidden() hidden class}
1933      * <li>an array whose component type does not have a canonical name</li>
1934      * </ul>
1935      *
1936      * The canonical name for a primitive class is the keyword for the
1937      * corresponding primitive type ({@code byte}, {@code short},
1938      * {@code char}, {@code int}, and so on).
1939      *
1940      * <p>An array type has a canonical name if and only if its
1941      * component type has a canonical name. When an array type has a

4119         }
4120         return directory;
4121     }
4122     private transient volatile Map<String, T> enumConstantDirectory;
4123 
4124     /**
4125      * Casts an object to the class or interface represented
4126      * by this {@code Class} object.
4127      *
4128      * @param obj the object to be cast
4129      * @return the object after casting, or null if obj is null
4130      *
4131      * @throws ClassCastException if the object is not
4132      * null and is not assignable to the type T.
4133      *
4134      * @since 1.5
4135      */
4136     @SuppressWarnings("unchecked")
4137     @IntrinsicCandidate
4138     public T cast(Object obj) {
4139         if (isPrimitiveValueType() && obj == null)
4140             throw new NullPointerException(getName() + " is a primitive value type");
4141 
4142         if (obj != null && !isInstance(obj))
4143             throw new ClassCastException(cannotCastMsg(obj));
4144         return (T) obj;
4145     }
4146 
4147     private String cannotCastMsg(Object obj) {
4148         return "Cannot cast " + obj.getClass().getName() + " to " + getName();
4149     }
4150 
4151     /**
4152      * Casts this {@code Class} object to represent a subclass of the class
4153      * represented by the specified class object.  Checks that the cast
4154      * is valid, and throws a {@code ClassCastException} if it is not.  If
4155      * this method succeeds, it always returns a reference to this {@code Class} object.
4156      *
4157      * <p>This method is useful when a client needs to "narrow" the type of
4158      * a {@code Class} object to pass it to an API that restricts the
4159      * {@code Class} objects that it is willing to accept.  A cast would
4160      * generate a compile-time warning, as the correctness of the cast
4161      * could not be checked at runtime (because generic types are implemented

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

4632      * {@code void}, then the result is a field descriptor string which
4633      * is a one-letter code corresponding to a primitive type or {@code void}
4634      * ({@code "B", "C", "D", "F", "I", "J", "S", "Z", "V"}) (JVMS {@jvms 4.3.2}).
4635      *
4636      * @apiNote
4637      * This is not a strict inverse of {@link #forName};
4638      * distinct classes which share a common name but have different class loaders
4639      * will have identical descriptor strings.
4640      *
4641      * @return the descriptor string for this {@code Class} object
4642      * @jvms 4.3.2 Field Descriptors
4643      * @since 12
4644      */
4645     @Override
4646     public String descriptorString() {
4647         if (isPrimitive())
4648             return Wrapper.forPrimitiveType(this).basicTypeString();
4649 
4650         if (isArray()) {
4651             return "[" + componentType.descriptorString();
4652         }
4653         char typeDesc = isPrimitiveValueType() ? 'Q' : 'L';
4654         if (isHidden()) {
4655             String name = getName();
4656             int index = name.indexOf('/');
4657             return new StringBuilder(name.length() + 2)
4658                     .append(typeDesc)
4659                     .append(name.substring(0, index).replace('.', '/'))
4660                     .append('.')
4661                     .append(name, index + 1, name.length())
4662                     .append(';')
4663                     .toString();
4664         } else {
4665             String name = getName().replace('.', '/');
4666             return new StringBuilder(name.length() + 2)
4667                     .append(typeDesc)
4668                     .append(name)
4669                     .append(';')
4670                     .toString();
4671         }
4672     }
4673 
4674     /**
4675      * Returns the component type of this {@code Class}, if it describes
4676      * an array type, or {@code null} otherwise.
4677      *
4678      * @implSpec
4679      * Equivalent to {@link Class#getComponentType()}.
4680      *
4681      * @return a {@code Class} describing the component type, or {@code null}
4682      * if this {@code Class} does not describe an array type
4683      * @since 12
4684      */
4685     @Override
4686     public Class<?> componentType() {
4687         return isArray() ? componentType : null;
< prev index next >